Passed
Push — master ( ff203e...c53fe2 )
by Evgeniy
01:57
created

SwaggerService   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 82.61%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 19
c 3
b 0
f 0
dl 0
loc 47
ccs 19
cts 23
cp 0.8261
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A fetch() 0 18 3
A setupDefaults() 0 4 1
A getViewPath() 0 3 1
A __construct() 0 4 1
A getViewName() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Swagger\Service;
6
7
use InvalidArgumentException;
8
use OpenApi\Generator;
9
use OpenApi\Processors\MergeIntoOpenApi;
10
use OpenApi\Util;
11
use OpenApi\Annotations\OpenApi;
12
use RuntimeException;
13
use Yiisoft\Aliases\Aliases;
14
15
use function array_map;
16
use function dirname;
17
use function sprintf;
18
19
final class SwaggerService
20
{
21
    private Aliases $aliases;
22
23
    private string $viewPath;
24
    private string $viewName;
25
26 5
    public function __construct(Aliases $aliases)
27
    {
28 5
        $this->aliases = $aliases;
29 5
        $this->setupDefaults();
30 5
    }
31
32 5
    private function setupDefaults(): void
33
    {
34 5
        $this->viewPath = dirname(__DIR__, 2) . '/views';
35 5
        $this->viewName = 'swagger-ui';
36 5
    }
37
38 2
    public function getViewPath(): string
39
    {
40 2
        return $this->aliases->get($this->viewPath);
41
    }
42
43 2
    public function getViewName(): string
44
    {
45 2
        return $this->viewName;
46
    }
47
48 2
    public function fetch(array $annotationPaths): OpenApi
49
    {
50 2
        if ($annotationPaths === []) {
51 1
            throw new InvalidArgumentException('Annotation paths cannot be empty array.');
52
        }
53
54 1
        $directories = array_map(fn (string $path): string => $this->aliases->get($path), $annotationPaths);
55 1
        $openApi = Generator::scan(Util::finder($directories));
56
57 1
        if ($openApi === null) {
58
            throw new RuntimeException(sprintf(
59
                'No OpenApi target set. Run the "%s" processor before "%s::fetch()".',
60
                MergeIntoOpenApi::class,
61
                self::class,
62
            ));
63
        }
64
65 1
        return $openApi;
66
    }
67
}
68