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

ParamConverterEventSubscriber::autoConfigure()   C

Complexity

Conditions 14
Paths 22

Size

Total Lines 31
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 27.6872

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 31
ccs 10
cts 17
cp 0.5881
rs 5.0864
cc 14
eloc 17
nc 22
nop 3
crap 27.6872

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
    protected $autoConvert;
20
21
    /**
22
     * @var bool
23
     */
24
    private $isParameterTypeSupported;
25
26
    /**
27
     * Constructor.
28
     *
29
     * @param ParamConverterManager $manager     A ParamConverterManager instance
30
     * @param bool                  $autoConvert Auto convert non-configured objects
31
     */
32 6
    public function __construct(ParamConverterManager $manager, $autoConvert = true)
33
    {
34 6
        $this->manager = $manager;
35 6
        $this->autoConvert = $autoConvert;
36 6
        $this->isParameterTypeSupported = method_exists('ReflectionParameter', 'getType');
37 6
    }
38
39
    /**
40
     * Modifies the ParamConverterManager instance.
41
     *
42
     * @param FilterControllerEvent $event A FilterControllerEvent instance
43
     */
44 6
    public function onKernelController(FilterControllerEvent $event)
45
    {
46 6
        $controller = $event->getController();
47 6
        $request = $event->getRequest();
48 6
        $configurations = array();
49
50 6
        if ($configuration = $request->attributes->get('_converters')) {
51
            foreach (is_array($configuration) ? $configuration : array($configuration) as $configuration) {
52
                $configurations[$configuration->getName()] = $configuration;
53
            }
54
        }
55
56 6
        if (is_array($controller)) {
57 6
            $r = new \ReflectionMethod($controller[0], $controller[1]);
58
        } elseif (is_object($controller) && is_callable($controller, '__invoke')) {
59
            $r = new \ReflectionMethod($controller, '__invoke');
60
        } else {
61
            $r = new \ReflectionFunction($controller);
62
        }
63
64
        // automatically apply conversion for non-configured objects
65 6
        if ($this->autoConvert) {
66 6
            $configurations = $this->autoConfigure($r, $request, $configurations);
67
        }
68
69 6
        $this->manager->apply($request, $configurations);
70 6
    }
71
72 6
    private function autoConfigure(\ReflectionFunctionAbstract $r, Request $request, $configurations)
73
    {
74 6
        foreach ($r->getParameters() as $param) {
75 1
            if ($param->getClass() && $param->getClass()->isInstance($request)) {
76
                continue;
77
            }
78
79 1
            $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...
80 1
            $class = $param->getClass();
81 1
            $hasType = $this->isParameterTypeSupported && $param->hasType();
82
83 1
            if ($class || $hasType) {
84
                if (!isset($configurations[$name])) {
85
                    $configuration = new ParamConverter([]);
86
                    $configuration->setName($name);
87
88
                    $configurations[$name] = $configuration;
89
                }
90
91
                if ($class && null === $configurations[$name]->getClass()) {
92
                    $configurations[$name]->setClass($class->getName());
93
                }
94
            }
95
96 1
            if (isset($configurations[$name])) {
97 1
                $configurations[$name]->setIsOptional($param->isOptional() || $param->isDefaultValueAvailable() || $hasType && $param->getType()->allowsNull());
98
            }
99
        }
100
101 6
        return $configurations;
102
    }
103
104
    /**
105
     * @return array
106
     */
107 6
    public static function getSubscribedEvents()
108
    {
109
        return [
110
            KernelEvents::CONTROLLER => [
111
                ['onKernelController', 100],
112 6
            ],
113
        ];
114
    }
115
}
116