Passed
Pull Request — master (#61)
by Dmitriy
02:49
created

DevPanelMiddleware   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
eloc 11
c 1
b 0
f 0
dl 0
loc 47
ccs 0
cts 14
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A process() 0 32 1
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\View\WebView;
13
use Yiisoft\Yii\Debug\Viewer\Asset\DevPanelAsset;
14
15
final class DevPanelMiddleware implements MiddlewareInterface
16
{
17
    public function __construct(
18
        private string $containerId,
19
        private string $viewerUrl,
20
        private string $backendUrl,
21
        private string $editorUrl,
22
        private AssetManager $assetManager,
23
        private WebView $view,
24
    ) {
25
    }
26
27
    /**
28
     * @inheritDoc
29
     */
30
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
31
    {
32
        $this->assetManager->register(DevPanelAsset::class);
33
        $this->view->registerJs(
34
            <<<JS
35
            const containerId = '{$this->containerId}';
36
            const container = document.createElement('div');
37
            container.setAttribute('id', containerId);
38
            container.style.flex = "1";
39
            document.body.append(container);
40
41
            window['YiiDevPanelWidget'] = window['YiiDevPanelWidget'] ?? {};
42
            window['YiiDevPanelWidget'].config = {
43
                containerId: containerId,
44
                options: {
45
                    application: {
46
                        editorUrl: '{$this->editorUrl}',
47
                    },
48
                    router: {
49
                        basename: '{$this->viewerUrl}',
50
                        useHashRouter: false,
51
                    },
52
                    backend: {
53
                        baseUrl: '{$this->backendUrl}',
54
                    }
55
                },
56
            };
57
            JS,
58
            WebView::POSITION_LOAD,
59
        );
60
61
        return $handler->handle($request);
62
    }
63
}
64