Passed
Push — v2 ( 9b853e...868892 )
by Daniel
04:58
created

RequestFormatResolver::getFormat()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
ccs 0
cts 5
cp 0
crap 6
rs 10
c 1
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Component Bundle Project
5
 *
6
 * (c) Daniel West <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentBundle\Serializer;
15
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\HttpFoundation\RequestStack;
18
19
/**
20
 * @author Daniel West <[email protected]>
21
 */
22
final class RequestFormatResolver
23
{
24
    private RequestStack $requestStack;
25
    private string $defaultFormat;
26
27
    public function __construct(RequestStack $requestStack, string $defaultFormat = 'jsonld')
28
    {
29
        $this->requestStack = $requestStack;
30
        $this->defaultFormat = $defaultFormat;
31
    }
32
33
    public function getFormat(): string
34
    {
35
        $request = $this->requestStack->getMasterRequest();
36
        if (!$request) {
37
            return $this->defaultFormat;
38
        }
39
40
        return $this->getFormatFromRequest($request);
41
    }
42
43
    public function getFormatFromRequest(Request $request): string
44
    {
45
        return $request->getRequestFormat($this->defaultFormat);
1 ignored issue
show
Bug Best Practice introduced by
The expression return $request->getRequ...t($this->defaultFormat) could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
46
        // return $request->attributes->get('_format') ?: $request->getFormat($request->headers->get('CONTENT_TYPE')) ?: $this->defaultFormat;
47
    }
48
}
49