Completed
Branch master (ffe81d)
by Neomerx
04:33
created

BaseResponses::getCodeResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php namespace Neomerx\JsonApi\Http;
2
3
/**
4
 * Copyright 2015-2018 [email protected]
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use Neomerx\JsonApi\Contracts\Document\ErrorInterface;
20
use Neomerx\JsonApi\Contracts\Encoder\EncoderInterface;
21
use Neomerx\JsonApi\Contracts\Encoder\Parameters\EncodingParametersInterface;
22
use Neomerx\JsonApi\Contracts\Http\Headers\HeaderParametersParserInterface;
23
use Neomerx\JsonApi\Contracts\Http\Headers\MediaTypeInterface;
24
use Neomerx\JsonApi\Contracts\Http\ResponsesInterface;
25
use Neomerx\JsonApi\Contracts\Schema\ContainerInterface;
26
use Neomerx\JsonApi\Exceptions\ErrorCollection;
27
28
/**
29
 * @package Neomerx\JsonApi
30
 */
31
abstract class BaseResponses implements ResponsesInterface
32
{
33
    /** Header name that contains format of input data from client */
34
    const HEADER_CONTENT_TYPE = HeaderParametersParserInterface::HEADER_CONTENT_TYPE;
35
36
    /** Header name that location of newly created resource */
37
    const HEADER_LOCATION = 'Location';
38
39
    /**
40
     * Create HTTP response.
41
     *
42
     * @param string|null $content
43
     * @param int         $statusCode
44
     * @param array       $headers
45
     *
46
     * @return mixed
47
     */
48
    abstract protected function createResponse(?string $content, int $statusCode, array $headers);
49
50
    /**
51
     * @return EncoderInterface
52
     */
53
    abstract protected function getEncoder(): EncoderInterface;
54
55
    /**
56
     * @return string|null
57
     */
58
    abstract protected function getUrlPrefix(): ?string;
59
60
    /**
61
     * @return EncodingParametersInterface|null
62
     */
63
    abstract protected function getEncodingParameters(): ?EncodingParametersInterface;
64
65
    /**
66
     * @return ContainerInterface
67
     */
68
    abstract protected function getSchemaContainer(): ?ContainerInterface;
69
70
    /**
71
     * @return MediaTypeInterface
72
     */
73
    abstract protected function getMediaType(): MediaTypeInterface;
74
75
    /**
76
     * @inheritdoc
77
     */
78 2
    public function getContentResponse(
79
        $data,
80
        int $statusCode = self::HTTP_OK,
81
        array $links = null,
82
        $meta = null,
83
        array $headers = []
84
    ) {
85 2
        $encoder = $this->getEncoder();
86 2
        $links === null ?: $encoder->withLinks($links);
87 2
        $meta === null ?: $encoder->withMeta($meta);
88 2
        $content = $encoder->encodeData($data, $this->getEncodingParameters());
89
90 2
        return $this->createJsonApiResponse($content, $statusCode, $headers, true);
91
    }
92
93
    /**
94
     * @inheritdoc
95
     */
96 2
    public function getCreatedResponse($resource, array $links = null, $meta = null, array $headers = [])
97
    {
98 2
        $encoder = $this->getEncoder();
99 2
        $links === null ?: $encoder->withLinks($links);
100 2
        $meta === null ?: $encoder->withMeta($meta);
101 2
        $content = $encoder->encodeData($resource, $this->getEncodingParameters());
102 2
        $headers[self::HEADER_LOCATION] = $this->getResourceLocationUrl($resource);
103
104 2
        return $this->createJsonApiResponse($content, self::HTTP_CREATED, $headers, true);
105
    }
106
107
    /**
108
     * @inheritdoc
109
     */
110 1
    public function getCodeResponse(int $statusCode, array $headers = [])
111
    {
112 1
        return $this->createJsonApiResponse(null, $statusCode, $headers, false);
113
    }
114
115
    /**
116
     * @inheritdoc
117
     */
118 2
    public function getMetaResponse($meta, int $statusCode = self::HTTP_OK, array $headers = [])
119
    {
120 2
        $encoder = $this->getEncoder();
121 2
        $content = $encoder->encodeMeta($meta);
122
123 2
        return $this->createJsonApiResponse($content, $statusCode, $headers, true);
124
    }
125
126
    /**
127
     * @inheritDoc
128
     */
129 2
    public function getIdentifiersResponse(
130
        $data,
131
        int $statusCode = self::HTTP_OK,
132
        array $links = null,
133
        $meta = null,
134
        array $headers = []
135
    ) {
136 2
        $encoder = $this->getEncoder();
137 2
        $links === null ?: $encoder->withLinks($links);
138 2
        $meta === null ?: $encoder->withMeta($meta);
139 2
        $content = $encoder->encodeIdentifiers($data, $this->getEncodingParameters());
140
141 2
        return $this->createJsonApiResponse($content, $statusCode, $headers, true);
142
    }
143
144
    /**
145
     * @inheritdoc
146
     *
147
     * @SuppressWarnings(PHPMD.ElseExpression)
148
     */
149 4
    public function getErrorResponse($errors, int $statusCode = self::HTTP_BAD_REQUEST, array $headers = [])
150
    {
151 4
        if ($errors instanceof ErrorCollection || is_array($errors) === true) {
152
            /** @var ErrorInterface[] $errors */
153 2
            $content = $this->getEncoder()->encodeErrors($errors);
154
        } else {
155
            /** @var ErrorInterface $errors */
156 2
            $content = $this->getEncoder()->encodeError($errors);
157
        }
158
159 4
        return $this->createJsonApiResponse($content, $statusCode, $headers, true);
160
    }
161
162
    /**
163
     * @param mixed $resource
164
     *
165
     * @return string
166
     */
167 2
    protected function getResourceLocationUrl($resource): string
168
    {
169 2
        $resSubUrl = $this->getSchemaContainer()->getSchema($resource)->getSelfSubLink($resource)->getSubHref();
170 2
        $urlPrefix = $this->getUrlPrefix();
171 2
        $location  = $urlPrefix . $resSubUrl;
172
173 2
        return $location;
174
    }
175
176
    /**
177
     * @param string|null $content
178
     * @param int         $statusCode
179
     * @param array       $headers
180
     * @param bool        $addContentType
181
     *
182
     * @return mixed
183
     *
184
     * @SuppressWarnings(PHPMD.BooleanArgumentFlag)
185
     */
186 13
    protected function createJsonApiResponse(
187
        ?string $content,
188
        int $statusCode,
189
        array $headers = [],
190
        $addContentType = true
191
    ) {
192 13
        if ($addContentType === true) {
193 12
            $headers[self::HEADER_CONTENT_TYPE] = $this->getMediaType()->getMediaType();
194
        }
195
196 13
        return $this->createResponse($content, $statusCode, $headers);
197
    }
198
}
199