Passed
Push — feature/unit-tests ( 785607...45ce75 )
by Daniel
05:06
created

SerializeFormatResolver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
cc 1
nc 1
nop 2
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
    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(null) ?: $request->getContentType() ?: $this->defaultFormat;
46
    }
47
}
48