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

ViewerEventSubscriber   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 40
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribedEvents() 0 4 1
A onKernelController() 0 21 5
A __construct() 0 3 1
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