Test Failed
Push — master ( 25b8d1...f70cb8 )
by Pavel
03:49
created

CrudsParamConverter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 0
cbo 4
dl 0
loc 47
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A onCrudRequest() 0 14 4
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
    const PARAMETER_WHITE_LIST = [
13
        'identifier',
14
        'data',
15
        'criteria',
16
        'order',
17
        'limit',
18
        'offset',
19
    ];
20
21
    /** @var  RouterInterface */
22
    private $router;
23
24
    /**
25
     * CrudsParamConverter constructor.
26
     *
27
     * @param RouterInterface $router
28
     */
29
    public function __construct(RouterInterface $router)
30
    {
31
        $this->router = $router;
32
    }
33
34
    public function onCrudRequest(GetResponseEvent $event)
35
    {
36
        if (!$this->checkRequest($event)) {
37
            return;
38
        }
39
40
        $request = $event->getRequest();
41
        foreach (self::PARAMETER_WHITE_LIST as $param) {
42
            $value = $request->get($param);
43
            if (null !== $value) {
44
                $request->attributes->set($param, $value);
45
            }
46
        }
47
    }
48
49
    /** {@inheritdoc} */
50
    protected function getRouter()
51
    {
52
        return $this->router;
53
    }
54
}
55