Completed
Push — 8.x-1.x ( 082f3b...0421f5 )
by
unknown
28:58 queued 09:33
created

ControllerEventSubscriber::getConfigurations()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5.025

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 17
ccs 9
cts 10
cp 0.9
rs 8.8571
cc 5
eloc 11
nc 5
nop 1
crap 5.025
1
<?php
2
3
namespace Drupal\controller_annotations\EventSubscriber;
4
5
use Doctrine\Common\Annotations\Reader;
6
use Doctrine\Common\Util\ClassUtils;
7
use Drupal\controller_annotations\Configuration\ConfigurationInterface;
8
use Drupal\controller_annotations\Configuration\ConfigurationLoader;
9
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
10
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
11
use Symfony\Component\HttpKernel\KernelEvents;
12
13
class ControllerEventSubscriber implements EventSubscriberInterface
14
{
15
    /**
16
     * @var Reader
17
     */
18
    protected $reader;
19
20
    /**
21
     * Constructor.
22
     *
23
     * @param Reader $reader An Reader instance
24
     */
25 7
    public function __construct(Reader $reader)
26
    {
27 7
        $this->reader = $reader;
28
29 7
        ConfigurationLoader::load();
30 7
    }
31
32
    /**
33
     * Modifies the Request object to apply configuration information found in
34
     * controllers annotations like the template to render or HTTP caching
35
     * configuration.
36
     *
37
     * @param FilterControllerEvent $event A FilterControllerEvent instance
38
     */
39 7
    public function onKernelController(FilterControllerEvent $event)
40
    {
41 7
        $controller = $event->getController();
42
43 7
        if (!is_array($controller) && method_exists($controller, '__invoke')) {
44
            $controller = array($controller, '__invoke');
45
        }
46
47 7
        if (!is_array($controller)) {
48 1
            return;
49
        }
50
51 6
        $className = class_exists('Doctrine\Common\Util\ClassUtils') ? ClassUtils::getClass($controller[0]) : get_class($controller[0]);
52 6
        $object = new \ReflectionClass($className);
53 6
        $method = $object->getMethod($controller[1]);
54
55 6
        $classConfigurations = $this->getConfigurations($this->reader->getClassAnnotations($object));
56 6
        $methodConfigurations = $this->getConfigurations($this->reader->getMethodAnnotations($method));
57
58 6
        $configurations = array();
59 6
        foreach (array_merge(array_keys($classConfigurations), array_keys($methodConfigurations)) as $key) {
60 6
            if (!array_key_exists($key, $classConfigurations)) {
61 6
                $configurations[$key] = $methodConfigurations[$key];
62 2
            } elseif (!array_key_exists($key, $methodConfigurations)) {
63 2
                $configurations[$key] = $classConfigurations[$key];
64
            } else {
65
                if (is_array($classConfigurations[$key])) {
66
                    if (!is_array($methodConfigurations[$key])) {
67
                        throw new \UnexpectedValueException('Configurations should both be an array or both not be an array');
68
                    }
69
                    $configurations[$key] = array_merge($classConfigurations[$key], $methodConfigurations[$key]);
70
                } else {
71
                    // method configuration overrides class configuration
72 6
                    $configurations[$key] = $methodConfigurations[$key];
73
                }
74
            }
75
        }
76
77 6
        $request = $event->getRequest();
78 6
        foreach ($configurations as $key => $attributes) {
79 6
            $request->attributes->set($key, $attributes);
80
        }
81 6
    }
82
83 6
    protected function getConfigurations(array $annotations)
84
    {
85 6
        $configurations = [];
86 6
        foreach ($annotations as $configuration) {
87 6
            if ($configuration instanceof ConfigurationInterface) {
88 6
                if ($configuration->allowArray()) {
89
                    $configurations['_'.$configuration->getAliasName()][] = $configuration;
90 6
                } elseif (!isset($configurations['_'.$configuration->getAliasName()])) {
91 6
                    $configurations['_'.$configuration->getAliasName()] = $configuration;
92
                } else {
93 6
                    throw new \LogicException(sprintf('Multiple "%s" annotations are not allowed.', $configuration->getAliasName()));
94
                }
95
            }
96
        }
97
98 6
        return $configurations;
99
    }
100
101
    /**
102
     * @return array
103
     */
104 6
    public static function getSubscribedEvents()
105
    {
106
        return [
107
            KernelEvents::CONTROLLER => [
108
                ['onKernelController', 100],
109
            ]
110 6
        ];
111
    }
112
}
113