Completed
Pull Request — master (#29)
by ARCANEDEV
16:51
created

StripeException::__construct()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 22
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 17
c 1
b 0
f 0
nc 1
nop 8
dl 0
loc 22
rs 9.2

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php namespace Arcanedev\Stripe\Exceptions;
2
3
use Exception;
4
5
/**
6
 * Class     StripeException
7
 *
8
 * @package  Arcanedev\Stripe\Bases
9
 * @author   ARCANEDEV <[email protected]>
10
 */
11
abstract class StripeException extends Exception
12
{
13
    /* ------------------------------------------------------------------------------------------------
14
     |  Properties
15
     | ------------------------------------------------------------------------------------------------
16
     */
17
    /**
18
     * Stripe Error type.
19
     *
20
     * @var string|null
21
     */
22
    protected $type;
23
24
    /**
25
     * Stripe Error code.
26
     *
27
     * @var string|null
28
     */
29
    protected $stripeCode;
30
31
    /**
32
     * HTTP Response Body (json).
33
     *
34
     * @var string|null
35
     */
36
    protected $httpBody;
37
38
    /**
39
     * HTTP Response Body (array).
40
     *
41
     * @var array|null
42
     */
43
    protected $jsonBody;
44
45
    /**
46
     * Parameters.
47
     *
48
     * @var array
49
     */
50
    protected $params = [];
51
52
    /**
53
     * Headers.
54
     *
55
     * @var array
56
     */
57
    protected $headers;
58
59
    /**
60
     * Request ID.
61
     *
62
     * @var string
63
     */
64
    protected $requestId;
65
66
    /* ------------------------------------------------------------------------------------------------
67
     |  Constructor
68
     | ------------------------------------------------------------------------------------------------
69
     */
70
    /**
71
     * Create Stripe Exception instance.
72
     *
73
     * @param  string       $message
74
     * @param  int          $code
75
     * @param  string|null  $type
76
     * @param  string|null  $stripeCode
77
     * @param  string|null  $httpBody
78
     * @param  array        $jsonBody
79
     * @param  array        $params
80
     * @param  array        $headers
81
     */
82
    public function __construct(
83
        $message,
84
        $code = 0,
85
        $type = null,
86
        $stripeCode = null,
87
        $httpBody = null,
88
        $jsonBody = [],
89
        $params = [],
90
        $headers = []
91
    ) {
92
        parent::__construct($message, $code);
93
94
        // Stripe Properties
95
        //--------------------------------------
96
        $this->setType($type);
97
        $this->setStripeCode($stripeCode);
98
        $this->setHttpBody($httpBody);
99
        $this->setJsonBody($jsonBody);
100
        $this->setParams($params);
101
        $this->setHeaders($headers);
102
        $this->setRequestId(isset($headers['Request-Id']) ? $headers['Request-Id'] : null);
103
    }
104
105
    /* ------------------------------------------------------------------------------------------------
106
     |  Getters & Setters
107
     | ------------------------------------------------------------------------------------------------
108
     */
109
    /**
110
     * Get Stripe Error type.
111
     *
112
     * @return string
113
     */
114
    public function getType()
115
    {
116
        return $this->type;
117
    }
118
119
    /**
120
     * Set Type.
121
     *
122
     * @param  string|null  $type
123
     *
124
     * @return self
125
     */
126
    private function setType($type)
127
    {
128
        $this->type = $type;
129
130
        return $this;
131
    }
132
133
    /**
134
     * Get Stripe code.
135
     *
136
     * @return string|null
137
     */
138
    public function getStripeCode()
139
    {
140
        return $this->stripeCode;
141
    }
142
143
    /**
144
     * Set Stripe code.
145
     *
146
     * @param  string|null  $stripeCode
147
     *
148
     * @return self
149
     */
150
    private function setStripeCode($stripeCode)
151
    {
152
        $this->stripeCode = $stripeCode;
153
154
        return $this;
155
    }
156
157
    /**
158
     * Get HTTP status code.
159
     *
160
     * @return int
161
     */
162
    public function getHttpStatus()
163
    {
164
        return $this->getCode();
165
    }
166
167
    /**
168
     * Get HTTP Body.
169
     *
170
     * @return string|null
171
     */
172
    public function getHttpBody()
173
    {
174
        return $this->httpBody;
175
    }
176
177
    /**
178
     * Set HTTP Body.
179
     *
180
     * @param  string|null  $httpBody
181
     *
182
     * @return self
183
     */
184
    private function setHttpBody($httpBody)
185
    {
186
        $this->httpBody = $httpBody;
187
188
        return $this;
189
    }
190
191
    /**
192
     * Get Json Body.
193
     *
194
     * @return array|null
195
     */
196
    public function getJsonBody()
197
    {
198
        return $this->jsonBody;
199
    }
200
201
    /**
202
     * Set Json Body.
203
     *
204
     * @param  array|null  $jsonBody
205
     *
206
     * @return self
207
     */
208
    private function setJsonBody($jsonBody)
209
    {
210
        $this->jsonBody = $jsonBody;
211
212
        return $this;
213
    }
214
215
    /**
216
     * Get Parameters.
217
     *
218
     * @return array
219
     */
220
    public function getParams()
221
    {
222
        return $this->params;
223
    }
224
225
    /**
226
     * Set parameters.
227
     *
228
     * @param  array  $params
229
     *
230
     * @return self
231
     */
232
    private function setParams($params)
233
    {
234
        $this->params = $params;
235
236
        return $this;
237
    }
238
239
    /**
240
     * Get the response headers.
241
     *
242
     * @return array
243
     */
244
    public function getHeaders()
245
    {
246
        return $this->headers;
247
    }
248
249
    /**
250
     * Set the response headers.
251
     *
252
     * @param  array  $headers
253
     *
254
     * @return self
255
     */
256
    private function setHeaders($headers)
257
    {
258
        $this->headers = $headers;
259
260
        return $this;
261
    }
262
263
    /**
264
     * Get the Request ID.
265
     *
266
     * @return string|null
267
     */
268
    public function getRequestId()
269
    {
270
        return $this->requestId;
271
    }
272
273
    /**
274
     * Set the Request ID.
275
     *
276
     * @param  string  $requestId
277
     *
278
     * @return $this
279
     */
280
    private function setRequestId($requestId)
281
    {
282
        $this->requestId = $requestId;
283
284
        return $this;
285
    }
286
}
287