Completed
Push — master ( 753120...2276fc )
by Dominik
03:23
created

ResponseManager::createResourceNotFound()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 13
nc 1
nop 4
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\ApiHttp\Manager;
6
7
use Chubbyphp\ApiHttp\Error\Error;
8
use Chubbyphp\ApiHttp\Error\ErrorInterface;
9
use Chubbyphp\ApiHttp\Factory\ResponseFactoryInterface;
10
use Chubbyphp\Deserialization\DeserializerInterface;
11
use Chubbyphp\Serialization\Normalizer\NormalizerContextInterface;
12
use Chubbyphp\Serialization\SerializerInterface;
13
use Psr\Http\Message\ResponseInterface as Response;
14
15
final class ResponseManager implements ResponseManagerInterface
16
{
17
    /**
18
     * @var DeserializerInterface
19
     */
20
    private $deserializer;
21
22
    /**
23
     * @var ResponseFactoryInterface
24
     */
25
    private $responseFactory;
26
27
    /**
28
     * @var SerializerInterface
29
     */
30
    private $serializer;
31
32
    /**
33
     * @param DeserializerInterface    $deserializer
34
     * @param ResponseFactoryInterface $responseFactory
35
     * @param SerializerInterface      $serializer
36
     */
37 14
    public function __construct(
38
        DeserializerInterface $deserializer,
39
        ResponseFactoryInterface $responseFactory,
40
        SerializerInterface $serializer
41
    ) {
42 14
        $this->deserializer = $deserializer;
43 14
        $this->responseFactory = $responseFactory;
44 14
        $this->serializer = $serializer;
45 14
    }
46
47
    /**
48
     * @param object                          $object
49
     * @param string                          $accept
50
     * @param int                             $status
51
     * @param NormalizerContextInterface|null $context
52
     *
53
     * @return Response
54
     */
55 10
    public function create(
56
        $object,
57
        string $accept,
58
        int $status = 200,
59
        NormalizerContextInterface $context = null
60
    ): Response {
61 10
        $body = $this->serializer->serialize($object, $accept, $context);
62
63 10
        $response = $this->responseFactory->createResponse($status)->withHeader('Content-Type', $accept);
64 10
        $response->getBody()->write($body);
65
66 10
        return $response;
67
    }
68
69
    /**
70
     * @param string $accept
71
     * @param int    $status
72
     *
73
     * @return Response
74
     */
75 2
    public function createEmpty(string $accept, int $status = 204): Response
76
    {
77 2
        return $this->responseFactory->createResponse($status)->withHeader('Content-Type', $accept);
78
    }
79
80
    /**
81
     * @param string $location
82
     * @param int    $status
83
     *
84
     * @return Response
85
     */
86 2
    public function createRedirect(string $location, int $status = 307): Response
87
    {
88 2
        return $this->responseFactory->createResponse($status)->withHeader('Location', $location);
89
    }
90
91
    /**
92
     * @param ErrorInterface                  $error
93
     * @param string                          $accept
94
     * @param int                             $status
95
     * @param NormalizerContextInterface|null $context
96
     *
97
     * @return Response
98
     */
99 8
    public function createFromError(
100
        ErrorInterface $error,
101
        string $accept,
102
        int $status = 400,
103
        NormalizerContextInterface $context = null
104
    ): Response {
105 8
        return $this->create($error, $accept, $status, $context);
106
    }
107
108
    /**
109
     * @param string                          $accept
110
     * @param NormalizerContextInterface|null $context
111
     *
112
     * @return Response
113
     */
114 2
    public function createNotAuthenticated(string $accept, NormalizerContextInterface $context = null): Response
115
    {
116 2
        return $this->createFromError(new Error(
117 2
            Error::SCOPE_HEADER,
118 2
            'not_authenticated',
119 2
            'missing or invalid authentication token to perform the request',
120 2
            'authentication'
121 2
        ), $accept, 401, $context);
122
    }
123
124
    /**
125
     * @param string                          $accept
126
     * @param NormalizerContextInterface|null $context
127
     *
128
     * @return Response
129
     */
130 2
    public function createNotAuthorized(string $accept, NormalizerContextInterface $context = null): Response
131
    {
132 2
        return $this->createFromError(new Error(
133 2
            Error::SCOPE_HEADER,
134 2
            'permission_denied',
135 2
            'missing authorization to perform request',
136 2
            'authorization'
137 2
        ), $accept, 403, $context);
138
    }
139
140
    /**
141
     * @param string                          $reference
142
     * @param array                           $arguments
143
     * @param string                          $accept
144
     * @param NormalizerContextInterface|null $context
145
     *
146
     * @return Response
147
     */
148 2
    public function createResourceNotFound(
149
        string $reference,
150
        array $arguments,
151
        string $accept,
152
        NormalizerContextInterface $context = null
153
    ): Response {
154 2
        return $this->createFromError(new Error(
155 2
            Error::SCOPE_RESOURCE,
156 2
            'resource_not_found',
157 2
            'the requested resource cannot be found',
158 2
            $reference,
159 2
            $arguments
160 2
        ), $accept, 404, $context);
161
    }
162
163
    /**
164
     * @param string $accept
165
     *
166
     * @return Response
167
     */
168
    public function createAcceptNotSupported(string $accept): Response
169
    {
170
        return $this->responseFactory->createResponse(406)->withHeader('X-Not-Acceptable', sprintf(
171
            'Accept "%s" is not supported, supported are %s',
172
            $accept,
173
            implode(', ', $this->serializer->getContentTypes())
174
        ));
175
    }
176
177
    /**
178
     * @param string                          $contentType
179
     * @param string                          $accept
180
     * @param NormalizerContextInterface|null $context
181
     *
182
     * @return Response
183
     */
184
    public function createContentTypeNotSupported(
185
        string $contentType,
186
        string $accept,
187
        NormalizerContextInterface $context = null
188
    ): Response {
189
        return $this->createFromError(new Error(
190
            Error::SCOPE_HEADER,
191
            'contentype_not_supported',
192
            'the given content type is not supported',
193
            'content-type',
194
            [
195
                'contentType' => $contentType,
196
                'supportedContentTypes' => $this->deserializer->getContentTypes(),
197
            ]
198
        ), $accept, 415, $context);
199
    }
200
}
201