Completed
Push — master ( f6a501...753120 )
by Dominik
02:55
created

ResponseManager::createFromError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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