ControllerSubscriber   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 29
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getSubscribedEvents() 0 6 1
A registerCurrentController() 0 9 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Explicit Architecture POC,
7
 * which is created on top of the Symfony Demo application.
8
 *
9
 * (c) Herberto Graça <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Acme\App\Infrastructure\TemplateEngine\Twig\Extension\SourceCode;
16
17
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
18
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
19
use Symfony\Component\HttpKernel\KernelEvents;
20
21
/**
22
 * Defines the method that 'listens' to the 'kernel.controller' event, which is
23
 * triggered whenever a controller is executed in the application.
24
 *
25
 * @author Ryan Weaver <[email protected]>
26
 * @author Javier Eguiluz <[email protected]>
27
 */
28
class ControllerSubscriber implements EventSubscriberInterface
29
{
30
    /**
31
     * @var SourceCodeExtension
32
     */
33
    private $twigExtension;
34
35
    public function __construct(SourceCodeExtension $twigExtension)
36
    {
37
        $this->twigExtension = $twigExtension;
38
    }
39
40
    public static function getSubscribedEvents(): array
41
    {
42
        return [
43
            KernelEvents::CONTROLLER => 'registerCurrentController',
44
        ];
45
    }
46
47
    public function registerCurrentController(FilterControllerEvent $event): void
48
    {
49
        // this check is needed because in Symfony a request can perform any
50
        // number of sub-requests. See
51
        // https://symfony.com/doc/current/components/http_kernel/introduction.html#sub-requests
52
        if ($event->isMasterRequest()) {
53
            $this->twigExtension->setController($event->getController());
54
        }
55
    }
56
}
57