Passed
Pull Request — master (#27)
by Anatoly
04:06
created

JsonResponse   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 63
ccs 18
cts 18
cp 1
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A createBody() 0 22 3
A __construct() 0 7 1
1
<?php declare(strict_types=1);
2
3
/**
4
 * It's free open-source software released under the MIT License.
5
 *
6
 * @author Anatoly Nekhay <[email protected]>
7
 * @copyright Copyright (c) 2018, Anatoly Nekhay
8
 * @license https://github.com/sunrise-php/http-message/blob/master/LICENSE
9
 * @link https://github.com/sunrise-php/http-message
10
 */
11
12
namespace Sunrise\Http\Message\Response;
13
14
/**
15
 * Import classes
16
 */
17
use Psr\Http\Message\StreamInterface;
18
use Sunrise\Http\Message\Exception\InvalidArgumentException;
19
use Sunrise\Http\Message\Response;
20
use Sunrise\Http\Message\Stream\PhpTempStream;
21
use JsonException;
22
23
/**
24
 * Import functions
25
 */
26
use function json_encode;
27
28
/**
29
 * Import constants
30
 */
31
use const JSON_THROW_ON_ERROR;
32
33
/**
34
 * JSON Response
35
 */
36
class JsonResponse extends Response
37
{
38
39
    /**
40
     * The response content type
41
     *
42
     * @var string
43
     */
44
    public const CONTENT_TYPE = 'application/json; charset=utf-8';
45
46
    /**
47
     * Constructor of the class
48
     *
49
     * @param int $statusCode
50
     * @param mixed $data
51
     * @param int $flags
52
     * @param int $depth
53
     *
54
     * @throws InvalidArgumentException
55
     */
56 4
    public function __construct(int $statusCode, $data, int $flags = 0, int $depth = 512)
57
    {
58 4
        $body = $this->createBody($data, $flags, $depth);
59
60 3
        $headers = ['Content-Type' => self::CONTENT_TYPE];
61
62 3
        parent::__construct($statusCode, null, $headers, $body);
63
    }
64
65
    /**
66
     * Creates the response body from the given JSON data
67
     *
68
     * @param mixed $data
69
     * @param int $flags
70
     * @param int $depth
71
     *
72
     * @return StreamInterface
73
     *
74
     * @throws InvalidArgumentException
75
     *         If the response body cannot be created from the given JSON data.
76
     */
77 4
    private function createBody($data, int $flags, int $depth): StreamInterface
78
    {
79 4
        if ($data instanceof StreamInterface) {
80 1
            return $data;
81
        }
82
83 3
        $flags |= JSON_THROW_ON_ERROR;
84
85
        try {
86 3
            $payload = json_encode($data, $flags, $depth);
87 1
        } catch (JsonException $e) {
88 1
            throw new InvalidArgumentException(sprintf(
89 1
                'Unable to create JSON response due to invalid JSON data: %s',
90 1
                $e->getMessage()
91 1
            ), 0, $e);
92
        }
93
94 2
        $stream = new PhpTempStream('r+b');
95 2
        $stream->write($payload);
96 2
        $stream->rewind();
97
98 2
        return $stream;
99
    }
100
}
101