JsonDecoderListener::onKernelRequest()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 12
rs 9.2
c 1
b 0
f 0
cc 4
eloc 6
nc 3
nop 1
1
<?php
2
/*
3
 * This file is part of the FreshCommonApiBundle
4
 *
5
 * (c) Artem Henvald <[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
declare(strict_types=1);
12
13
namespace Fresh\CommonApiBundle\EventListener;
14
15
use Symfony\Component\HttpFoundation\ParameterBag;
16
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
17
18
/**
19
 * JsonDecoderListener.
20
 *
21
 * @author Artem Henvald <[email protected]>
22
 */
23
class JsonDecoderListener
24
{
25
    /** @var array */
26
    private const JSON_CONTENT_TYPES = [
27
        'application/json',
28
    ];
29
30
    /**
31
     * @param GetResponseEvent $event
32
     */
33
    public function onKernelRequest(GetResponseEvent $event): void
34
    {
35
        $request = $event->getRequest();
36
37
        if ($request->headers->has('Content-Type') && \in_array($request->headers->get('Content-Type'), self::JSON_CONTENT_TYPES, true)) {
38
            $data = \json_decode($request->getContent(), true);
39
40
            if (\is_array($data)) {
41
                $request->request = new ParameterBag($data);
42
            }
43
        }
44
    }
45
}
46