Passed
Push — master ( 7b9d4f...6fbff1 )
by Marcin
02:19
created

ResponseBuilderLegacy::errorWithDataAndHttpCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 5
dl 0
loc 9
ccs 7
cts 7
cp 1
crap 1
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace MarcinOrlowski\ResponseBuilder;
5
6
/**
7
 * Laravel API Response Builder
8
 *
9
 * @package   MarcinOrlowski\ResponseBuilder
10
 *
11
 * @author    Marcin Orlowski <mail (#) marcinOrlowski (.) com>
12
 * @copyright 2016-2019 Marcin Orlowski
13
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
14
 * @link      https://github.com/MarcinOrlowski/laravel-api-response-builder
15
 */
16
17
use Illuminate\Support\Facades\Config;
18
use Illuminate\Support\Facades\Response;
19
use Symfony\Component\HttpFoundation\Response as HttpResponse;
20
21
22
/**
23
 * Builds standardized HttpResponse response object
24
 */
25
class ResponseBuilderLegacy extends ResponseBuilderBase
26
{
27
    /**
28
     * Returns success
29
     *
30
     * @param object|array|null $data          Array of primitives and supported objects to be returned in 'data' node
31
     *                                         of the JSON response, single supported object or @null if there's no
32
     *                                         to be returned.
33
     * @param integer|null      $api_code      API code to be returned or @null to use value of BaseApiCodes::OK().
34
     * @param array|null        $placeholders  Placeholders passed to Lang::get() for message placeholders
35
     *                                         substitution or @null if none.
36
     * @param integer|null      $http_code     HTTP code to be used for HttpResponse sent or @null
37
     *                                         for default DEFAULT_HTTP_CODE_OK.
38
     * @param integer|null      $json_opts     See http://php.net/manual/en/function.json-encode.php for supported
39
     *                                         options or pass @null to use value from your config (or defaults).
40
     *
41
     * @return HttpResponse
42
     */
43 6
    public static function success($data = null, $api_code = null, array $placeholders = null,
44
                                   int $http_code = null, int $json_opts = null): HttpResponse
45
    {
46 6
        return ResponseBuilder::asSuccess($api_code)
47 6
            ->withData($data)
48 6
            ->withPlaceholders($placeholders)
49 6
            ->withHttpCode($http_code)
50 6
            ->withJsonOptions($json_opts)
51 6
            ->build();
52
    }
53
54
    /**
55
     * Returns success
56
     *
57
     * @param integer|null $api_code      API code to be returned or @null to use value of BaseApiCodes::OK().
58
     * @param array|null   $placeholders  Placeholders passed to Lang::get() for message placeholders
59
     *                                    substitution or @null if none.
60
     * @param integer|null $http_code     HTTP code to be used for HttpResponse sent or @null
61
     *                                    for default DEFAULT_HTTP_CODE_OK.
62
     *
63
     * @return HttpResponse
64
     *
65
     * @deprecated Please use Builder class.
66
     */
67 1
    public static function successWithCode(int $api_code = null, array $placeholders = null,
68
                                           int $http_code = null): HttpResponse
69
    {
70 1
        return ResponseBuilder::asSuccess($api_code)
71 1
            ->withPlaceholders($placeholders)
72 1
            ->withHttpCode($http_code)
73 1
            ->build();
74
    }
75
76
    /**
77
     * @param string            $message       Custom message to be returned as part of the response.
78
     * @param object|array|null $data          Array of primitives and supported objects to be returned in 'data' node
79
     *                                         of the JSON response, single supported object or @null if there's no
80
     *                                         to be returned.
81
     * @param integer|null      $http_code     HTTP code to be used for HttpResponse sent or @null
82
     *                                         for default DEFAULT_HTTP_CODE_OK.
83
     *
84
     * @return HttpResponse
85
     *
86
     * @deprecated Please use Builder class.
87
     */
88 1
    public static function successWithMessage(string $message, $data = null, int $api_code = null,
89
                                              int $http_code = null): HttpResponse
90
    {
91 1
        return ResponseBuilder::asSuccess($api_code)
92 1
            ->withMessage($message)
93 1
            ->withData($data)
94 1
            ->withHttpCode($http_code)
95 1
            ->build();
96
    }
97
98
    /**
99
     * @param object|array|null $data          Array of primitives and supported objects to be returned in 'data' node.
100
     *                                         of the JSON response, single supported object or @null if there's no
101
     *                                         to be returned.
102
     * @param integer|null      $api_code      API code to be returned or @null to use value of BaseApiCodes::OK().
103
     * @param array|null        $placeholders  Placeholders passed to Lang::get() for message placeholders
104
     *                                         substitution or @null if none.
105
     * @param integer|null      $http_code     HTTP code to be used for HttpResponse sent or @null
106
     *                                         for default DEFAULT_HTTP_CODE_OK.
107
     * @param integer|null      $json_opts     See http://php.net/manual/en/function.json-encode.php for supported
108
     *                                         options or pass @null to use value from your config (or defaults).
109
     *
110
     * @return HttpResponse
111
     *
112
     * @deprecated Please use Builder class.
113
     */
114 4
    public static function successWithHttpCode(int $http_code = null): HttpResponse
115
    {
116 4
        return ResponseBuilder::asSuccess()
117 4
            ->withHttpCode($http_code)
118 4
            ->build();
119
    }
120
121
    /**
122
     * Builds error Response object. Supports optional arguments passed to Lang::get() if associated error
123
     * message uses placeholders as well as return data payload
124
     *
125
     * @param integer           $api_code      Your API code to be returned with the response object.
126
     * @param array|null        $placeholders  Placeholders passed to Lang::get() for message placeholders
127
     *                                         substitution or @null if none.
128
     * @param object|array|null $data          Array of primitives and supported objects to be returned in 'data' node
129
     *                                         of the JSON response, single supported object or @null if there's no
130
     *                                         to be returned.
131
     * @param integer|null      $http_code     HTTP code to be used for HttpResponse sent or @null
132
     *                                         for default DEFAULT_HTTP_CODE_ERROR.
133
     * @param integer|null      $json_opts     See http://php.net/manual/en/function.json-encode.php for supported
134
     *                                         options or pass @null to use value from your config (or defaults).
135
     *
136
     * @return HttpResponse
137
     */
138 5
    public static function error(int $api_code, array $placeholders = null, $data = null, int $http_code = null,
139
                                 int $json_opts = null): HttpResponse
140
    {
141 5
        return ResponseBuilder::asError($api_code)
142 4
            ->withPlaceholders($placeholders)
143 4
            ->withData($data)
144 4
            ->withHttpCode($http_code)
145 4
            ->withJsonOptions($json_opts)
146 4
            ->build();
147
    }
148
149
    /**
150
     * @param integer           $api_code      Your API code to be returned with the response object.
151
     * @param object|array|null $data          Array of primitives and supported objects to be returned in 'data' node
152
     *                                         of the JSON response, single supported object or @null if there's no
153
     *                                         to be returned.
154
     * @param array|null        $placeholders  Placeholders passed to Lang::get() for message placeholders
155
     *                                         substitution or @null if none.
156
     * @param integer|null      $json_opts     See http://php.net/manual/en/function.json-encode.php for supported
157
     *                                         options or pass @null to use value from your config (or defaults).
158
     *
159
     * @return HttpResponse
160
     *
161
     * @deprecated Please use Builder class.
162
     */
163 1
    public static function errorWithData(int $api_code, $data, array $placeholders = null,
164
                                         int $json_opts = null): HttpResponse
165
    {
166 1
        return ResponseBuilder::asError($api_code)
167 1
            ->withData($data)
168 1
            ->withPlaceholders($placeholders)
169 1
            ->withJsonOptions($json_opts)
170 1
            ->build();
171
    }
172
173
    /**
174
     * @param integer           $api_code      Your API code to be returned with the response object.
175
     * @param object|array|null $data          Array of primitives and supported objects to be returned in 'data' node
176
     *                                         of the JSON response, single supported object or @null if there's no
177
     *                                         to be returned.
178
     * @param integer           $http_code     HTTP code to be used for HttpResponse sent.
179
     * @param array|null        $placeholders  Placeholders passed to Lang::get() for message placeholders
180
     *                                         substitution or @null if none.
181
     * @param integer|null      $json_opts     See http://php.net/manual/en/function.json-encode.php for supported
182
     *                                         options or pass @null to use value from your config (or defaults).
183
     *
184
     * @return HttpResponse
185
     *
186
     * @throws \InvalidArgumentException if http_code is @null
187
     *
188
     * @deprecated Please use Builder class.
189
     */
190 1
    public static function errorWithDataAndHttpCode(int $api_code, $data, int $http_code, array $placeholders = null,
191
                                                    int $json_opts = null): HttpResponse
192
    {
193 1
        return ResponseBuilder::asError($api_code)
194 1
            ->withData($data)
195 1
            ->withHttpCode($http_code)
196 1
            ->withPlaceholders($placeholders)
197 1
            ->withJsonOptions($json_opts)
198 1
            ->build();
199
    }
200
201
    /**
202
     * @param integer    $api_code     Your API code to be returned with the response object.
203
     * @param integer    $http_code    HTTP code to be used for HttpResponse sent or @null
204
     *                                 for default DEFAULT_HTTP_CODE_ERROR.
205
     * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders
206
     *                                 substitution or @null if none.
207
     *
208
     * @return HttpResponse
209
     *
210
     * @throws \InvalidArgumentException if http_code is @null
211
     *
212
     * @deprecated Please use Builder class.
213
     */
214 1
    public static function errorWithHttpCode(int $api_code, int $http_code, array $placeholders = null): HttpResponse
215
    {
216 1
        return ResponseBuilder::asError($api_code)
217 1
            ->withHttpCode($http_code)
218 1
            ->withPlaceholders($placeholders)
219 1
            ->build();
220
    }
221
222
    /**
223
     * @param integer           $api_code  Your API code to be returned with the response object.
224
     * @param string            $message   Custom message to be returned as part of error response
225
     * @param object|array|null $data      Array of primitives and supported objects to be returned in 'data' node
226
     *                                     of the JSON response, single supported object or @null if there's no
227
     *                                     to be returned.
228
     * @param integer|null      $http_code Optional HTTP status code to be used for HttpResponse sent
229
     *                                     or @null for DEFAULT_HTTP_CODE_ERROR
230
     * @param integer|null      $json_opts See http://php.net/manual/en/function.json-encode.php for supported
231
     *                                     options or pass @null to use value from your config (or defaults).
232
     *
233
     * @return HttpResponse
234
     *
235
     * @deprecated Please use Builder class.
236
     */
237 1
    public static function errorWithMessageAndData(int $api_code, string $message, $data,
238
                                                   int $http_code = null, int $json_opts = null): HttpResponse
239
    {
240 1
        return ResponseBuilder::asError($api_code)
241 1
            ->withMessage($message)
242 1
            ->withData($data)
243 1
            ->withHttpCode($http_code)
244 1
            ->withJsonOptions($json_opts)
245 1
            ->build();
246
    }
247
248
    /**
249
     * @param integer           $api_code   Your API code to be returned with the response object.
250
     * @param string            $message    custom message to be returned as part of error response
251
     * @param object|array|null $data       Array of primitives and supported objects to be returned in 'data' node
252
     *                                      of the JSON response, single supported object or @null if there's no
253
     *                                      to be returned.
254
     * @param integer|null      $http_code  HTTP code to be used for HttpResponse sent or @null
255
     *                                      for default DEFAULT_HTTP_CODE_ERROR.
256
     * @param integer|null      $json_opts  See http://php.net/manual/en/function.json-encode.php for supported
257
     *                                      options or pass @null to use value from your config (or defaults).
258
     * @param array|null        $debug_data optional debug data array to be added to returned JSON.
259
     *
260
     * @return HttpResponse
261
     *
262
     * @deprecated Please use Builder class.
263
     *
264
     * @noinspection PhpTooManyParametersInspection
265
     */
266 1
    public static function errorWithMessageAndDataAndDebug(int $api_code, string $message, $data,
267
                                                           int $http_code = null, int $json_opts = null,
268
                                                           array $debug_data = null): HttpResponse
269
    {
270 1
        return ResponseBuilder::asError($api_code)
271 1
            ->withMessage($message)
272 1
            ->withData($data)
273 1
            ->withHttpCode($http_code)
274 1
            ->withJsonOptions($json_opts)
275 1
            ->withDebugData($debug_data)
276 1
            ->build();
277
    }
278
279
    /**
280
     * @param integer      $api_code  Your API code to be returned with the response object.
281
     * @param string       $message   Custom message to be returned as part of error response
282
     * @param integer|null $http_code HTTP code to be used with final response sent or @null
283
     *                                for default DEFAULT_HTTP_CODE_ERROR.
284
     *
285
     * @return HttpResponse
286
     *
287
     * @deprecated Please use Builder class.
288
     */
289 1
    public static function errorWithMessage(int $api_code, string $message, int $http_code = null): HttpResponse
290
    {
291 1
        return ResponseBuilder::asError($api_code)
292 1
            ->withMessage($message)
293 1
            ->withHttpCode($http_code)
294 1
            ->build();
295
    }
296
297
}
298