HTMLOpenApiRenderer   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 31
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 9 1
A getFormat() 0 3 1
A __construct() 0 4 1
1
<?php declare(strict_types=1);
2
3
namespace Diegoborgs\NaturalSwaggerPhp\Renders;
4
5
use OpenApi\Annotations\OpenApi;
6
use Twig\Loader\FilesystemLoader;
7
8
class HTMLOpenApiRenderer implements OpenApiRendererInterface
9
{
10
    private const BASE_ASSETS = "https://cdn.jsdelivr.net/gh/eudiegoborgs/natural-swagger-php/src/Resources/assets";
11
12
    private \Twig\Environment $twig;
13
14
    public function __construct()
15
    {
16
        $loader = new FilesystemLoader(dirname(__FILE__) . '/../Resources/Views');
17
        $this->twig = new \Twig\Environment($loader);
18
    }
19
20
    public function getFormat(): string
21
    {
22
        return RenderOpenApi::HTML;
23
    }
24
25
    /**
26
     * @throws \Twig\Error\RuntimeError
27
     * @throws \Twig\Error\SyntaxError
28
     * @throws \Twig\Error\LoaderError
29
     */
30
    public function render(OpenApi $spec, array $options = []): string
31
    {
32
        return $this->twig->render(
33
            'SwaggerUi/swagger.html.twig',
34
            array_merge_recursive(
35
                $options,
36
                [
37
                    'swagger_data' => ['spec' => json_decode($spec->toJson(), true)],
38
                    'base_assets' => self::BASE_ASSETS
39
                ]
40
            )
41
        );
42
    }
43
}
44