SwaggerUi   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 42
ccs 15
cts 15
cp 1
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 10 1
A __construct() 0 6 1
A withJsonUrl() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Swagger\Action;
6
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\ServerRequestInterface;
9
use Psr\Http\Server\RequestHandlerInterface;
10
use Yiisoft\Arrays\ArrayHelper;
11
use Yiisoft\Assets\AssetManager;
12
use Yiisoft\Swagger\Service\SwaggerService;
13
use Yiisoft\Yii\View\Renderer\ViewRenderer;
14
15
final class SwaggerUi implements RequestHandlerInterface
16
{
17
    private array $defaultParams = [
18
        'dom_id' => '#swagger-ui',
19
        'deepLinking' => true,
20
        'presets' => [
21
            'SwaggerUIBundle.presets.apis',
22
            'SwaggerUIStandalonePreset',
23
        ],
24
        'plugins' => [
25
            'SwaggerUIBundle.plugins.DownloadUrl',
26
        ],
27
        'layout' => 'StandaloneLayout',
28
    ];
29
    private string $jsonUrl = '/';
30
31 2
    public function __construct(
32
        private ViewRenderer $viewRenderer,
33
        private SwaggerService $swaggerService,
34
        private AssetManager $assetManager,
35
        private array $params
36
    ) {
37 2
    }
38
39 1
    public function handle(ServerRequestInterface $request): ResponseInterface
40
    {
41 1
        $params = ArrayHelper::merge($this->defaultParams, $this->params);
42 1
        $params['url'] = $this->jsonUrl;
43
44 1
        return $this->viewRenderer
45 1
            ->withViewPath($this->swaggerService->getViewPath())
46 1
            ->renderPartial($this->swaggerService->getViewName(), [
47 1
                'assetManager' => $this->assetManager,
48 1
                'params' => $params,
49 1
            ]);
50
    }
51
52 2
    public function withJsonUrl(string $jsonUrl): self
53
    {
54 2
        $new = clone $this;
55 2
        $new->jsonUrl = $jsonUrl;
56 2
        return $new;
57
    }
58
}
59