ResponseManager::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 8
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\ApiHttp\Manager;
6
7
use Chubbyphp\ApiHttp\ApiProblem\ApiProblemInterface;
8
use Chubbyphp\Deserialization\DeserializerInterface;
9
use Chubbyphp\Serialization\Normalizer\NormalizerContextInterface;
10
use Chubbyphp\Serialization\SerializerInterface;
11
use Psr\Http\Message\ResponseFactoryInterface;
12
use Psr\Http\Message\ResponseInterface;
13
14
final class ResponseManager implements ResponseManagerInterface
15
{
16
    /**
17
     * @var DeserializerInterface
18
     */
19
    private $deserializer;
20
21
    /**
22
     * @var ResponseFactoryInterface
23
     */
24
    private $responseFactory;
25
26
    /**
27
     * @var SerializerInterface
28
     */
29
    private $serializer;
30
31
    public function __construct(
32
        DeserializerInterface $deserializer,
33
        ResponseFactoryInterface $responseFactory,
34
        SerializerInterface $serializer
35
    ) {
36 7
        $this->deserializer = $deserializer;
37
        $this->responseFactory = $responseFactory;
38
        $this->serializer = $serializer;
39
    }
40
41 7
    /**
42 7
     * @param object $object
43 7
     */
44 7
    public function create(
45
        $object,
46
        string $accept,
47
        int $status = 200,
48
        NormalizerContextInterface $context = null
49
    ): ResponseInterface {
50
        $body = $this->serializer->serialize($object, $accept, $context);
51
52
        $response = $this->responseFactory->createResponse($status)->withHeader('Content-Type', $accept);
53
        $response->getBody()->write($body);
54 2
55
        return $response;
56
    }
57
58
    public function createEmpty(string $accept, int $status = 204): ResponseInterface
59
    {
60 2
        return $this->responseFactory->createResponse($status)->withHeader('Content-Type', $accept);
61
    }
62 2
63 2
    public function createRedirect(string $location, int $status = 307): ResponseInterface
64
    {
65 2
        return $this->responseFactory->createResponse($status)->withHeader('Location', $location);
66
    }
67
68
    /**
69
     * @param NormalizerContextInterface $context
70
     */
71
    public function createFromApiProblem(
72
        ApiProblemInterface $apiProblem,
73
        string $accept,
74 2
        NormalizerContextInterface $context = null
75
    ): ResponseInterface {
76 2
        $status = $apiProblem->getStatus();
77
78
        $response = $this->responseFactory->createResponse($status)
79
            ->withHeader('Content-Type', str_replace('/', '/problem+', $accept))
80
        ;
81
82
        foreach ($apiProblem->getHeaders() as $name => $value) {
83
            $response = $response->withHeader($name, $value);
84
        }
85 2
86
        $body = $this->serializer->serialize($apiProblem, $accept, $context);
87 2
88
        $response->getBody()->write($body);
89
90
        return $response;
91
    }
92
}
93