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

CrudsParamConverter   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 86.36%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
lcom 0
cbo 4
dl 0
loc 48
ccs 19
cts 22
cp 0.8636
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B onCrudRequest() 0 24 6
A getRouter() 0 4 1
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