Completed
Pull Request — master (#146)
by
unknown
06:07 queued 02:41
created

Responses::getIdentifiersResponse()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 11

Duplication

Lines 14
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 14
loc 14
ccs 6
cts 6
cp 1
rs 9.4285
cc 3
eloc 11
nc 4
nop 5
crap 3
1
<?php namespace Neomerx\JsonApi\Http;
2
3
/**
4
 * Copyright 2015 [email protected] (www.neomerx.com)
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\Document\Error;
20
use \Neomerx\JsonApi\Exceptions\ErrorCollection;
21
use \Neomerx\JsonApi\Contracts\Http\ResponsesInterface;
22
use \Neomerx\JsonApi\Contracts\Encoder\EncoderInterface;
23
use \Neomerx\JsonApi\Contracts\Schema\ContainerInterface;
24
use \Neomerx\JsonApi\Contracts\Http\Headers\HeaderInterface;
25
use \Neomerx\JsonApi\Contracts\Http\Headers\MediaTypeInterface;
26
use \Neomerx\JsonApi\Contracts\Http\Headers\SupportedExtensionsInterface;
27
use \Neomerx\JsonApi\Contracts\Encoder\Parameters\EncodingParametersInterface;
28
29
/**
30
 * @package Neomerx\JsonApi
31
 */
32
abstract class Responses implements ResponsesInterface
33
{
34
    /** Header name that contains format of input data from client */
35
    const HEADER_CONTENT_TYPE = HeaderInterface::HEADER_CONTENT_TYPE;
36
37
    /** Header name that location of newly created resource */
38
    const HEADER_LOCATION = HeaderInterface::HEADER_LOCATION;
39
40
    /**
41
     * Create HTTP response.
42
     *
43
     * @param string|null $content
44
     * @param int         $statusCode
45
     * @param array       $headers
46
     *
47
     * @return mixed
48
     */
49
    abstract protected function createResponse($content, $statusCode, array $headers);
50
51
    /**
52
     * @return EncoderInterface
53
     */
54
    abstract protected function getEncoder();
55
56
    /**
57
     * @return string|null
58
     */
59
    abstract protected function getUrlPrefix();
60
61
    /**
62
     * @return EncodingParametersInterface|null
63
     */
64
    abstract protected function getEncodingParameters();
65
66
    /**
67
     * @return ContainerInterface
68
     */
69
    abstract protected function getSchemaContainer();
70
71
    /**
72
     * @return SupportedExtensionsInterface|null
73
     */
74
    abstract protected function getSupportedExtensions();
75
76
    /**
77
     * @return MediaTypeInterface
78
     */
79
    abstract protected function getMediaType();
80
81
    /**
82
     * @inheritdoc
83
     */
84 2 View Code Duplication
    public function getContentResponse(
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...
85
        $data,
86
        $statusCode = self::HTTP_OK,
87
        $links = null,
88
        $meta = null,
89
        array $headers = []
90
    ) {
91 2
        $encoder = $this->getEncoder();
92 2
        $links === null ?: $encoder->withLinks($links);
93 2
        $meta === null ?: $encoder->withMeta($meta);
94 2
        $content = $encoder->encodeData($data, $this->getEncodingParameters());
95
96 2
        return $this->createJsonApiResponse($content, $statusCode, $headers);
97
    }
98
99
    /**
100
     * @inheritdoc
101
     */
102 2 View Code Duplication
    public function getCreatedResponse($resource, $links = null, $meta = null, array $headers = [])
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...
103
    {
104 2
        $encoder = $this->getEncoder();
105 2
        $links === null ?: $encoder->withLinks($links);
106 2
        $meta === null ?: $encoder->withMeta($meta);
107 2
        $content = $encoder->encodeData($resource, $this->getEncodingParameters());
108 2
        $headers[self::HEADER_LOCATION] = $this->getResourceLocationUrl($resource);
109
110 2
        return $this->createJsonApiResponse($content, self::HTTP_CREATED, $headers);
111
    }
112
113
    /**
114
     * @inheritdoc
115
     */
116 5
    public function getCodeResponse($statusCode, array $headers = [])
117
    {
118 5
        return $this->createJsonApiResponse(null, $statusCode, $headers);
119
    }
120
121
    /**
122
     * @inheritdoc
123
     */
124 2
    public function getMetaResponse($meta, $statusCode = self::HTTP_OK, array $headers = [])
125
    {
126 2
        $encoder = $this->getEncoder();
127 2
        $content = $encoder->encodeMeta($meta);
128
129 2
        return $this->createJsonApiResponse($content, $statusCode, $headers);
130
    }
131
132
    /**
133
     * @inheritDoc
134
     */
135 2 View Code Duplication
    public function getIdentifiersResponse(
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...
136
        $data,
137
        $statusCode = self::HTTP_OK,
138
        $links = null,
139
        $meta = null,
140
        array $headers = []
141
    ) {
142 2
        $encoder = $this->getEncoder();
143 2
        $links === null ?: $encoder->withLinks($links);
144 2
        $meta === null ?: $encoder->withMeta($meta);
145 2
        $content = $encoder->encodeIdentifiers($data, $this->getEncodingParameters());
146
147 2
        return $this->createJsonApiResponse($content, $statusCode, $headers); 
148
    }
149
150
    /**
151
     * @inheritdoc
152
     */
153 4
    public function getErrorResponse($errors, $statusCode = self::HTTP_BAD_REQUEST, array $headers = [])
154
    {
155 4
        if ($errors instanceof ErrorCollection || is_array($errors) === true) {
156
            /** @var Error[] $errors */
157 2
            $content = $this->getEncoder()->encodeErrors($errors);
158 2
        } else {
159
            /** @var Error $errors */
160 2
            $content = $this->getEncoder()->encodeError($errors);
161
        }
162
163 4
        return $this->createJsonApiResponse($content, $statusCode, $headers);
164
    }
165
166
    /**
167
     * @param mixed $resource
168
     *
169
     * @return string
170
     */
171 2
    protected function getResourceLocationUrl($resource)
172
    {
173 2
        $resSubUrl = $this->getSchemaContainer()->getSchema($resource)->getSelfSubLink($resource)->getSubHref();
174 2
        $urlPrefix = $this->getUrlPrefix();
175 2
        $location  = $urlPrefix . $resSubUrl;
176
177 2
        return $location;
178
    }
179
180
    /**
181
     * @param string|null $content
182
     * @param int         $statusCode
183
     * @param array       $headers
184
     *
185
     * @return mixed
186
     */
187 17
    protected function createJsonApiResponse($content, $statusCode, array $headers = [])
188
    {
189 17
        $mediaType   = $this->getMediaType();
190 17
        $contentType = $mediaType->getMediaType();
191 17
        $params      = $mediaType->getParameters();
192
193 17
        $separator = ';';
194 17
        if (isset($params[MediaTypeInterface::PARAM_EXT])) {
195 2
            $ext = $params[MediaTypeInterface::PARAM_EXT];
196 2
            if (empty($ext) === false) {
197 2
                $contentType .= $separator . MediaTypeInterface::PARAM_EXT . '="' . $ext . '"';
198 2
                $separator = ',';
199 2
            }
200 2
        }
201
202 17
        $extensions = $this->getSupportedExtensions();
203 17
        if ($extensions !== null && ($list = $extensions->getExtensions()) !== null && empty($list) === false) {
204 2
            $contentType .= $separator . MediaTypeInterface::PARAM_SUPPORTED_EXT . '="' . $list . '"';
205 2
        }
206
207 17
        $headers[self::HEADER_CONTENT_TYPE] = $contentType;
208
209 17
        return $this->createResponse($content, $statusCode, $headers);
210
    }
211
}
212