RenderOpenApi   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 36
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 9 1
A __construct() 0 4 2
A getAvailableFormats() 0 3 1
1
<?php declare(strict_types=1);
2
3
namespace Diegoborgs\NaturalSwaggerPhp\Renders;
4
5
use OpenApi\Generator;
6
7
class RenderOpenApi
8
{
9
    public const HTML = 'html';
10
    public const JSON = 'json';
11
    public const YAML = 'yaml';
12
13
    public const PHP_PATTERN = "*.php";
14
15
    /**
16
     * @var array<string, OpenApiRendererInterface|null>
17
     */
18
    private array $renderers;
19
20
    public function __construct(OpenApiRendererInterface ...$renderers)
21
    {
22
        foreach ($renderers as $renderer) {
23
            $this->renderers[$renderer->getFormat()] = $renderer;
24
        }
25
    }
26
27
    public function getAvailableFormats(): array
28
    {
29
        return array_keys($this->renderers);
30
    }
31
    /**
32
     * @throws \InvalidArgumentException If the area to dump is not valid
33
     */
34
    public function render(string $format = RenderOpenApi::JSON, array $options = []): string
35
    {
36
        $options = array_merge([
37
            'base_path' => __DIR__
38
        ], $options);
39
40
        $spec = Generator::scan([$options['base_path']]);
41
42
        return $this->renderers[$format]->render($spec, $options);
43
    }
44
}
45