Completed
Branch master (85eed3)
by
unknown
02:32
created

Responses::createResponse()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 1
ccs 0
cts 0
cp 0
nc 1
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\Parameters\EncodingParametersInterface;
27
use \Neomerx\JsonApi\Contracts\Http\Parameters\SupportedExtensionsInterface;
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 1
    public function getContentResponse($data, $statusCode = self::HTTP_OK, $links = null, $meta = null)
85
    {
86 1
        $encoder = $this->getEncoder();
87 1
        $links === null ?: $encoder->withLinks($links);
88 1
        $meta === null ?: $encoder->withMeta($meta);
89 1
        $content = $encoder->encodeData($data, $this->getEncodingParameters());
90
91 1
        return $this->createJsonApiResponse($content, $statusCode);
92
    }
93
94
    /**
95
     * @inheritdoc
96
     */
97 1
    public function getCreatedResponse($resource, $links = null, $meta = null)
98
    {
99 1
        $encoder = $this->getEncoder();
100 1
        $links === null ?: $encoder->withLinks($links);
101 1
        $meta === null ?: $encoder->withMeta($meta);
102 1
        $content = $encoder->encodeData($resource, $this->getEncodingParameters());
103 1
        $headers = [self::HEADER_LOCATION => $this->getResourceLocationUrl($resource)];
104
105 1
        return $this->createJsonApiResponse($content, self::HTTP_CREATED, $headers);
106
    }
107
108
    /**
109
     * @inheritdoc
110
     */
111 4
    public function getCodeResponse($statusCode)
112
    {
113 4
        return $this->createJsonApiResponse(null, $statusCode);
114
    }
115
116
    /**
117
     * @inheritdoc
118
     */
119 1
    public function getMetaResponse($meta, $statusCode = self::HTTP_OK)
120
    {
121 1
        $encoder = $this->getEncoder();
122 1
        $content = $encoder->encodeMeta($meta);
123
124 1
        return $this->createJsonApiResponse($content, $statusCode);
125
    }
126
127
    /**
128
     * @inheritdoc
129
     */
130 3
    public function getErrorResponse($errors, $statusCode = self::HTTP_BAD_REQUEST)
131
    {
132 3
        if ($errors instanceof ErrorCollection || is_array($errors) === true) {
133
            /** @var Error[] $errors */
134 2
            $content = $this->getEncoder()->encodeErrors($errors);
135 2
        } else {
136
            /** @var Error $errors */
137 1
            $content = $this->getEncoder()->encodeError($errors);
138
        }
139
140 3
        return $this->createJsonApiResponse($content, $statusCode);
141
    }
142
143
    /**
144
     * @param mixed $resource
145
     *
146
     * @return string
147
     */
148 1
    protected function getResourceLocationUrl($resource)
149
    {
150 1
        $resSubUrl = $this->getSchemaContainer()->getSchema($resource)->getSelfSubLink($resource)->getSubHref();
151 1
        $urlPrefix = $this->getUrlPrefix();
152 1
        $location  = $urlPrefix . $resSubUrl;
153
154 1
        return $location;
155
    }
156
157
    /**
158
     * @param string|null $content
159
     * @param int         $statusCode
160
     * @param array       $headers
161
     *
162
     * @return mixed
163
     */
164 10
    protected function createJsonApiResponse($content, $statusCode, array $headers = [])
165
    {
166 10
        $mediaType   = $this->getMediaType();
167 10
        $contentType = $mediaType->getMediaType();
168 10
        $params      = $mediaType->getParameters();
169
170 10
        $separator = ';';
171 10
        if (isset($params[MediaTypeInterface::PARAM_EXT])) {
172 2
            $ext = $params[MediaTypeInterface::PARAM_EXT];
173 2
            if (empty($ext) === false) {
174 2
                $contentType .= $separator . MediaTypeInterface::PARAM_EXT . '="' . $ext . '"';
175 2
                $separator = ',';
176 2
            }
177 2
        }
178
179 10
        $extensions = $this->getSupportedExtensions();
180 10
        if ($extensions !== null && ($list = $extensions->getExtensions()) !== null && empty($list) === false) {
181 2
            $contentType .= $separator . MediaTypeInterface::PARAM_SUPPORTED_EXT . '="' . $list . '"';
182 2
        }
183
184 10
        $headers[self::HEADER_CONTENT_TYPE] = $contentType;
185
186 10
        return $this->createResponse($content, $statusCode, $headers);
187
    }
188
}
189