DevPanelMiddleware::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 0
c 1
b 0
f 0
dl 0
loc 10
ccs 0
cts 1
cp 0
rs 10
cc 1
nc 1
nop 8
crap 2

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Viewer\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\Assets\AssetManager;
12
use Yiisoft\Router\UrlGeneratorInterface;
13
use Yiisoft\View\WebView;
14
use Yiisoft\Yii\Debug\Viewer\Asset\DevPanelAsset;
15
16
final class DevPanelMiddleware implements MiddlewareInterface
17
{
18
    public function __construct(
19
        private string $containerId,
20
        private string $viewerUrl,
21
        private string $backendUrl,
22
        private string $editorUrl,
23
        private string $staticUrl,
24
        private AssetManager $assetManager,
25
        private WebView $view,
26
        private UrlGeneratorInterface $urlGenerator,
27
    ) {
28
    }
29
30
    /**
31
     * @inheritDoc
32
     */
33
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
34
    {
35
        $baseUriPrefix = $this->urlGenerator->getUriPrefix();
36
        $this->assetManager->registerCustomized(DevPanelAsset::class, ['baseUrl' => $this->staticUrl]);
37
        $this->view->registerJs(
38
            <<<JS
39
            (function(){
40
            let queryParams = {toolbar: '1'};
41
            try {
42
                queryParams = Object.fromEntries(new URLSearchParams(location.search));
43
            } catch (e) {
44
                console.error('Error while parsing query params: ', e);
45
            }
46
47
            const containerId = '{$this->containerId}';
48
            const container = document.createElement('div');
49
            container.setAttribute('id', containerId);
50
            container.style.flex = "1";
51
            document.body.append(container);
52
53
            window['YiiDevPanelWidget'] = window['YiiDevPanelWidget'] ?? {};
54
            window['YiiDevPanelWidget'].config = {
55
                containerId: containerId,
56
                options: {
57
                    application: {
58
                        editorUrl: '{$this->editorUrl}',
59
                    },
60
                    modules: {
61
                        toolbar: queryParams?.toolbar !== '0',
62
                    },
63
                    router: {
64
                        basename: '{$baseUriPrefix}{$this->viewerUrl}',
65
                        useHashRouter: false,
66
                    },
67
                    backend: {
68
                        baseUrl: '{$this->backendUrl}{$baseUriPrefix}',
69
                        usePreferredUrl: true,
70
                    }
71
                },
72
            };
73
            })();
74
            JS,
75
            WebView::POSITION_LOAD,
76
        );
77
78
        return $handler->handle($request);
79
    }
80
}
81