Completed
Push — master ( 9fe0d2...0e0f1f )
by Sam
03:08
created

DisabledToolSubscriber::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 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 */
21
    protected $container;
22
23
    /**
24
     * Save the container for later use.
25
     * @param Container $container
26
     */
27
    public function __construct(Container $container)
28
    {
29
        $this->container = $container;
30
    }
31
32
    /**
33
     * Register our interest in the kernel.controller event.
34
     * @return string[]
35
     */
36
    public static function getSubscribedEvents()
37
    {
38
        return [
39
            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
    public function onKernelController(FilterControllerEvent $event)
49
    {
50
        $controller = $event->getController();
51
        if (method_exists($controller[0], 'getToolShortname')) {
52
            $tool = $controller[0]->getToolShortname();
53
            if (!$this->container->getParameter("enable.$tool")) {
54
                throw new NotFoundHttpException('This tool is disabled');
55
            }
56
        }
57
    }
58
}
59