Completed
Pull Request — master (#90)
by Arnaud
07:12 queued 36s
created

KernelSubscriber   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 0
loc 94
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A getSubscribedEvents() 0 7 1
A onKernelController() 0 20 2
A onKernelRequest() 0 8 1
1
<?php
2
3
namespace LAG\AdminBundle\Event\Subscriber;
4
5
use LAG\AdminBundle\Action\Factory\ActionFactory;
6
use LAG\AdminBundle\Admin\Factory\AdminFactory;
7
use LAG\AdminBundle\Admin\Request\RequestHandler;
8
use LAG\AdminBundle\Application\Configuration\ApplicationConfigurationStorage;
9
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
10
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
11
use Symfony\Component\HttpKernel\KernelEvents;
12
13
/**
14
 * This class allow an Admin and an ActionConfiguration to be injected into the current Action/controller. It also add
15
 * the global Admin application configuration to Twig global parameters.
16
 */
17
class KernelSubscriber implements EventSubscriberInterface
18
{
19
    /**
20
     * @var RequestHandler
21
     */
22
    private $requestHandler;
23
    
24
    /**
25
     * @var AdminFactory
26
     */
27
    private $adminFactory;
28
    
29
    /**
30
     * @var ActionFactory
31
     */
32
    private $actionFactory;
33
   
34
    /**
35
     * @var ApplicationConfigurationStorage
36
     */
37
    private $applicationConfigurationStorage;
38
    
39
    /**
40 2
     * KernelSubscriber constructor.
41
     *
42
     * @param AdminFactory                    $adminFactory
43
     * @param ActionFactory                   $actionFactory
44
     * @param RequestHandler                  $requestHandler
45 2
     * @param ApplicationConfigurationStorage $applicationConfigurationStorage
46 2
     */
47 2
    public function __construct(
48 2
        AdminFactory $adminFactory,
49
        ActionFactory $actionFactory,
50
        RequestHandler $requestHandler,
51
        ApplicationConfigurationStorage $applicationConfigurationStorage
52
    ) {
53
        $this->requestHandler = $requestHandler;
54
        $this->adminFactory = $adminFactory;
55 1
        $this->actionFactory = $actionFactory;
56
        $this->applicationConfigurationStorage = $applicationConfigurationStorage;
57
    }
58 1
    
59 1
    /**
60
     * Return the subscribed events (kernelController and kernelView).
61
     *
62
     * @return array
63
     */
64
    public static function getSubscribedEvents()
65
    {
66
        return [
67
            KernelEvents::CONTROLLER => 'onKernelController',
68 1
            KernelEvents::REQUEST => 'onKernelRequest',
69
        ];
70 1
    }
71 1
    
72
    /**
73
     * On kernelController event, an Admin and an ActionConfiguration can be injected to the current action.
74
     *
75 1
     * @param FilterControllerEvent $event
76
     */
77
    public function onKernelController(FilterControllerEvent $event)
78 1
    {
79 1
        $controller = $event->getController();
80
        $request = $event->getRequest();
81
        
82
        // If the current request is supported by the request handler, we do load the requested Admin and its
83 1
        // configurations
84 1
        if ($this->requestHandler->supports($request)) {
85
            // Inject the current Admin into the Controller
86
            $this
87 1
                ->adminFactory
88
                ->injectAdmin($controller, $request)
89
            ;
90
            // Inject the resolved Configuration into the Controller
91
            $this
92
                ->actionFactory
93 1
                ->injectConfiguration($controller, $request)
94
            ;
95
        }
96 1
    }
97 1
    
98
    /**
99 1
     * On KernelRequest event, we init the Admin factory, so the Admins and Actions configurations will be available
100
     * in the controller.
101
     */
102
    public function onKernelRequest()
103
    {
104
        // Init the admin factory
105
        $this
106
            ->adminFactory
107
            ->init()
108
        ;
109
    }
110
}
111