Passed
Branch master (a7861d)
by Dominik
03:10
created

ResponseManager   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 68
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 68
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
A createResponse() 0 20 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 3
    public function __construct(
42
        RequestManagerInterface $requestManager,
43
        ResponseFactoryInterface $responseFactory,
44
        SerializerInterface $serializer,
45
        TransformerInterface $transformer
46
    ) {
47 3
        $this->requestManager = $requestManager;
48 3
        $this->responseFactory = $responseFactory;
49 3
        $this->serializer = $serializer;
50 3
        $this->transformer = $transformer;
51 3
    }
52
53
    /**
54
     * @param Request $request
55
     * @param int     $code
56
     * @param object  $object
57
     *
58
     * @return Response
59
     */
60 3
    public function createResponse(Request $request, int $code = 200, $object = null): Response
61
    {
62 3
        $response = $this->responseFactory->createResponse($code);
63
64 3
        if (200 === $code && null === $object) {
65 1
            return $response->withStatus(204);
66
        }
67
68 2
        if (null === $accept = $this->requestManager->getAccept($request)) {
69 1
            return $response->withStatus(406);
70
        }
71
72 1
        $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 60 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...
73
74
        /** @var Response $response */
75 1
        $response = $response->withStatus($code)->withHeader('Content-Type', $accept);
76 1
        $response->getBody()->write($body);
77
78 1
        return $response;
79
    }
80
}
81