Completed
Pull Request — master (#90)
by Arnaud
03:24
created

KernelSubscriber::onKernelController()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 20
ccs 9
cts 9
cp 1
rs 9.4285
cc 2
eloc 10
nc 2
nop 1
crap 2
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 Symfony\Component\EventDispatcher\EventSubscriberInterface;
9
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
10
use Symfony\Component\HttpKernel\KernelEvents;
11
12
/**
13
 * This class allow an Admin and an ActionConfiguration to be injected into the current Action/controller. It also add
14
 * the global Admin application configuration to Twig global parameters.
15
 */
16
class KernelSubscriber implements EventSubscriberInterface
17
{
18
    /**
19
     * @var RequestHandler
20
     */
21
    private $requestHandler;
22
    
23
    /**
24
     * @var AdminFactory
25
     */
26
    private $adminFactory;
27
    
28
    /**
29
     * @var ActionFactory
30
     */
31
    private $actionFactory;
32
    
33
    /**
34
     * KernelSubscriber constructor.
35
     *
36
     * @param AdminFactory $adminFactory
37
     * @param ActionFactory $actionFactory
38
     * @param RequestHandler $requestHandler
39
     */
40 2
    public function __construct(
41
        AdminFactory $adminFactory,
42
        ActionFactory $actionFactory,
43
        RequestHandler $requestHandler
44
    ) {
45 2
        $this->requestHandler = $requestHandler;
46 2
        $this->adminFactory = $adminFactory;
47 2
        $this->actionFactory = $actionFactory;
48 2
    }
49
    
50
    /**
51
     * Return the subscribed events (kernelController and kernelView).
52
     *
53
     * @return array
54
     */
55 1
    public static function getSubscribedEvents()
56
    {
57
        return [
58 1
            KernelEvents::CONTROLLER => 'onKernelController',
59 1
            KernelEvents::REQUEST => 'onKernelRequest',
60
        ];
61
    }
62
    
63
    /**
64
     * On kernelController event, an Admin and an ActionConfiguration can be injected to the current action.
65
     *
66
     * @param FilterControllerEvent $event
67
     */
68 1
    public function onKernelController(FilterControllerEvent $event)
69
    {
70 1
        $controller = $event->getController();
71 1
        $request = $event->getRequest();
72
        
73
        // if the current request is supported by the request handler, we do load the requested Admin and its
74
        // configurations
75 1
        if ($this->requestHandler->supports($request)) {
76
            // inject the current Admin into the Controller
77
            $this
78 1
                ->adminFactory
79 1
                ->injectAdmin($controller, $request)
80
            ;
81
            // inject the resolved Configuration into the Controller
82
            $this
83 1
                ->actionFactory
84 1
                ->injectConfiguration($controller, $request)
85
            ;
86
        }
87 1
    }
88
    
89
    /**
90
     * On KernelRequest event, we init the Admin factory, so the Admins and Actions configurations will be available
91
     * in the controller.
92
     */
93 1
    public function onKernelRequest()
94
    {
95
        $this
96 1
            ->adminFactory
97 1
            ->init()
98
        ;
99 1
    }
100
}
101