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

SwaggerService::fetch()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.4326

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
c 3
b 0
f 0
nc 3
nop 1
dl 0
loc 18
ccs 7
cts 11
cp 0.6364
crap 3.4326
rs 9.9332
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