Passed
Push — main ( b15a24...935ae5 )
by Axel
04:15
created

CreateThemedResponseSubscriber   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 65
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribedEvents() 0 4 1
A __construct() 0 9 1
A createThemedResponse() 0 41 5
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\ThemeBundle\EventSubscriber;
15
16
use EasyCorp\Bundle\EasyAdminBundle\Config\Option\EA;
17
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
18
use Symfony\Component\DependencyInjection\Attribute\Autowire;
19
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
20
use Symfony\Component\HttpFoundation\RedirectResponse;
21
use Symfony\Component\HttpKernel\Event\RequestEvent;
22
use Symfony\Component\HttpKernel\HttpKernelInterface;
23
use Symfony\Component\HttpKernel\KernelEvents;
24
use Zikula\ThemeBundle\Controller\Dashboard\UserDashboardController;
25
26
class CreateThemedResponseSubscriber implements EventSubscriberInterface
27
{
28
    private bool $installed;
29
30
    private bool $debug;
31
32
    public function __construct(
33
        private readonly AdminUrlGenerator $adminUrlGenerator,
34
        #[Autowire('%env(ZIKULA_INSTALLED)%')]
35
        string $installed,
36
        #[Autowire('%env(APP_DEBUG)%')]
37
        string $debug,
38
    ) {
39
        $this->installed = '0.0.0' !== $installed;
40
        $this->debug = !empty($debug);
41
    }
42
43
    public static function getSubscribedEvents(): array
44
    {
45
        return [
46
            KernelEvents::REQUEST => ['createThemedResponse'],
47
        ];
48
    }
49
50
    public function createThemedResponse(RequestEvent $event): void
51
    {
52
        $request = $event->getRequest();
53
        if (!$event->isMainRequest() || $request->isXmlHttpRequest()) {
54
            return;
55
        }
56
        if (!$this->installed) {
57
            return;
58
        }
59
60
        $route = $request->attributes->get('_route', '');
61
        if (str_starts_with($route, 'home')) { // TODO remove hardcoded assumption -> check if controller is a dashboard
62
            return;
63
        }
64
65
        $dashboard = UserDashboardController::class; // TODO $this->themeEngine->getActiveDashboardControllerClass();
66
        $routeParameters = $request->attributes->get('_route_params');
67
68
        // menu indexes
69
        $index = -1;
70
        $subIndex = -1;
71
72
        $url = $this->adminUrlGenerator
0 ignored issues
show
Unused Code introduced by
The assignment to $url is dead and can be removed.
Loading history...
73
            ->setDashboard($dashboard)
74
            ->setRoute($route, $routeParameters)
75
            ->set(EA::MENU_INDEX, $index)
76
            ->set(EA::SUBMENU_INDEX, $subIndex)
77
            ->generateUrl()
78
        ;
79
        // $event->setResponse(new RedirectResponse($url));
80
        return;
81
82
        $queryParams = $requestParams = $attributes = [];
0 ignored issues
show
Unused Code introduced by
$queryParams = $requestP...= $attributes = array() is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
83
        $attributes['_controller'] = [$dashboard, 'index'];
84
        $attributes['_route'] = $route;
85
        $attributes['_route_params'] = $routeParameters;
86
87
        $subRequest = $request->duplicate($queryParams, $requestParams, $attributes);
88
89
        $event->setResponse(
90
            $event->getKernel()->handle($subRequest, HttpKernelInterface::SUB_REQUEST)
91
        );
92
        /**
93
        TODO
94
95
        check if EAB is available
96
        check if EAB is activated for admin/user area (ThemeBundle configuration)
97
98
        Benefit: works with plain Symfony as well as EAB!
99
         */
100
    }
101
}
102