Passed
Push — master ( 791acf...f6a501 )
by Dominik
02:06
created

ResponseManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
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 6
    public function __construct(
32
        ResponseFactoryInterface $responseFactory,
33
        SerializerInterface $serializer
34
    ) {
35 6
        $this->responseFactory = $responseFactory;
36 6
        $this->serializer = $serializer;
37 6
    }
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 2
    public function create(
48
        $object,
49
        string $accept,
50
        int $status = 200,
51
        NormalizerContextInterface $context = null
52
    ): Response {
53 2
        $body = $this->serializer->serialize($object, $accept, $context);
54
55 2
        $response = $this->responseFactory->createResponse($status)->withHeader('Content-Type', $accept);
56 2
        $response->getBody()->write($body);
57
58 2
        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
    public function createByError(
92
        ErrorInterface $error,
93
        string $accept,
94
        int $status = 400,
95
        NormalizerContextInterface $context = null
96
    ): Response {
97
        return $this->create($error, $accept, $status, $context);
98
    }
99
100
    /**
101
     * @param Request                         $request
102
     * @param string                          $accept
103
     * @param string                          $authenticationType
104
     * @param string                          $reason
105
     * @param NormalizerContextInterface|null $context
106
     *
107
     * @return Response
108
     */
109 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...
110
        Request $request,
111
        string $accept,
112
        string $authenticationType,
113
        string $reason,
114
        NormalizerContextInterface $context = null
115
    ): Response {
116
        return $this->createByError(new Error(
117
            Error::SCOPE_HEADER,
118
            'not_authenticated',
119
            'missing or invalid authentication token',
120
            'authentication',
121
            [
122
                'type' => $authenticationType,
123
                'value' => $request->getHeaderLine('Authorization'),
124
                'reason' => $reason,
125
            ]
126
        ), $accept, 401, $context);
127
    }
128
129
    /**
130
     * @param string                          $type
131
     * @param array                           $arguments
132
     * @param string                          $accept
133
     * @param NormalizerContextInterface|null $context
134
     *
135
     * @return Response
136
     */
137 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...
138
        string $type,
139
        array $arguments,
140
        string $accept,
141
        NormalizerContextInterface $context = null
142
    ): Response {
143
        return $this->createByError(new Error(
144
            Error::SCOPE_HEADER,
145
            'permission_denied',
146
            'authenticated client/user is not allowed to perform this action',
147
            $type,
148
            $arguments
149
        ), $accept, 403, $context);
150
    }
151
152
    /**
153
     * @param string                          $type
154
     * @param array                           $arguments
155
     * @param string                          $accept
156
     * @param NormalizerContextInterface|null $context
157
     *
158
     * @return Response
159
     */
160 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...
161
        string $type,
162
        array $arguments,
163
        string $accept,
164
        NormalizerContextInterface $context = null
165
    ): Response {
166
        return $this->createByError(new Error(
167
            Error::SCOPE_RESOURCE,
168
            'resource_not_found',
169
            'the requested resource cannot be found',
170
            $type,
171
            $arguments
172
        ), $accept, 404, $context);
173
    }
174
175
    /**
176
     * @param Request $request
177
     *
178
     * @return Response
179
     */
180
    public function createAcceptNotSupported(Request $request): Response
181
    {
182
        return $this->responseFactory->createResponse(406)->withHeader('X-Not-Acceptable', sprintf(
183
            'Accept "%s" is not supported, supported are %s',
184
            $request->getHeaderLine('Accept'),
185
            implode(', ', $this->serializer->getContentTypes())
186
        ));
187
    }
188
189
    /**
190
     * @param Request                         $request
191
     * @param string                          $accept
192
     * @param array                           $supportedContentTypes
193
     * @param NormalizerContextInterface|null $context
194
     *
195
     * @return Response
196
     */
197 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...
198
        Request $request,
199
        string $accept,
200
        array $supportedContentTypes,
201
        NormalizerContextInterface $context = null
202
    ): Response {
203
        return $this->createByError(new Error(
204
            Error::SCOPE_HEADER,
205
            'contentype_not_supported',
206
            'the given content type is not supported',
207
            'content-type',
208
            [
209
                'contentType' => $request->getHeaderLine('Content-Type'),
210
                'supportedContentTypes' => $supportedContentTypes,
211
            ]
212
        ), $accept, 415, $context);
213
    }
214
}
215