Completed
Push — master ( 946d9b...6e9e2e )
by Dominik
07:10 queued 14s
created

ResponseManager::createRedirect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 2
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\NormalizerContext;
12
use Chubbyphp\Serialization\SerializerInterface;
13
use Psr\Http\Message\ServerRequestInterface as Request;
14
use Psr\Http\Message\ResponseInterface as Response;
15
16
final class ResponseManager implements ResponseManagerInterface
17
{
18
    /**
19
     * @var ResponseFactoryInterface
20
     */
21
    private $responseFactory;
22
23
    /**
24
     * @var DeserializerInterface
25
     */
26
    private $deserializer;
27
28
    /**
29
     * @var SerializerInterface
30
     */
31
    private $serializer;
32
33
    /**
34
     * @param ResponseFactoryInterface $responseFactory
35
     * @param DeserializerInterface    $deserializer
36
     * @param SerializerInterface      $serializer
37
     */
38
    public function __construct(
39
        ResponseFactoryInterface $responseFactory,
40
        DeserializerInterface $deserializer,
41
        SerializerInterface $serializer
42
    ) {
43
        $this->responseFactory = $responseFactory;
44
        $this->deserializer = $deserializer;
45
        $this->serializer = $serializer;
46
    }
47
48
    /**
49
     * @param object                 $object
50
     * @param string                 $accept
51
     * @param int                    $status
52
     * @param NormalizerContext|null $context
53
     *
54
     * @return Response
55
     */
56
    public function create(
57
        $object,
58
        string $accept,
59
        int $status = 200,
60
        NormalizerContext $context = null
61
    ): Response {
62
        $body = $this->serializer->serialize($object, $accept, $context);
63
64
        $response = $this->responseFactory->createResponse($status)->withHeader('Content-Type', $accept);
65
        $response->getBody()->write($body);
66
67
        return $response;
68
    }
69
70
    /**
71
     * @param string $accept
72
     * @param int    $status
73
     *
74
     * @return Response
75
     */
76
    public function createEmpty(string $accept, int $status = 204): Response
77
    {
78
        return $this->responseFactory->createResponse($status)->withHeader('Content-Type', $accept);
79
    }
80
81
    /**
82
     * @param string $location
83
     * @param int    $status
84
     *
85
     * @return Response
86
     */
87
    public function createRedirect(string $location, int $status = 307): Response
88
    {
89
        return $this->responseFactory->createResponse($status)->withAddedHeader('Location', $location);
90
    }
91
92
    /**
93
     * @param ErrorInterface         $error
94
     * @param string                 $accept
95
     * @param int                    $status
96
     * @param NormalizerContext|null $context
97
     *
98
     * @return Response
99
     */
100
    public function createByError(
101
        ErrorInterface $error,
102
        string $accept,
103
        int $status = 400,
104
        NormalizerContext $context = null
105
    ): Response {
106
        return $this->create($error, $accept, $status, $context);
107
    }
108
109
    /**
110
     * @param Request                $request
111
     * @param string                 $accept
112
     * @param string                 $authenticationType
113
     * @param string                 $reason
114
     * @param NormalizerContext|null $context
115
     *
116
     * @return Response
117
     */
118 View Code Duplication
    public function createNotAuthenticated(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
119
        Request $request,
120
        string $accept,
121
        string $authenticationType,
122
        string $reason,
123
        NormalizerContext $context = null
124
    ): Response {
125
        return $this->createByError(new Error(
126
            Error::SCOPE_HEADER,
127
            'not_authenticated',
128
            'missing or invalid authentication token',
129
            'authentication',
130
            [
131
                'type' => $authenticationType,
132
                'value' => $request->getHeaderLine('Authorization'),
133
                'reason' => $reason,
134
            ]
135
        ), $accept, 401, $context);
136
    }
137
138
    /**
139
     * @param string                 $type
140
     * @param array                  $arguments
141
     * @param string                 $accept
142
     * @param NormalizerContext|null $context
143
     *
144
     * @return Response
145
     */
146 View Code Duplication
    public function createNotAuthorized(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
147
        string $type,
148
        array $arguments,
149
        string $accept,
150
        NormalizerContext $context = null
151
    ): Response {
152
        return $this->createByError(new Error(
153
            Error::SCOPE_HEADER,
154
            'permission_denied',
155
            'authenticated client/user is not allowed to perform this action',
156
            $type,
157
            $arguments
158
        ), $accept, 403, $context);
159
    }
160
161
    /**
162
     * @param string                 $type
163
     * @param array                  $arguments
164
     * @param string                 $accept
165
     * @param NormalizerContext|null $context
166
     *
167
     * @return Response
168
     */
169 View Code Duplication
    public function createResourceNotFound(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
170
        string $type,
171
        array $arguments,
172
        string $accept,
173
        NormalizerContext $context = null
174
    ): Response {
175
        return $this->createByError(new Error(
176
            Error::SCOPE_RESOURCE,
177
            'resource_not_found',
178
            'the requested resource cannot be found',
179
            $type,
180
            $arguments
181
        ), $accept, 404, $context);
182
    }
183
184
    /**
185
     * @param Request $request
186
     *
187
     * @return Response
188
     */
189
    public function createAcceptNotSupported(Request $request): Response
190
    {
191
        return $this->responseFactory->createResponse(406)->withHeader('X-Not-Acceptable', sprintf(
192
            'Accept "%s" is not supported, supported are %s',
193
            $request->getHeaderLine('Accept'),
194
            implode(', ', $this->serializer->getContentTypes())
195
        ));
196
    }
197
198
    /**
199
     * @param Request                $request
200
     * @param string                 $accept
201
     * @param array                  $supportedContentTypes
202
     * @param NormalizerContext|null $context
203
     *
204
     * @return Response
205
     */
206 View Code Duplication
    public function createContentTypeNotSupported(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
207
        Request $request,
208
        string $accept,
209
        array $supportedContentTypes,
210
        NormalizerContext $context = null
211
    ): Response {
212
        return $this->createByError(new Error(
213
            Error::SCOPE_HEADER,
214
            'contentype_not_supported',
215
            'the given content type is not supported',
216
            'content-type',
217
            [
218
                'contentType' => $request->getHeaderLine('Content-Type'),
219
                'supportedContentTypes' => $this->deserializer->getContentTypes(),
220
            ]
221
        ), $accept, 415, $context);
222
    }
223
}
224