Passed
Pull Request — master (#32)
by
unknown
03:35
created

ResponseBuilder::respond()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 2
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Flugg\Responder\Http;
4
5
use Illuminate\Contracts\Support\Arrayable;
6
use Illuminate\Contracts\Support\Jsonable;
7
use Illuminate\Http\JsonResponse;
8
use Illuminate\Support\Collection;
9
use JsonSerializable;
10
11
/**
12
 * This class is an abstract response builder and hold common functionality the success-
13
 * and error response buuilder classes.
14
 *
15
 * @package flugger/laravel-responder
16
 * @author  Alexander Tømmerås <[email protected]>
17
 * @license The MIT License
18
 */
19
abstract class ResponseBuilder implements Arrayable, Jsonable, JsonSerializable
20
{
21
22
    /**
23
     * Flag indicating if success flag should be added to the serialized data.
24
     *
25
     * @var bool
26
     */
27
    protected $includeSuccessFlag;
28
29
    /**
30
     * Flag indicating if status code should be added to the serialized data.
31
     *
32
     * @var bool
33
     */
34
    protected $includeStatusCode;
35
36
    /**
37
     * The success flag property
38
     *
39
     * @var bool
40
     */
41
    protected $successFlag;
42
43
    /**
44
     * The HTTP status code for the response.
45
     *
46
     * @var int
47
     */
48
    protected $statusCode;
49
50
    /**
51
     * Response factory used to generate JSON responses.
52
     *
53
     * @var \Illuminate\Contracts\Routing\ResponseFactory|\Laravel\Lumen\Http\ResponseFactory $responseFactory
54
     */
55
    protected $responseFactory;
56
57
    /**
58
     * Constructor.
59
     *
60
     * @param \Illuminate\Contracts\Routing\ResponseFactory|\Laravel\Lumen\Http\ResponseFactory $responseFactory
61
     */
62
    public function __construct($responseFactory)
63
    {
64
        $this->responseFactory = $responseFactory;
65
    }
66
67
    /**
68
     * Serialize the data and wrap it in a JSON response object.
69
     *
70
     * @param  int|null $statusCode
71
     * @param  array    $headers
72
     * @return \Illuminate\Http\JsonResponse
73
     */
74
    public function respond(int $statusCode = null, array $headers = []):JsonResponse
75
    {
76
        if (! is_null($statusCode)) {
77
            $this->setStatus($statusCode);
78
        }
79
80
        $data = $this->toArray();
81
        $data = $this->includeStatusCode($data);
82
        $data = $this->includeSuccessFlag($data);
83
84
        return $this->responseFactory->json($data, $this->statusCode, $headers);
85
    }
86
87
    /**
88
     * Set the HTTP status code for the response.
89
     *
90
     * @param  int $statusCode
91
     * @return self
92
     */
93
    public function setStatus(int $statusCode):ResponseBuilder
94
    {
95
        $this->statusCode = $statusCode;
96
97
        return $this;
98
    }
99
100
    /**
101
     * Return response success flag
102
     *
103
     * @return bool
104
     */
105
    abstract public function isSuccessResponse():bool;
106
107
    /**
108
     * Set a flag indicating if success should be added to the response.
109
     *
110
     * @param  bool $includeSuccessFlag
111
     * @return self
112
     */
113
    public function setIncludeSuccessFlag(bool $includeSuccessFlag):ResponseBuilder
114
    {
115
        $this->includeSuccessFlag = $includeSuccessFlag;
116
117
        return $this;
118
    }
119
120
    /**
121
     * Set a flag indicating if status code should be added to the response.
122
     *
123
     * @param  bool $includeStatusCode
124
     * @return self
125
     */
126
    public function setIncludeStatusCode(bool $includeStatusCode):ResponseBuilder
127
    {
128
        $this->includeStatusCode = $includeStatusCode;
129
130
        return $this;
131
    }
132
133
    /**
134
     * Convert the response to an Illuminate collection.
135
     *
136
     * @return \Illuminate\Support\Collection
137
     */
138
    public function toCollection():Collection
139
    {
140
        return new Collection($this->toArray());
141
    }
142
143
    /**
144
     * Convert the response to JSON.
145
     *
146
     * @param  int $options
147
     * @return string
148
     */
149
    public function toJson($options = 0)
150
    {
151
        return json_encode($this->jsonSerialize(), $options);
152
    }
153
154
    /**
155
     * Convert the object into something JSON serializable.
156
     *
157
     * @return array
158
     */
159
    public function jsonSerialize()
160
    {
161
        return $this->toArray();
162
    }
163
164
    /**
165
     * Convert the response to an array.
166
     *
167
     * @return array
168
     */
169
    abstract public function toArray():array;
170
171
    /**
172
     * Include a status code to the serialized data if enabled.
173
     *
174
     * @param  array $data
175
     * @return array
176
     */
177
    protected function includeSuccessFlag(array $data):array
178
    {
179
        if (! $this->includeSuccessFlag) {
180
            return $data;
181
        }
182
183
        return array_merge(['success' => $this->isSuccessResponse()], $data);
184
    }
185
186
    /**
187
     * Include a status code to the serialized data if enabled.
188
     *
189
     * @param  array $data
190
     * @return array
191
     */
192
    protected function includeStatusCode(array $data):array
193
    {
194
        if (! $this->includeStatusCode) {
195
            return $data;
196
        }
197
198
        return array_merge(['status' => $this->statusCode], $data);
199
    }
200
}