Completed
Push — master ( 1b3a7e...f4d44d )
by Pavel
04:33
created

CrudsParamConverter::onCrudRequest()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 6.1979

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 24
ccs 14
cts 17
cp 0.8235
rs 8.5125
c 1
b 0
f 0
cc 6
eloc 14
nc 6
nop 1
crap 6.1979
1
<?php
2
3
namespace ScayTrase\Api\Cruds\Listener;
4
5
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
6
use Symfony\Component\Routing\RouterInterface;
7
8
final class CrudsParamConverter
9
{
10
    use CrudsRequestCheckerTrait;
11
12
    /** @var  RouterInterface */
13
    private $router;
14
15
    /**
16
     * CrudsParamConverter constructor.
17
     *
18
     * @param RouterInterface $router
19
     */
20 12
    public function __construct(RouterInterface $router)
21
    {
22 12
        $this->router = $router;
23 12
    }
24
25 12
    public function onCrudRequest(GetResponseEvent $event)
26
    {
27 12
        if (!$this->checkRequest($event)) {
28
            return;
29
        }
30
31 12
        $route = $this->getRoute($event);
32 12
        if (!$route->hasOption('cruds_options')) {
33
            return;
34
        }
35
36 12
        $options = (array)$route->getOption('cruds_options');
37 12
        if (!array_key_exists('arguments', $options)) {
38
            return;
39
        }
40
41 12
        $request = $event->getRequest();
42 12
        foreach ((array)$options['arguments'] as $param) {
43 12
            $value = $request->get($param);
44 12
            if (null !== $value) {
45 12
                $request->attributes->set($param, $value);
46 12
            }
47 12
        }
48 12
    }
49
50
    /** {@inheritdoc} */
51 12
    protected function getRouter()
52
    {
53 12
        return $this->router;
54
    }
55
}
56