FakeResponse::withJson()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Zurbaev\ApiClient\Helpers;
4
5
use GuzzleHttp\Psr7\Response;
6
7
class FakeResponse
8
{
9
    /**
10
     * Response body.
11
     *
12
     * @var mixed
13
     */
14
    protected $body;
15
16
    /**
17
     * HTTP Status.
18
     *
19
     * @var int
20
     */
21
    protected $status = 200;
22
23
    /**
24
     * HTTP Headers.
25
     *
26
     * @var array
27
     */
28
    protected $headers = [];
29
30
    /**
31
     * HTTP Version.
32
     *
33
     * @var string
34
     */
35
    protected $httpVersion = '1.1';
36
37
    /**
38
     * Reason phrase.
39
     */
40
    protected $reason;
41
42
    /**
43
     * Creates new fake HTTP Response.
44
     *
45
     * @return $this
46
     */
47
    public static function fake()
48
    {
49
        return new static();
50
    }
51
52
    /**
53
     * Sets HTTP response status.
54
     *
55
     * @param int $status
56
     *
57
     * @return $this
58
     */
59
    public function withStatus(int $status)
60
    {
61
        $this->status = $status;
62
63
        return $this;
64
    }
65
66
    /**
67
     * Sets HTTP response body.
68
     *
69
     * @param string|null|resource|\Psr\Http\Message\StreamInterface $body
70
     *
71
     * @return $this
72
     */
73
    public function withBody($body)
74
    {
75
        $this->body = $body;
76
77
        return $this;
78
    }
79
80
    /**
81
     * Sets response's JSON content.
82
     *
83
     * @param array $json
84
     *
85
     * @return $this
86
     */
87
    public function withJson(array $json)
88
    {
89
        $this->body = json_encode($json);
90
91
        return $this;
92
    }
93
94
    /**
95
     * Sets response's HTTP headers.
96
     *
97
     * @param array $headers
98
     *
99
     * @return $this
100
     */
101
    public function withHeaders(array $headers)
102
    {
103
        $this->headers = $headers;
104
105
        return $this;
106
    }
107
108
    /**
109
     * Sets HTTP version.
110
     *
111
     * @param string $version
112
     *
113
     * @return $this
114
     */
115
    public function withHttpVersion(string $version)
116
    {
117
        $this->httpVersion = $version;
118
119
        return $this;
120
    }
121
122
    /**
123
     * Sets Reason Phrase.
124
     *
125
     * @param string $reason
126
     *
127
     * @return $this
128
     */
129
    public function withReason(string $reason)
130
    {
131
        $this->reason = $reason;
132
133
        return $this;
134
    }
135
136
    /**
137
     * Generates HTTP Response.
138
     *
139
     * @return Response
140
     */
141
    public function toResponse(): Response
142
    {
143
        return new Response(
144
            $this->status,
145
            $this->headers,
146
            $this->body,
147
            $this->httpVersion,
148
            $this->reason
149
        );
150
    }
151
}
152