Skip to content

Commit 76f09fc

Browse files
committed
fix: remove tmp data after saving production data;
1 parent 4453ec7 commit 76f09fc

File tree

4 files changed

+20
-13
lines changed

4 files changed

+20
-13
lines changed

src/Drivers/LocalDriver.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ public function getTmpData()
4040
public function saveData()
4141
{
4242
file_put_contents($this->prodFilePath, json_encode($this->getTmpData()));
43+
44+
if (file_exists($this->tempFilePath)) {
45+
unlink($this->tempFilePath);
46+
}
4347
}
4448

4549
public function getDocumentation(): array

src/Drivers/StorageDriver.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class StorageDriver implements SwaggerDriverInterface
1515

1616
public function __construct()
1717
{
18-
$this->disk = config('auto-doc.drivers.storage.disk');
18+
$this->disk = Storage::disk(config('auto-doc.drivers.storage.disk'));
1919
$this->prodFilePath = config('auto-doc.drivers.storage.production_path');
2020
$this->tempFilePath = 'temp_documentation.json';
2121

@@ -26,13 +26,13 @@ public function __construct()
2626

2727
public function saveTmpData($data)
2828
{
29-
Storage::disk($this->disk)->put($this->tempFilePath, json_encode($data));
29+
$this->disk->put($this->tempFilePath, json_encode($data));
3030
}
3131

3232
public function getTmpData()
3333
{
34-
if (Storage::disk($this->disk)->exists($this->tempFilePath)) {
35-
$content = Storage::disk($this->disk)->get($this->tempFilePath);
34+
if ($this->disk->exists($this->tempFilePath)) {
35+
$content = $this->disk->get($this->tempFilePath);
3636

3737
return json_decode($content, true);
3838
}
@@ -42,16 +42,20 @@ public function getTmpData()
4242

4343
public function saveData()
4444
{
45-
Storage::disk($this->disk)->put($this->prodFilePath, json_encode($this->getTmpData()));
45+
$this->disk->put($this->prodFilePath, json_encode($this->getTmpData()));
46+
47+
if ($this->disk->exists($this->tempFilePath)) {
48+
$this->disk->delete($this->tempFilePath);
49+
}
4650
}
4751

4852
public function getDocumentation(): array
4953
{
50-
if (!Storage::disk($this->disk)->exists($this->prodFilePath)) {
54+
if (!$this->disk->exists($this->prodFilePath)) {
5155
throw new FileNotFoundException();
5256
}
5357

54-
$fileContent = Storage::disk($this->disk)->get($this->prodFilePath);
58+
$fileContent = $this->disk->get($this->prodFilePath);
5559

5660
return json_decode($fileContent, true);
5761
}

tests/LocalDriverTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,14 @@ public function testGetAndSaveTmpData()
6969

7070
public function testSaveData()
7171
{
72-
$this->localDriverClass->saveTmpData($this->tmpData);
72+
file_put_contents($this->tmpDocumentationFilePath, json_encode($this->tmpData));
7373

7474
$this->localDriverClass->saveData();
7575

7676
$this->assertFileExists($this->productionFilePath);
7777
$this->assertFileEquals($this->generateFixturePath('tmp_data_non_formatted.json'), $this->productionFilePath);
7878

79-
$this->assertEqualsJsonFixture('tmp_data', $this->localDriverClass->getTmpData());
79+
$this->assertFileDoesNotExist($this->tmpDocumentationFilePath);
8080
}
8181

8282
public function testGetDocumentation()

tests/StorageDriverTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ public function testSaveTmpData()
3838

3939
$this->disk->assertExists($this->tmpDocumentationFilePath);
4040

41-
$tmpDocumentation = json_decode($this->disk->get($this->tmpDocumentationFilePath), true);
42-
$this->assertEqualsJsonFixture('tmp_data_non_formatted', $tmpDocumentation);
41+
$this->assertEqualsFixture('tmp_data_non_formatted.json', $this->disk->get($this->tmpDocumentationFilePath));
4342
}
4443

4544
public function testGetTmpData()
@@ -76,14 +75,14 @@ public function testGetAndSaveTmpData()
7675

7776
public function testSaveData()
7877
{
79-
$this->storageDriverClass->saveTmpData($this->tmpData);
78+
$this->disk->put($this->tmpDocumentationFilePath, json_encode($this->tmpData));
8079

8180
$this->storageDriverClass->saveData();
8281

8382
$this->disk->assertExists($this->productionFilePath);
8483
$this->assertEqualsFixture('tmp_data_non_formatted.json', $this->disk->get($this->productionFilePath));
8584

86-
$this->assertEqualsJsonFixture('tmp_data', $this->storageDriverClass->getTmpData());
85+
$this->disk->assertMissing($this->tmpDocumentationFilePath);
8786
}
8887

8988
public function testGetDocumentation()

0 commit comments

Comments
 (0)