Passed
Pull Request — master (#125)
by MusikAnimal
03:53
created

DisabledToolSubscriber::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
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\Container;
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
16
 */
17
class DisabledToolSubscriber implements EventSubscriberInterface
18
{
19
20
    /** @var Container The DI container. */
21
    protected $container;
22
23
    /**
24
     * Save the container for later use.
25
     * @param Container $container The DI container.
26
     */
27 14
    public function __construct(Container $container)
28
    {
29 14
        $this->container = $container;
30 14
    }
31
32
    /**
33
     * Register our interest in the kernel.controller event.
34
     * @return string[]
35
     */
36 1
    public static function getSubscribedEvents()
37
    {
38
        return [
39 1
            KernelEvents::CONTROLLER => 'onKernelController',
40
        ];
41
    }
42
43
    /**
44
     * Check to see if the current tool is enabled.
45
     * @param FilterControllerEvent $event The event.
46
     * @throws NotFoundHttpException If the tool is not enabled.
47
     */
48 14
    public function onKernelController(FilterControllerEvent $event)
49
    {
50 14
        $controller = $event->getController();
51 14
        if (method_exists($controller[0], 'getToolShortname')) {
52 8
            $tool = $controller[0]->getToolShortname();
53 8
            if (!$this->container->getParameter("enable.$tool")) {
54
                throw new NotFoundHttpException('This tool is disabled');
55
            }
56
        }
57 14
    }
58
}
59