Passed
Push — feature/unit-tests ( 9a4722...f5904a )
by Daniel
05:28
created

SerializeFormatResolver::getFormat()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
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 SerializeFormatResolver
23
{
24
    private RequestStack $requestStack;
25
    private string $defaultFormat;
26
27 6
    public function __construct(RequestStack $requestStack, string $defaultFormat = 'jsonld')
28
    {
29 6
        $this->requestStack = $requestStack;
30 6
        $this->defaultFormat = $defaultFormat;
31 6
    }
32
33 2
    public function getFormat(): string
34
    {
35 2
        $request = $this->requestStack->getMasterRequest();
36 2
        if (!$request) {
37 1
            return $this->defaultFormat;
38
        }
39
40 1
        return $this->getFormatFromRequest($request);
41
    }
42
43 5
    public function getFormatFromRequest(Request $request): string
44
    {
45 5
        return $request->getRequestFormat(null) ?: $request->getContentType() ?: $this->defaultFormat;
46
    }
47
}
48