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

RequestFormatResolver   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getFormat() 0 8 2
A getFormatFromRequest() 0 3 1
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