Completed
Push — master ( c66aa1...b705d2 )
by Dominik
04:19
created

createAcceptNotSupportedResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 0
cp 0
cc 1
eloc 7
nc 1
nop 1
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\Factory\ResponseFactoryInterface;
9
use Chubbyphp\Serialization\SerializerInterface;
10
use Chubbyphp\Serialization\TransformerInterface;
11
use Psr\Http\Message\ServerRequestInterface as Request;
12
use Psr\Http\Message\ResponseInterface as Response;
13
14
final class ResponseManager implements ResponseManagerInterface
15
{
16
    /**
17
     * @var RequestManagerInterface
18
     */
19
    private $requestManager;
20
21
    /**
22
     * @var ResponseFactoryInterface
23
     */
24
    private $responseFactory;
25
26
    /**
27
     * @var SerializerInterface
28
     */
29
    private $serializer;
30
31
    /**
32
     * @var TransformerInterface
33
     */
34
    private $transformer;
35
36
    /**
37
     * @param RequestManagerInterface  $requestManager
38
     * @param ResponseFactoryInterface $responseFactory
39
     * @param SerializerInterface      $serializer
40
     * @param TransformerInterface     $transformer
41 5
     */
42
    public function __construct(
43
        RequestManagerInterface $requestManager,
44
        ResponseFactoryInterface $responseFactory,
45
        SerializerInterface $serializer,
46
        TransformerInterface $transformer
47 5
    ) {
48 5
        $this->requestManager = $requestManager;
49 5
        $this->responseFactory = $responseFactory;
50 5
        $this->serializer = $serializer;
51 5
        $this->transformer = $transformer;
52
    }
53
54
    /**
55
     * @param Request $request
56
     * @param int     $code
57
     * @param string  $accept
58
     * @param object|null  $object
59
     *
60
     * @return Response
61 5
     */
62
    public function createResponse(Request $request, int $code, string $accept, $object = null): Response
63
    {
64
        $response = $this->responseFactory->createResponse($code);
65
66
        if (null === $object) {
67 5
            if (200 === $code) {
68
                return $response->withStatus(204);
69 5
            }
70 2
71 1
            return $response;
72
        }
73
74 1
        $body = $this->transformer->transform($this->serializer->serialize($request, $object), $accept);
75
76
        $response = $response->withStatus($code)->withHeader('Content-Type', $accept);
77 3
        $response->getBody()->write($body);
78 1
79
        return $response;
80
    }
81 2
82
    /**
83
     * @param Request $request
84 2
     * @param int     $code
85 2
     * @param string  $accept
86
     * @param Error   $error
87 2
     *
88
     * @return Response
89
     */
90
    public function createResponseByError(Request $request, int $code, string $accept, Error $error): Response
91
    {
92
        $response = $this->responseFactory->createResponse($code);
93
94
        $body = $this->transformer->transform($this->serializer->serialize($request, $error), $accept);
95
96
        $response = $response->withStatus($code)->withHeader('Content-Type', $accept);
97
        $response->getBody()->write($body);
98
99
        return $response;
100
    }
101
102
    /**
103
     * @param Request $request
104
     *
105
     * @return Response
106
     */
107
    public function createAcceptNotSupportedResponse(Request $request): Response
108
    {
109
        $response = $this->responseFactory->createResponse(406);
110
        $response->withHeader('X-Not-Acceptable', sprintf(
111
            'Accept "%" is not supported, supported are %s',
112
            $request->getHeaderLine('Accept'),
113
            $this->requestManager->getSupportedAccepts()
114
        ));
115
116
        return $response;
117
    }
118
119
    /**
120
     * @param Request $request
121
     * @param string  $accept
122
     *
123
     * @return Response
124
     */
125 View Code Duplication
    public function createContentTypeNotSupportedResponse(Request $request, string $accept): Response
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...
126
    {
127
        return $this->createResponseByError($request, 415, new Error(
0 ignored issues
show
Documentation introduced by
new \Chubbyphp\ApiHttp\E...pportedContentTypes())) is of type object<Chubbyphp\ApiHttp\Error\Error>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
128
            Error::SCOPE_HEADER,
129
            'contentype_not_supported',
130
            'the given content type is not supported',
131
            'content-type',
132
            [
133
                'contentType' => $request->getHeaderLine('Content-Type'),
134
                'supportedContentTypes' => $this->requestManager->getSupportedContentTypes(),
135
            ]
136
        ), $accept);
0 ignored issues
show
Documentation introduced by
$accept is of type string, but the function expects a object<Chubbyphp\ApiHttp\Error\Error>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
137
    }
138
139
    /**
140
     * @param Request $request
141
     * @param string  $accept
142
     * @param string  $contentType
143
     *
144
     * @return Response
145
     */
146 View Code Duplication
    public function createBodyNotDeserializableResponse(Request $request, string $accept, string $contentType): Response
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
    {
148
        return $this->createResponseByError($request, 400, new Error(
0 ignored issues
show
Documentation introduced by
new \Chubbyphp\ApiHttp\E...) $request->getBody())) is of type object<Chubbyphp\ApiHttp\Error\Error>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
149
            Error::SCOPE_BODY,
150
            'body_not_parsable',
151
            'the given body is not parsable with given content-type',
152
            'deserialize',
153
            [
154
                'contentType' => $contentType,
155
                'body' => (string) $request->getBody(),
156
            ]
157
        ), $accept);
0 ignored issues
show
Documentation introduced by
$accept is of type string, but the function expects a object<Chubbyphp\ApiHttp\Error\Error>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
158
    }
159
160
    /**
161
     * @param Request $request
162
     * @param string  $accept
163
     * @param string  $scope
164
     * @param string  $type
165
     * @param array   $errors
166
     *
167
     * @return Response
168
     */
169
    public function createValidationErrorResponse(
170
        Request $request,
171
        string $accept,
172
        string $scope,
173
        string $type,
174
        array $errors
175
    ): Response {
176
        return $this->createResponseByError($request, 422, new Error(
0 ignored issues
show
Documentation introduced by
new \Chubbyphp\ApiHttp\E...model', $type, $errors) is of type object<Chubbyphp\ApiHttp\Error\Error>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
177
            $scope,
178
            'validation_error',
179
            'there where validation errors while validating the model',
180
            $type,
181
            $errors
182
        ), $accept);
0 ignored issues
show
Documentation introduced by
$accept is of type string, but the function expects a object<Chubbyphp\ApiHttp\Error\Error>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
183
    }
184
185
    /**
186
     * @param Request $request
187
     * @param string $accept
188
     * @param string $type
189
     * @param array $arguments
190
     * @return Response
191
     */
192
    public function createResourceNotFoundResponse(
193
        Request $request,
194
        string $accept,
195
        string $type,
196
        array $arguments
197
    ): Response {
198
        return $this->createResponseByError($request, 404, new Error(
0 ignored issues
show
Documentation introduced by
new \Chubbyphp\ApiHttp\E...st', $type, $arguments) is of type object<Chubbyphp\ApiHttp\Error\Error>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
199
            Error::SCOPE_RESOURCE,
200
            'resource_not_found',
201
            'the wished resource does not exist',
202
            $type, $arguments
203
        ), $accept);
0 ignored issues
show
Documentation introduced by
$accept is of type string, but the function expects a object<Chubbyphp\ApiHttp\Error\Error>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
204
    }
205
}
206