Completed
Push — 8.x-1.x ( 35c1b8...518784 )
by
unknown
24:14
created

ParamConverterEventSubscriber::resolveMethod()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4.074

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
ccs 5
cts 6
cp 0.8333
rs 9.2
cc 4
eloc 6
nc 3
nop 1
crap 4.074
1
<?php
2
3
namespace Drupal\controller_annotations\EventSubscriber;
4
5
use Drupal\controller_annotations\Configuration\ParamConverter;
6
use Drupal\controller_annotations\Request\ParamConverter\ParamConverterManager;
7
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
10
use Symfony\Component\HttpKernel\KernelEvents;
11
12
class ParamConverterEventSubscriber implements EventSubscriberInterface
13
{
14
    /**
15
     * @var ParamConverterManager
16
     */
17
    protected $manager;
18
19
    /**
20
     * @var bool
21
     */
22
    protected $autoConvert;
23
24
    /**
25
     * @var bool
26
     */
27
    private $isParameterTypeSupported;
28
29
    /**
30
     * @param ParamConverterManager $manager     A ParamConverterManager instance
31
     * @param bool                  $autoConvert Auto convert non-configured objects
32
     */
33 14
    public function __construct(ParamConverterManager $manager, $autoConvert = true)
34
    {
35 14
        $this->manager = $manager;
36 14
        $this->autoConvert = $autoConvert;
37 14
        $this->isParameterTypeSupported = method_exists('ReflectionParameter', 'getType');
38 14
    }
39
40
    /**
41
     * Modifies the ParamConverterManager instance.
42
     *
43
     * @param FilterControllerEvent $event A FilterControllerEvent instance
44
     */
45 14
    public function onKernelController(FilterControllerEvent $event)
46
    {
47 14
        $controller = $event->getController();
48 14
        $request = $event->getRequest();
49 14
        $configurations = [];
50
51 14
        if ($configuration = $request->attributes->get('_converters')) {
52 2
            foreach (is_array($configuration) ? $configuration : [$configuration] as $configuration) {
53 2
                $configurations[$configuration->getName()] = $configuration;
54
            }
55
        }
56
57
        // automatically apply conversion for non-configured objects
58 14
        if ($this->autoConvert) {
59 11
            $configurations = $this->autoConfigure($this->resolveMethod($controller), $request, $configurations);
60
        }
61
62 14
        $this->manager->apply($request, $configurations);
63 14
    }
64
65
    /**
66
     * @param $controller
67
     *
68
     * @return \ReflectionFunction|\ReflectionMethod
69
     */
70 11
    protected function resolveMethod($controller)
71
    {
72 11
        if (is_array($controller)) {
73 9
            return new \ReflectionMethod($controller[0], $controller[1]);
74
        }
75 2
        if (is_object($controller) && is_callable($controller, '__invoke')) {
76 2
            return new \ReflectionMethod($controller, '__invoke');
77
        }
78
79
        return new \ReflectionFunction($controller);
80
    }
81
82
    /**
83
     * @param \ReflectionFunctionAbstract $r
84
     * @param \Symfony\Component\HttpFoundation\Request $request
85
     * @param array $configurations
86
     *
87
     * @return array
88
     */
89 11
    private function autoConfigure(\ReflectionFunctionAbstract $r, Request $request, $configurations)
90
    {
91 11
        foreach ($r->getParameters() as $param) {
92 6
            if ($param->getClass() && $param->getClass()->isInstance($request)) {
93 2
                continue;
94
            }
95
96 4
            $name = $param->getName();
0 ignored issues
show
Bug introduced by
Consider using $param->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
97 4
            $class = $param->getClass();
98 4
            $hasType = $this->isParameterTypeSupported && $param->hasType();
99
100 4
            if ($class || $hasType) {
101 3
                if (!isset($configurations[$name])) {
102 3
                    $configuration = new ParamConverter([]);
103 3
                    $configuration->setName($name);
104
105 3
                    $configurations[$name] = $configuration;
106
                }
107
108 3
                if ($class && null === $configurations[$name]->getClass()) {
109 3
                    $configurations[$name]->setClass($class->getName());
110
                }
111
            }
112
113 4
            if (isset($configurations[$name])) {
114 4
                $configurations[$name]->setIsOptional($param->isOptional() || $param->isDefaultValueAvailable() || $hasType && $param->getType()->allowsNull());
115
            }
116
        }
117
118 11
        return $configurations;
119
    }
120
121
    /**
122
     * @return array
123
     */
124 7
    public static function getSubscribedEvents()
125
    {
126
        return [
127
            KernelEvents::CONTROLLER => [
128
                ['onKernelController', 100],
129 7
            ],
130
        ];
131
    }
132
}
133