Test Failed
Pull Request — master (#79)
by Dmitriy
03:26 queued 56s
created

SwaggerUi::withJsonUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
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\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
    public function __construct(
32
        private ViewRenderer $viewRenderer,
33
        private SwaggerService $swaggerService,
34
        private AssetManager $assetManager,
35
        private array $params
36
    ) {
37
    }
38
39
    public function handle(ServerRequestInterface $request): ResponseInterface
40
    {
41
        $params = ArrayHelper::merge($this->defaultParams, $this->params);
42
        $params['url'] = $this->jsonUrl;
43
44
        return $this->viewRenderer
45
            ->withViewPath($this->swaggerService->getViewPath())
46
            ->renderPartial($this->swaggerService->getViewName(), [
47
                'assetManager' => $this->assetManager,
48
                'params' => $params,
49
            ]);
50
    }
51
52
    public function withJsonUrl(string $jsonUrl): self
53
    {
54
        $new = clone $this;
55
        $new->jsonUrl = $jsonUrl;
56
        return $new;
57
    }
58
}
59