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

AbstractAction   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
dl 0
loc 34
ccs 0
cts 18
cp 0
rs 10
c 1
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getResponse() 0 15 2
A getFormat() 0 3 1
A __construct() 0 7 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\Action;
15
16
use Silverback\ApiComponentBundle\Exception\InvalidParameterException;
17
use Silverback\ApiComponentBundle\Serializer\RequestFormatResolver;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpFoundation\Response;
20
use Symfony\Component\Serializer\Encoder\DecoderInterface;
21
use Symfony\Component\Serializer\SerializerInterface;
22
23
/**
24
 * @author Daniel West <[email protected]>
25
 */
26
class AbstractAction
27
{
28
    protected DecoderInterface $serializer;
29
    protected RequestFormatResolver $requestFormatResolver;
30
31
    public function __construct(SerializerInterface $serializer, RequestFormatResolver $requestFormatResolver)
32
    {
33
        if (!$serializer instanceof DecoderInterface) {
34
            throw new InvalidParameterException(sprintf('The serializer injected into %s should implement %s', __CLASS__, DecoderInterface::class));
35
        }
36
        $this->serializer = $serializer;
37
        $this->requestFormatResolver = $requestFormatResolver;
38
    }
39
40
    protected function getFormat(Request $request): string
41
    {
42
        return $this->requestFormatResolver->getFormatFromRequest($request);
43
    }
44
45
    protected function getResponse(Request $request, $response = null, ?int $status = null): Response
46
    {
47
        $headers = [
48
            'Content-Type' => sprintf('%s; charset=utf-8', $format = $this->getFormat($request)),
49
            'Vary' => 'Accept',
50
            'X-Content-Type-Options' => 'nosniff',
51
            'X-Frame-Options' => 'deny',
52
        ];
53
        if (!\is_string($response)) {
54
            $response = $this->serializer->serialize($response, $format, []);
0 ignored issues
show
Bug introduced by
The method serialize() does not exist on Symfony\Component\Serial...ncoder\DecoderInterface. It seems like you code against a sub-type of Symfony\Component\Serial...ncoder\DecoderInterface such as Symfony\Component\Serializer\Serializer. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

54
            /** @scrutinizer ignore-call */ 
55
            $response = $this->serializer->serialize($response, $format, []);
Loading history...
55
        }
56
        new Response(
57
            $response,
58
            $status ?? Response::HTTP_OK,
59
            $headers
60
        );
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return Symfony\Component\HttpFoundation\Response. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
61
    }
62
}
63