Passed
Pull Request — master (#376)
by MusikAnimal
10:04 queued 21s
created

DisabledToolSubscriber   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 54.55%

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 39
ccs 6
cts 11
cp 0.5455
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A onKernelController() 0 8 5
A getSubscribedEvents() 0 4 1
1
<?php
2
/**
3
 * This file contains only the DisabledToolSubscriber class.
4
 */
5
6
declare(strict_types = 1);
7
8
namespace AppBundle\EventSubscriber;
9
10
use AppBundle\Controller\XtoolsController;
11
use Symfony\Component\DependencyInjection\ContainerInterface;
12
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
13
use Symfony\Component\HttpKernel\Event\ControllerEvent;
14
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
15
use Symfony\Component\HttpKernel\KernelEvents;
16
17
/**
18
 * A DisabledToolSubscriber checks to see if the current tool is disabled
19
 * and will throw an exception accordingly.
20
 */
21
class DisabledToolSubscriber implements EventSubscriberInterface
22
{
23
24
    /** @var ContainerInterface The DI container. */
25
    protected $container;
26
27
    /**
28
     * Save the container for later use.
29
     * @param ContainerInterface $container The DI container.
30
     */
31 15
    public function __construct(ContainerInterface $container)
32
    {
33 15
        $this->container = $container;
34 15
    }
35
36
    /**
37
     * Register our interest in the kernel.controller event.
38
     * @return string[]
39
     */
40
    public static function getSubscribedEvents(): array
41
    {
42
        return [
43
            KernelEvents::CONTROLLER => 'onKernelController',
44
        ];
45
    }
46
47
    /**
48
     * Check to see if the current tool is enabled.
49
     * @param ControllerEvent $event The event.
50
     * @throws NotFoundHttpException If the tool is not enabled.
51
     */
52 15
    public function onKernelController(ControllerEvent $event): void
53
    {
54 15
        $controller = $event->getController();
55
56 15
        if ($controller instanceof XtoolsController && method_exists($controller, 'getIndexRoute')) {
57
            $tool = $controller[0]->getIndexRoute();
58
            if (!in_array($tool, ['homepage', 'meta', 'Quote']) && !$this->container->getParameter("enable.$tool")) {
59
                throw new NotFoundHttpException('This tool is disabled');
60
            }
61
        }
62 15
    }
63
}
64