Passed
Push — 4.0-wip ( 6335ec...2b093e )
by Damien
04:23
created

ViewerEventSubscriber::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace DH\AuditorBundle\Event;
4
5
use DH\Auditor\Auditor;
6
use DH\Auditor\Configuration as AuditorConfiguration;
7
use DH\Auditor\Provider\Doctrine\Configuration as DoctrineProviderConfiguration;
8
use DH\Auditor\Provider\Doctrine\DoctrineProvider;
9
use DH\AuditorBundle\Controller\ViewerController;
10
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
11
use Symfony\Component\HttpKernel\Event\ControllerEvent;
12
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
13
use Symfony\Component\HttpKernel\KernelEvents;
14
15
class ViewerEventSubscriber implements EventSubscriberInterface
16
{
17
    /**
18
     * @var Auditor
19
     */
20
    private $auditor;
21
22
    public function __construct(Auditor $auditor)
23
    {
24
        $this->auditor = $auditor;
25
    }
26
27
    public function onKernelController(ControllerEvent $event): void
28
    {
29
        $controller = $event->getController();
30
31
        // when a controller class defines multiple action methods, the controller
32
        // is returned as [$controllerInstance, 'methodName']
33
        if (\is_array($controller)) {
34
            $controller = $controller[0];
35
        }
36
37
        /** @var AuditorConfiguration $auditorConfiguration */
38
        $auditorConfiguration = $this->auditor->getConfiguration();
39
40
        /** @var DoctrineProviderConfiguration $providerConfiguration */
41
        $providerConfiguration = $this->auditor->getProvider(DoctrineProvider::class)->getConfiguration();
42
43
        $isAuditorEnabled = $auditorConfiguration->isEnabled();
44
        $isViewerEnabled = $providerConfiguration->isViewerEnabled();
45
46
        if ($controller instanceof ViewerController && (!$isAuditorEnabled || !$isViewerEnabled)) {
47
            throw new NotFoundHttpException();
48
        }
49
    }
50
51
    public static function getSubscribedEvents()
52
    {
53
        return [
54
            KernelEvents::CONTROLLER => 'onKernelController',
55
        ];
56
    }
57
}
58