Passed
Push — master ( 00c130...483a28 )
by iLex
02:10
created

CustomResponseFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ilex\CustomResponse;
6
7
use Psr\Http\Message\ResponseFactoryInterface;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\UriInterface;
10
11
final class CustomResponseFactory implements CustomResponseFactoryInterface
12
{
13
    /**
14
     * @var ResponseFactoryInterface
15
     */
16
    private $responseFactory;
17
18 1
    public function __construct(ResponseFactoryInterface $responseFactory)
19
    {
20 1
        $this->responseFactory = $responseFactory;
21 1
    }
22
23
    /**
24
     * Create a JSON response with the given data.
25
     *
26
     * Default JSON encoding is performed with the following options, which
27
     * produces RFC4627-compliant JSON, capable of embedding into HTML.
28
     *
29
     * - JSON_HEX_TAG
30
     * - JSON_HEX_APOS
31
     * - JSON_HEX_AMP
32
     * - JSON_HEX_QUOT
33
     * - JSON_UNESCAPED_SLASHES
34
     *
35
     * @param array $data Data to convert to JSON.
36
     * @param int $status Integer status code for the response; 200 by default.
37
     * @param int $encodingOptions JSON encoding options to use.
38
     *
39
     * @return ResponseInterface
40
     */
41 4
    public function createJsonResponseFromArray(
42
        array $data,
43
        int $status = 200,
44
        int $encodingOptions = CustomResponseFactoryInterface::DEFAULT_JSON_FLAGS
45
    ): ResponseInterface {
46 4
        $response = $this->responseFactory->createResponse($status)
47 4
            ->withHeader('content-type', 'application/json');
48 4
        $json = \json_encode($data, $encodingOptions);
49 4
        if (false === $json) {
50 2
            throw new \RuntimeException('json_encode have error');
51
        }
52 2
        $response->getBody()->write($json);
53 2
        return $response;
54
    }
55
56
    /**
57
     * 201 Created responses are often empty, and only include a Link or
58
     *      Location header pointing to the newly created resource.
59
     * 202 Accepted responses are typically empty, indicating that the new
60
     *      entity has been received, but not yet processed.
61
     * 204 No Content responses are, by definition, empty, and often used as a
62
     *      success response when deleting an entity.
63
     *
64
     * @param int $status
65
     *
66
     * @return ResponseInterface
67
     */
68 2
    public function createEmptyResponse(
69
        int $status = CustomResponseFactoryInterface::DEFAULT_STATUS_EMPTY
70
    ): ResponseInterface {
71 2
        return $this->responseFactory->createResponse($status);
72
    }
73
74 4
    public function createRedirectResponseFromString(
75
        string $uri,
76
        int $status = CustomResponseFactoryInterface::DEFAULT_STATUS_REDIRECT
77
    ): ResponseInterface {
78 4
        return $this->responseFactory->createResponse($status)
79 4
            ->withHeader('location', $uri);
80
    }
81
82 2
    public function createRedirectResponseFromUri(
83
        UriInterface $uri,
84
        int $status = CustomResponseFactoryInterface::DEFAULT_STATUS_REDIRECT
85
    ): ResponseInterface {
86 2
        return $this->createRedirectResponseFromString((string)$uri, $status);
87
    }
88
}
89