SwaggerUi   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 13.33%

Importance

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

3 Methods

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