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

DisabledToolSubscriber   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 90.91%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A onKernelController() 0 8 4
A __construct() 0 3 1
A getSubscribedEvents() 0 4 1
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