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

DisabledToolSubscriber::onKernelController()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 8.125

Importance

Changes 0
Metric Value
cc 5
eloc 5
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 8
ccs 3
cts 6
cp 0.5
crap 8.125
rs 9.6111
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