Passed
Push — main ( b5d404...906970 )
by Axel
14:03
created

getDashboardControllerFqcn()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 9
c 0
b 0
f 0
nc 16
nop 1
dl 0
loc 18
rs 9.6111
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\Contracts\Controller\DashboardControllerInterface;
18
use EasyCorp\Bundle\EasyAdminBundle\Factory\AdminContextFactory;
19
use EasyCorp\Bundle\EasyAdminBundle\Factory\ControllerFactory;
20
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
21
use Symfony\Component\HttpFoundation\Request;
22
use Symfony\Component\HttpKernel\Event\RequestEvent;
23
use Symfony\Component\HttpKernel\KernelEvents;
24
use Zikula\ThemeBundle\Helper\FallbackDashboardDetector;
25
26
class CreateThemedResponseSubscriber implements EventSubscriberInterface
27
{
28
    public function __construct(
29
        private readonly AdminContextFactory $adminContextFactory,
30
        private readonly ControllerFactory $controllerFactory,
31
        private readonly FallbackDashboardDetector $dashboardDetector
32
    ) {
33
    }
34
35
    public static function getSubscribedEvents(): array
36
    {
37
        return [
38
            KernelEvents::REQUEST => ['onKernelRequest', -50], // run after AdminRouterSubscriber (has default priority 0)
39
        ];
40
    }
41
42
    public function onKernelRequest(RequestEvent $event): void
43
    {
44
        $request = $event->getRequest();
45
        if (null !== $adminContext = $request->attributes->get(EA::CONTEXT_REQUEST_ATTRIBUTE)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $adminContext is dead and can be removed.
Loading history...
46
            // nothing to do if we already have an EA context
47
            return;
48
        }
49
50
        if (null === $dashboardControllerFqcn = $this->getDashboardControllerFqcn($request)) {
51
            // current controller is not a dashboard controller, so add a fallback
52
            $dashboardControllerFqcn = $this->dashboardDetector->getDashboardControllerFqcn($request);
53
        }
54
55
        if (null === $dashboardControllerInstance = $this->getDashboardControllerInstance($dashboardControllerFqcn, $request)) {
56
            return;
57
        }
58
59
        // $crudControllerInstance is always null atm but this seems fine as we are dealing with a regular Symfony controller
60
        $crudControllerInstance = $this->getCrudControllerInstance($request);
61
        $adminContext = $this->adminContextFactory->create($request, $dashboardControllerInstance, $crudControllerInstance);
62
        $request->attributes->set(EA::CONTEXT_REQUEST_ATTRIBUTE, $adminContext);
63
64
        $request->query->set(EA::ROUTE_NAME, $request->attributes->get('_route'));
65
        $request->query->set(EA::ROUTE_PARAMS, $request->attributes->get('_route_params'));
66
    }
67
68
    /**
69
     * Copy of AdminRouterSubscriber#getDashboardControllerFqcn.
70
     */
71
    private function getDashboardControllerFqcn(Request $request): ?string
72
    {
73
        $controller = $request->attributes->get('_controller');
74
        $controllerFqcn = null;
75
76
        if (\is_string($controller)) {
77
            [$controllerFqcn, ] = explode('::', $controller);
78
        }
79
80
        if (\is_array($controller)) {
81
            $controllerFqcn = $controller[0];
82
        }
83
84
        if (\is_object($controller)) {
85
            $controllerFqcn = $controller::class;
86
        }
87
88
        return is_subclass_of($controllerFqcn, DashboardControllerInterface::class) ? $controllerFqcn : null;
89
    }
90
91
    /**
92
     * Copy of AdminRouterSubscriber#getDashboardControllerInstance.
93
     */
94
    private function getDashboardControllerInstance(string $dashboardControllerFqcn, Request $request): ?DashboardControllerInterface
95
    {
96
        return $this->controllerFactory->getDashboardControllerInstance($dashboardControllerFqcn, $request);
97
    }
98
99
    /**
100
     * Copy of AdminRouterSubscriber#getCrudControllerInstance.
101
     */
102
    private function getCrudControllerInstance(Request $request): ?CrudControllerInterface
0 ignored issues
show
Bug introduced by
The type Zikula\ThemeBundle\Event...CrudControllerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
103
    {
104
        $crudControllerFqcn = $request->query->get(EA::CRUD_CONTROLLER_FQCN);
105
106
        $crudAction = $request->query->get(EA::CRUD_ACTION);
107
108
        return $this->controllerFactory->getCrudControllerInstance($crudControllerFqcn, $crudAction, $request);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->controller... $crudAction, $request) could return the type EasyCorp\Bundle\EasyAdmi...CrudControllerInterface which is incompatible with the type-hinted return Zikula\ThemeBundle\Event...ontrollerInterface|null. Consider adding an additional type-check to rule them out.
Loading history...
109
    }
110
}
111