ParamConverterListener   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 0 Features 2
Metric Value
wmc 11
c 5
b 0
f 2
lcom 1
cbo 4
dl 0
loc 66
ccs 31
cts 31
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A addParamConverter() 0 4 1
A apply() 0 14 3
B onKernelController() 0 23 6
1
<?php
2
3
namespace Zenstruck\ControllerUtil\EventListener;
4
5
use Symfony\Component\HttpFoundation\Request;
6
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
7
use Zenstruck\ControllerUtil\ParamConverter\ParamConverter;
8
9
/**
10
 * Convert the request parameters into objects when type-hinted.
11
 *
12
 * This replicates the SensioFrameworkExtraBundle behavior but keeps it simple
13
 * and without a dependency to allow usage outside Symfony Framework apps
14
 * (Silex, ..).
15
 *
16
 * @author Benjamin Eberlei <[email protected]>
17
 * @author Kevin Bond <[email protected]>
18
 *
19
 * @see https://github.com/QafooLabs/QafooLabsNoFrameworkBundle
20
 * @see https://github.com/sensiolabs/SensioFrameworkExtraBundle
21
 */
22
class ParamConverterListener
23
{
24
    private $paramConverters;
25
26
    /**
27
     * @param ParamConverter[] $paramConverters
28
     */
29 8
    public function __construct(array $paramConverters = array())
30
    {
31 8
        $this->paramConverters = $paramConverters;
32 8
    }
33
34
    /**
35
     * @param ParamConverter $paramConverter
36
     */
37 8
    public function addParamConverter(ParamConverter $paramConverter)
38
    {
39 8
        $this->paramConverters[] = $paramConverter;
40 8
    }
41
42
    /**
43
     * @param FilterControllerEvent $event
44
     */
45 8
    public function onKernelController(FilterControllerEvent $event)
46
    {
47 8
        $controller = $event->getController();
48 8
        $request = $event->getRequest();
49
50 8
        if (is_array($controller)) {
51 2
            $reflection = new \ReflectionMethod($controller[0], $controller[1]);
52 8
        } elseif (is_string($controller)) {
53 2
            $reflection = new \ReflectionFunction($controller);
54 2
        } else {
55
            // use __invoke
56 4
            $objReflector = new \ReflectionObject($controller);
57 4
            $reflection = $objReflector->getMethod('__invoke');
58
        }
59
60 8
        foreach ($reflection->getParameters() as $param) {
61 8
            if (!$param->getClass() || $param->getClass()->isInstance($request)) {
62 8
                continue;
63
            }
64
65 8
            $this->apply($request, $param);
66 8
        }
67 8
    }
68
69
    /**
70
     * @param Request              $request
71
     * @param \ReflectionParameter $param
72
     */
73 8
    private function apply(Request $request, \ReflectionParameter $param)
74
    {
75 8
        $class = $param->getClass()->getName();
76
77 8
        foreach ($this->paramConverters as $paramConverter) {
78 8
            if (!$paramConverter->supports($class)) {
79 4
                continue;
80
            }
81
82 4
            $request->attributes->set($param->getName(), $paramConverter->getObject($request));
83
84 4
            break;
85 8
        }
86 8
    }
87
}
88