RequestTransformer   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 30
ccs 9
cts 9
cp 1
rs 10
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A acceptJsonBody() 0 5 3
A transform() 0 4 1
A setRequestFormat() 0 10 2
A __construct() 0 2 1
1
<?php
2
3
namespace MediaMonks\RestApi\Request;
4
5
use MediaMonks\RestApi\Serializer\SerializerInterface;
6
use Symfony\Component\HttpFoundation\Request;
7
8
class RequestTransformer implements RequestTransformerInterface
9
{
10
    public function __construct(protected SerializerInterface $serializer)
11
    {
12
    }
13
14
    public function transform(Request $request)
15
    {
16
        $this->acceptJsonBody($request);
17
        $this->setRequestFormat($request);
18 6
    }
19
20 6
    protected function acceptJsonBody(Request $request)
21 6
    {
22
        if (str_starts_with($request->headers->get('Content-Type', ''), 'application/json')) {
23
            $data = json_decode($request->getContent(), true);
24
            $request->request->replace(is_array($data) ? $data : []);
25
        }
26 4
    }
27
28 4
    protected function setRequestFormat(Request $request)
29 4
    {
30 4
        $default = Format::getDefault();
31
        $format = $request->getRequestFormat($request->query->get('_format', $default));
32
33
        if (!in_array($format, $this->serializer->getSupportedFormats())) {
34
            $format = $this->serializer->getDefaultFormat();
35 4
        }
36
37 4
        $request->setRequestFormat($format);
38 2
    }
39
}
40