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

DisabledToolSubscriber   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 10
cts 11
cp 0.9091
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A onKernelController() 0 7 3
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\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