Completed
Push — develop ( d26178...6d3477 )
by Neomerx
04:43
created

JsonApiResponse::createBody()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
crap 2
1
<?php namespace Limoncello\Flute\Http;
2
3
/**
4
 * Copyright 2015-2017 [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\Http\Headers\MediaTypeInterface;
20
use Zend\Diactoros\Response;
21
use Zend\Diactoros\Response\InjectContentTypeTrait;
22
use Zend\Diactoros\Stream;
23
24
/**
25
 * @package Limoncello\Flute
26
 */
27
class JsonApiResponse extends Response
28
{
29
    /** HTTP code */
30
    const HTTP_UNPROCESSABLE_ENTITY = 422;
31
32
    use InjectContentTypeTrait;
33
34
    /**
35
     * @param string|null $content
36
     * @param int         $status
37
     * @param array       $headers
38
     */
39 25
    public function __construct(string $content = null, int $status = 200, array $headers = [])
40
    {
41 25
        $body = new Stream('php://temp', 'wb+');
42
43 25
        if ($content !== null) {
44 22
            $body->write($content);
45 22
            $body->rewind();
46
        }
47
48
        // inject content-type even when there is no content otherwise
49
        // it would be set to 'text/html' by PHP/Web server/Browser
50 25
        $headers = $this->injectContentType(MediaTypeInterface::JSON_API_MEDIA_TYPE, $headers);
51
52 25
        parent::__construct($body, $status, $headers);
53
    }
54
}
55