Skip to content

Commit 9a3eb0b

Browse files
committed
fix: replaced SwaggerDriverInterface with abstract BaseDriver class, added MissedRemoteDocumentationUrlException
Abstract BaseDriver has been added to unify saveTmpData, getTmpData methods semantic.
1 parent 676887a commit 9a3eb0b

13 files changed

+80
-104
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"require": {
1919
"php": ">=7.3.0",
2020
"laravel/framework": ">=5.3.0",
21-
"phpunit/phpunit": ">=7.0|<10.0"
21+
"phpunit/phpunit": ">=7.0"
2222
},
2323
"require-dev": {
2424
"orchestra/testbench": "^6.25",

config/auto-doc.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
|
101101
| The name of driver, which will collect and save documentation
102102
| Feel free to use your own driver class which should be inherited from
103-
| `RonasIT\Support\AutoDoc\Interfaces\SwaggerDriverInterface` interface,
103+
| `RonasIT\Support\AutoDoc\Drivers\BaseDriver` class,
104104
| or one of our drivers from the `drivers` config:
105105
*/
106106
'driver' => env('SWAGGER_DRIVER', 'local'),

src/Drivers/BaseDriver.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace RonasIT\Support\AutoDoc\Drivers;
4+
5+
abstract class BaseDriver
6+
{
7+
protected $tempFilePath;
8+
9+
public function saveTmpData($data)
10+
{
11+
file_put_contents($this->tempFilePath, json_encode($data));
12+
}
13+
14+
public function getTmpData()
15+
{
16+
if (file_exists($this->tempFilePath)) {
17+
$content = file_get_contents($this->tempFilePath);
18+
19+
return json_decode($content, true);
20+
}
21+
22+
return null;
23+
}
24+
25+
abstract public function saveData(): void;
26+
27+
abstract public function getDocumentation(): array;
28+
}

src/Drivers/LocalDriver.php

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,23 @@
33
namespace RonasIT\Support\AutoDoc\Drivers;
44

55
use Illuminate\Contracts\Filesystem\FileNotFoundException;
6-
use RonasIT\Support\AutoDoc\Exceptions\MissedProductionFilePathException;
7-
use RonasIT\Support\AutoDoc\Interfaces\SwaggerDriverInterface;
6+
use RonasIT\Support\AutoDoc\Exceptions\MissedRemoteDocumentationUrlException;
87

9-
class LocalDriver implements SwaggerDriverInterface
8+
class LocalDriver extends BaseDriver
109
{
1110
protected $prodFilePath;
12-
protected $tempFilePath;
1311

1412
public function __construct()
1513
{
1614
$this->prodFilePath = config('auto-doc.drivers.local.production_path');
1715
$this->tempFilePath = storage_path('temp_documentation.json');
1816

1917
if (empty($this->prodFilePath)) {
20-
throw new MissedProductionFilePathException();
18+
throw new MissedRemoteDocumentationUrlException();
2119
}
2220
}
2321

24-
public function saveTmpData($data)
25-
{
26-
file_put_contents($this->tempFilePath, json_encode($data));
27-
}
28-
29-
public function getTmpData()
30-
{
31-
if (file_exists($this->tempFilePath)) {
32-
$content = file_get_contents($this->tempFilePath);
33-
34-
return json_decode($content, true);
35-
}
36-
37-
return null;
38-
}
39-
40-
public function saveData()
22+
public function saveData(): void
4123
{
4224
file_put_contents($this->prodFilePath, json_encode($this->getTmpData()));
4325

src/Drivers/RemoteDriver.php

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,25 @@
33
namespace RonasIT\Support\AutoDoc\Drivers;
44

55
use Illuminate\Contracts\Filesystem\FileNotFoundException;
6-
use RonasIT\Support\AutoDoc\Interfaces\SwaggerDriverInterface;
6+
use RonasIT\Support\AutoDoc\Exceptions\MissedRemoteDocumentationUrlException;
77

8-
class RemoteDriver implements SwaggerDriverInterface
8+
class RemoteDriver extends BaseDriver
99
{
1010
protected $key;
1111
protected $remoteUrl;
12-
protected $tempFilePath;
1312

1413
public function __construct()
1514
{
1615
$this->key = config('auto-doc.drivers.remote.key');
1716
$this->remoteUrl = config('auto-doc.drivers.remote.url');
1817
$this->tempFilePath = storage_path('temp_documentation.json');
19-
}
20-
21-
public function saveTmpData($data)
22-
{
23-
file_put_contents($this->tempFilePath, json_encode($data));
24-
}
2518

26-
public function getTmpData()
27-
{
28-
if (file_exists($this->tempFilePath)) {
29-
$content = file_get_contents($this->tempFilePath);
30-
31-
return json_decode($content, true);
19+
if (empty($this->remoteUrl)) {
20+
throw new MissedRemoteDocumentationUrlException();
3221
}
33-
34-
return null;
3522
}
3623

37-
public function saveData()
24+
public function saveData(): void
3825
{
3926
$this->makeHttpRequest('post', $this->getUrl(), $this->getTmpData(), [
4027
'Content-Type: application/json'
@@ -64,7 +51,7 @@ protected function getUrl(): string
6451
/**
6552
* @codeCoverageIgnore
6653
*/
67-
protected function makeHttpRequest($type, $url, $data = [], $headers = [])
54+
protected function makeHttpRequest($type, $url, $data = [], $headers = []): array
6855
{
6956
$curl = curl_init();
7057

src/Drivers/StorageDriver.php

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@
44

55
use Illuminate\Contracts\Filesystem\FileNotFoundException;
66
use Illuminate\Support\Facades\Storage;
7-
use RonasIT\Support\AutoDoc\Exceptions\MissedProductionFilePathException;
8-
use RonasIT\Support\AutoDoc\Interfaces\SwaggerDriverInterface;
7+
use RonasIT\Support\AutoDoc\Exceptions\MissedRemoteDocumentationUrlException;
98

10-
class StorageDriver implements SwaggerDriverInterface
9+
class StorageDriver extends BaseDriver
1110
{
1211
protected $disk;
1312
protected $prodFilePath;
14-
protected $tempFilePath;
1513

1614
public function __construct()
1715
{
@@ -20,27 +18,11 @@ public function __construct()
2018
$this->tempFilePath = storage_path('temp_documentation.json');
2119

2220
if (empty($this->prodFilePath)) {
23-
throw new MissedProductionFilePathException();
21+
throw new MissedRemoteDocumentationUrlException();
2422
}
2523
}
2624

27-
public function saveTmpData($data)
28-
{
29-
file_put_contents($this->tempFilePath, json_encode($data));
30-
}
31-
32-
public function getTmpData()
33-
{
34-
if (file_exists($this->tempFilePath)) {
35-
$content = file_get_contents($this->tempFilePath);
36-
37-
return json_decode($content, true);
38-
}
39-
40-
return null;
41-
}
42-
43-
public function saveData()
25+
public function saveData(): void
4426
{
4527
$this->disk->put($this->prodFilePath, json_encode($this->getTmpData()));
4628

src/Exceptions/InvalidDriverClassException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ class InvalidDriverClassException extends Exception
88
{
99
public function __construct(string $driver)
1010
{
11-
parent::__construct("The driver '{$driver}' is not implements the SwaggerDriverInterface.");
11+
parent::__construct("The driver '{$driver}' does not extend the BaseDriver class.");
1212
}
1313
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace RonasIT\Support\AutoDoc\Exceptions;
4+
5+
use Exception;
6+
7+
class MissedRemoteDocumentationUrlException extends Exception
8+
{
9+
public function __construct($message = null, $code = 0, Exception $previous = null)
10+
{
11+
$message = $message ?? 'Remote documentation url missed in config';
12+
13+
parent::__construct($message, $code, $previous);
14+
}
15+
}

src/Interfaces/SwaggerDriverInterface.php

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/Services/SwaggerService.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@
88
use Illuminate\Support\Str;
99
use ReflectionClass;
1010
use Illuminate\Http\Request;
11+
use RonasIT\Support\AutoDoc\Drivers\BaseDriver;
1112
use RonasIT\Support\AutoDoc\Exceptions\LegacyConfigException;
1213
use RonasIT\Support\AutoDoc\Exceptions\WrongSecurityConfigException;
1314
use RonasIT\Support\AutoDoc\Traits\GetDependenciesTrait;
1415
use Symfony\Component\HttpFoundation\Response;
15-
use RonasIT\Support\AutoDoc\Interfaces\SwaggerDriverInterface;
1616
use RonasIT\Support\AutoDoc\Exceptions\InvalidDriverClassException;
1717
use RonasIT\Support\AutoDoc\Exceptions\SwaggerDriverClassNotFoundException;
1818

1919
/**
20-
* @property SwaggerDriverInterface $driver
20+
* @property BaseDriver $driver
2121
*/
2222
class SwaggerService
2323
{
@@ -97,7 +97,7 @@ protected function setDriver()
9797
$this->driver = app($className);
9898
}
9999

100-
if (!$this->driver instanceof SwaggerDriverInterface) {
100+
if (!$this->driver instanceof BaseDriver) {
101101
throw new InvalidDriverClassException($driver);
102102
}
103103
}

0 commit comments

Comments
 (0)