Passed
Push — master ( 4e10bd...9ef64d )
by MusikAnimal
04:55
created

DisabledToolSubscriber   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 0
cts 17
cp 0
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\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
16
 */
17
class DisabledToolSubscriber implements EventSubscriberInterface
18
{
19
20
    /** @var ContainerInterface The DI container. */
21
    protected $container;
22
23
    /**
24
     * Save the container for later use.
25
     * @param Container $container The DI container.
0 ignored issues
show
Bug introduced by
The type AppBundle\EventSubscriber\Container was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
     */
27
    public function __construct(ContainerInterface $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