Passed
Push — master ( e6ce91...429907 )
by MusikAnimal
04:46
created

DisabledToolSubscriber::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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