Passed
Push — master ( cb5fa9...d1aa36 )
by Dominik
01:47
created

ResponseManager   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 6
dl 0
loc 73
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
B createResponse() 0 24 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\ApiHttp\Manager;
6
7
use Chubbyphp\ApiHttp\Factory\ResponseFactoryInterface;
8
use Chubbyphp\Serialization\SerializerInterface;
9
use Chubbyphp\Serialization\TransformerInterface;
10
use Psr\Http\Message\ServerRequestInterface as Request;
11
use Psr\Http\Message\ResponseInterface as Response;
12
13
final class ResponseManager implements ResponseManagerInterface
14
{
15
    /**
16
     * @var RequestManagerInterface
17
     */
18
    private $requestManager;
19
20
    /**
21
     * @var ResponseFactoryInterface
22
     */
23
    private $responseFactory;
24
25
    /**
26
     * @var SerializerInterface
27
     */
28
    private $serializer;
29
30
    /**
31
     * @var TransformerInterface
32
     */
33
    private $transformer;
34
35
    /**
36
     * @param RequestManagerInterface  $requestManager
37
     * @param ResponseFactoryInterface $responseFactory
38
     * @param SerializerInterface      $serializer
39
     * @param TransformerInterface     $transformer
40
     */
41 4
    public function __construct(
42
        RequestManagerInterface $requestManager,
43
        ResponseFactoryInterface $responseFactory,
44
        SerializerInterface $serializer,
45
        TransformerInterface $transformer
46
    ) {
47 4
        $this->requestManager = $requestManager;
48 4
        $this->responseFactory = $responseFactory;
49 4
        $this->serializer = $serializer;
50 4
        $this->transformer = $transformer;
51 4
    }
52
53
    /**
54
     * @param Request     $request
55
     * @param int         $code
56
     * @param object      $object
57
     * @param string|null $defaultAccept
58
     *
59
     * @return Response
60
     */
61 4
    public function createResponse(
62
        Request $request,
63
        int $code = 200,
64
        $object = null,
65
        string $defaultAccept = null
66
    ): Response {
67 4
        $response = $this->responseFactory->createResponse($code);
68
69 4
        if (200 === $code && null === $object) {
70 1
            return $response->withStatus(204);
71
        }
72
73 3
        if (null === $accept = $this->requestManager->getAccept($request, $defaultAccept)) {
74 1
            return $response->withStatus(406);
75
        }
76
77 2
        $body = $this->transformer->transform($this->serializer->serialize($request, $object), $accept);
0 ignored issues
show
Bug introduced by
It seems like $object defined by parameter $object on line 64 can also be of type null; however, Chubbyphp\Serialization\...rInterface::serialize() does only seem to accept object, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
78
79
        /** @var Response $response */
80 2
        $response = $response->withStatus($code)->withHeader('Content-Type', $accept);
81 2
        $response->getBody()->write($body);
82
83 2
        return $response;
84
    }
85
}
86