ResponseBuilder::setStatusCode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Tequilarapido\ApiResponse;
4
5
use Illuminate\Http\Response as HttpResponse;
6
use Symfony\Component\HttpFoundation\Cookie;
7
8
/**
9
 * Inspired from dingo/api.
10
 *
11
 * Class ResponseBuilder
12
 */
13
class ResponseBuilder
14
{
15
    /**
16
     * Response content.
17
     *
18
     * @var mixed
19
     */
20
    protected $response;
21
22
    /**
23
     * The HTTP response headers.
24
     *
25
     * @var array
26
     */
27
    protected $headers = [];
28
29
    /**
30
     * The HTTP response cookies.
31
     *
32
     * @var array
33
     */
34
    protected $cookies = [];
35
36
    /**
37
     * The HTTP response status code.
38
     *
39
     * @var int
40
     */
41
    protected $statusCode = 200;
42
43
    /**
44
     * Create a new response builder instance.
45
     *
46
     * @param mixed $response
47
     */
48 20
    public function __construct($response)
49
    {
50 20
        $this->response = $response;
51 20
    }
52
53
    /**
54
     * Add a cookie to the response.
55
     *
56
     * @param \Symfony\Component\HttpFoundation\Cookie $cookie
57
     *
58
     * @return ResponseBuilder
59
     */
60 4
    public function withCookie(Cookie $cookie)
61
    {
62 4
        $this->cookies[] = $cookie;
63
64 4
        return $this;
65
    }
66
67
    /**
68
     * Add a cookie to the response.
69
     *
70
     * @param \Symfony\Component\HttpFoundation\Cookie $cookie
71
     *
72
     * @return ResponseBuilder
73
     */
74 2
    public function cookie(Cookie $cookie)
75
    {
76 2
        return $this->withCookie($cookie);
77
    }
78
79
    /**
80
     * Add a header to the response.
81
     *
82
     * @param string $name
83
     * @param string $value
84
     *
85
     * @return ResponseBuilder
86
     */
87 3
    public function withHeader($name, $value)
88
    {
89 3
        $this->headers[$name] = $value;
90
91 3
        return $this;
92
    }
93
94
    /**
95
     * Add a header to the response.
96
     *
97
     * @param string $name
98
     * @param string $value
99
     *
100
     * @return ResponseBuilder
101
     */
102 2
    public function header($name, $value)
103
    {
104 2
        return $this->withHeader($name, $value);
105
    }
106
107
    /**
108
     * Set the responses status code.
109
     *
110
     * @param int $statusCode
111
     *
112
     * @return ResponseBuilder
113
     */
114 9
    public function setStatusCode($statusCode)
115
    {
116 9
        $this->statusCode = $statusCode;
117
118 9
        return $this;
119
    }
120
121
    /**
122
     * Set the response status code.
123
     *
124
     * @param int $statusCode
125
     *
126
     * @return ResponseBuilder
127
     */
128 1
    public function statusCode($statusCode)
129
    {
130 1
        return $this->setStatusCode($statusCode);
131
    }
132
133
    /**
134
     * Build the response.
135
     *
136
     * @return \Illuminate\Http\Response
137
     */
138 14
    public function build()
139
    {
140 14
        $response = new HttpResponse($this->response, $this->statusCode, $this->headers);
141
        
142
        // Force json content type
143 14
        $response->header('Content-Type', 'application/json');
144
145 14
        foreach ($this->cookies as $cookie) {
146 1
            if ($cookie instanceof Cookie) {
147 1
                $response->withCookie($cookie);
148
            }
149
        }
150
151 14
        return $response;
152
    }
153
154
    /**
155
     * Returns cookies.
156
     *
157
     * @return array
158
     */
159 3
    public function getCookies()
160
    {
161 3
        return $this->cookies;
162
    }
163
164
    /**
165
     * Return headers.
166
     *
167
     * @return array
168
     */
169 2
    public function getHeaders()
170
    {
171 2
        return $this->headers;
172
    }
173
174
    /**
175
     * Return status code.
176
     *
177
     * @return int
178
     */
179 1
    public function getStatusCode()
180
    {
181 1
        return $this->statusCode;
182
    }
183
    
184
    /**
185
     * Add meta to response
186
     *
187
     * @param $key
188
     * @param $value
189
     * @return $this
190
     */
191
    public function setMeta($key, $value)
192
    {
193
        if (!isset($this->response['meta'])) {
194
            $this->response['meta'] = [];
195
        }
196
197
        $this->response['meta'][$key] = $value;
198
        
199
        return $this;
200
    }
201
}
202