Skip to content

Commit f92b7b6

Browse files
authored
Merge pull request #13 from sevaske/fix-pr-11
fix: Fixed the issue in the README.md + Fixed the directory creation logic in ensureDirectoryExists()
2 parents 02790f9 + 0a0e80c commit f92b7b6

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

README.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ $certificate = $json_data['certificate'];
321321
$secret = $json_data['secret'];
322322

323323
// Load the private key
324-
$privateKey = (new Storage)->put(__DIR__ . '/output/private.pem');
324+
$privateKey = (new Storage)->get(__DIR__ . '/output/private.pem');
325325
$cleanPrivateKey = trim(str_replace(["-----BEGIN PRIVATE KEY-----", "-----END PRIVATE KEY-----"], "", $privateKey));
326326

327327
// Create a Certificate instance
@@ -332,10 +332,9 @@ $certificate = new Certificate(
332332
);
333333

334334
// Sign the invoice
335-
$signedInvoice = InvoiceSigner::signInvoice($xmlInvoice, $certificate);
336-
335+
$signedInvoice = InvoiceSigner::signInvoice($xmlInvoice, $certificate)->getXML();
337336
// Save the signed invoice
338-
InvoiceSigner::signInvoice($xmlInvoice, $certificate)->saveXMLFile('/output/signed_invoice.xml');
337+
(new Storage)->put(__DIR__.'/output/signed_invoice.xml', $signedInvoice);
339338
```
340339

341340
### 📤 **5. Submitting the Signed Invoice to ZATCA**

examples/SigningInvoices.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222
// get private key
2323
$privateKey = file_get_contents(__DIR__ .'/output/private.pem');
2424

25-
$claenPrivateKey = trim(str_replace(["-----BEGIN PRIVATE KEY-----", "-----END PRIVATE KEY-----"], "", $privateKey));
25+
$cleanPrivateKey = trim(str_replace(["-----BEGIN PRIVATE KEY-----", "-----END PRIVATE KEY-----"], "", $privateKey));
2626

2727
$certificate = (new Certificate(
2828
$certificate,
29-
$claenPrivateKey,
29+
$cleanPrivateKey,
3030
$secret
3131
));
3232

src/Storage.php

+9-6
Original file line numberDiff line numberDiff line change
@@ -147,18 +147,21 @@ protected function ensureDirectoryExists(string $path): void
147147
}
148148

149149
// If directory exists but is not writable, throw exception
150-
if (is_dir($path) && !is_writable($path)) {
151-
throw new ZatcaStorageException('Directory exists but is not writable.', ['path' => $path]);
150+
if (is_dir($path)) {
151+
if (!is_writable($path)) {
152+
throw new ZatcaStorageException('Directory exists but is not writable.', ['path' => $path]);
153+
}
154+
return;
152155
}
153156

154-
// If parent directory is not writable, fail before mkdir()
157+
// Ensure the parent directory exists before creating the target directory
155158
$parentDir = dirname($path);
156-
if (!is_writable($parentDir)) {
157-
throw new ZatcaStorageException('Parent directory is not writable.', ['path' => $parentDir]);
159+
if (!is_dir($parentDir)) {
160+
$this->ensureDirectoryExists($parentDir); // Recursively create parent directories
158161
}
159162

160163
// If directory does not exist, attempt to create it
161-
if (!is_dir($path) && !mkdir($path, 0777, true) && !is_dir($path)) {
164+
if (!is_dir($path) && !mkdir($path, 0755, true) && !is_dir($path)) {
162165
throw new ZatcaStorageException('Failed to create directory.', ['path' => $path]);
163166
}
164167
}

0 commit comments

Comments
 (0)