Completed
Push — 8.x-1.x ( dece3c...0d95ba )
by
unknown
28:39
created

ParamConverterEventSubscriber::autoConfigure()   C

Complexity

Conditions 14
Paths 22

Size

Total Lines 31
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 14

Importance

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

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