Completed
Push — master ( d87ed8...99bf40 )
by Artem
10s
created

JsonDecoderListener   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A onKernelRequest() 0 16 4
1
<?php
2
/**
3
 * This file is part of the FreshCommonApiBundle
4
 *
5
 * (c) Artem Genvald <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Fresh\CommonApiBundle\EventListener;
12
13
use Symfony\Component\HttpFoundation\ParameterBag;
14
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
15
16
/**
17
 * JsonDecoderListener.
18
 *
19
 * @author Artem Genvald <[email protected]>
20
 */
21
class JsonDecoderListener
22
{
23
    /**
24
     * @var array $jsonContentTypes JSON content types
25
     */
26
    private $jsonContentTypes = [
27
        'application/json',
28
    ];
29
30
    /**
31
     * On kernel request.
32
     *
33
     * @param GetResponseEvent $event Event
34
     */
35
    public function onKernelRequest(GetResponseEvent $event)
36
    {
37
        $request = $event->getRequest();
38
39
        if ($request->headers->has('Content-Type')) {
40
            $contentType = $request->headers->get('Content-Type');
41
42
            if (in_array($contentType, $this->jsonContentTypes)) {
43
                $data = json_decode($request->getContent(), true);
44
45
                if (is_array($data)) {
46
                    $request->request = new ParameterBag($data);
47
                }
48
            }
49
        }
50
    }
51
}
52