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

ResponseBuilder::setIncludeSuccessFlag()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
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
     * @param  bool     $successFlag
73
     * @return \Illuminate\Http\JsonResponse
74
     */
75
    public function respond(int $statusCode = null, array $headers = [], bool $successFlag = true):JsonResponse
76
    {
77
        if (! is_null($statusCode)) {
78
            $this->setStatus($statusCode);
79
        }
80
        $this->successFlag = $successFlag;
81
82
        $data = $this->toArray();
83
        $data = $this->includeStatusCode($data);
84
        $data = $this->includeSuccessFlag($data);
85
86
        return $this->responseFactory->json($data, $this->statusCode, $headers);
87
    }
88
89
    /**
90
     * Set the HTTP status code for the response.
91
     *
92
     * @param  int $statusCode
93
     * @return self
94
     */
95
    public function setStatus(int $statusCode):ResponseBuilder
96
    {
97
        $this->statusCode = $statusCode;
98
99
        return $this;
100
    }
101
102
    /**
103
     * Set a flag indicating if success should be added to the response.
104
     *
105
     * @param  bool $includeSuccessFlag
106
     * @return self
107
     */
108
    public function setIncludeSuccessFlag(bool $includeSuccessFlag):ResponseBuilder
109
    {
110
        $this->includeSuccessFlag = $includeSuccessFlag;
111
112
        return $this;
113
    }
114
115
    /**
116
     * Set a flag indicating if status code should be added to the response.
117
     *
118
     * @param  bool $includeStatusCode
119
     * @return self
120
     */
121
    public function setIncludeStatusCode(bool $includeStatusCode):ResponseBuilder
122
    {
123
        $this->includeStatusCode = $includeStatusCode;
124
125
        return $this;
126
    }
127
128
    /**
129
     * Convert the response to an Illuminate collection.
130
     *
131
     * @return \Illuminate\Support\Collection
132
     */
133
    public function toCollection():Collection
134
    {
135
        return new Collection($this->toArray());
136
    }
137
138
    /**
139
     * Convert the response to JSON.
140
     *
141
     * @param  int $options
142
     * @return string
143
     */
144
    public function toJson($options = 0)
145
    {
146
        return json_encode($this->jsonSerialize(), $options);
147
    }
148
149
    /**
150
     * Convert the object into something JSON serializable.
151
     *
152
     * @return array
153
     */
154
    public function jsonSerialize()
155
    {
156
        return $this->toArray();
157
    }
158
159
    /**
160
     * Convert the response to an array.
161
     *
162
     * @return array
163
     */
164
    abstract public function toArray():array;
165
166
    /**
167
     * Include a status code to the serialized data if enabled.
168
     *
169
     * @param  array $data
170
     * @return array
171
     */
172
    protected function includeSuccessFlag(array $data):array
173
    {
174
        if (! $this->includeSuccessFlag) {
175
            return $data;
176
        }
177
178
        return array_merge(['success' => $this->successFlag], $data);
179
    }
180
181
    /**
182
     * Include a status code to the serialized data if enabled.
183
     *
184
     * @param  array $data
185
     * @return array
186
     */
187
    protected function includeStatusCode(array $data):array
188
    {
189
        if (! $this->includeStatusCode) {
190
            return $data;
191
        }
192
193
        return array_merge(['status' => $this->statusCode], $data);
194
    }
195
}