Completed
Push — master ( ce2be0...7bcb9a )
by Olivier
02:08
created

Invoice::createFromArray()   F

Complexity

Conditions 11
Paths 512

Size

Total Lines 59

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 50
CRAP Score 11.0069

Importance

Changes 0
Metric Value
dl 0
loc 59
ccs 50
cts 52
cp 0.9615
rs 3.9567
c 0
b 0
f 0
cc 11
nc 512
nop 1
crap 11.0069

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This software may be modified and distributed under the terms
7
 * of the MIT license. See the LICENSE file for details.
8
 */
9
10
namespace Shapin\Stripe\Model\Invoice;
11
12
use Shapin\Stripe\Model\ContainsMetadata;
13
use Shapin\Stripe\Model\CreatableFromArray;
14
use Shapin\Stripe\Model\Customer\CustomField;
15
use Shapin\Stripe\Model\Discount\Discount;
16
use Shapin\Stripe\Model\LivemodeTrait;
17
use Shapin\Stripe\Model\MetadataTrait;
18
use Shapin\Stripe\Model\MetadataCollection;
19
use Money\Currency;
20
use Money\Money;
21
22
final class Invoice implements CreatableFromArray, ContainsMetadata
23
{
24
    use LivemodeTrait, MetadataTrait;
25
26
    const BILLING_CHARGE_AUTOMATICALLY = 'charge_automatically';
27
    const BILLING_SEND_INVOICE = 'send_invoice';
28
29
    const BILLING_REASON_SUBSCRIPTION_CYCLE = 'subscription_cycle';
30
    const BILLING_REASON_SUBSCRIPTION_CREATE = 'subscription_create';
31
    const BILLING_REASON_SUBSCRIPTION_UPDATE = 'subscription_update';
32
    const BILLING_REASON_SUBSCRIPTION = 'subscription';
33
    const BILLING_REASON_MANUAL = 'manual';
34
    const BILLING_REASON_UPCOMING = 'upcoming';
35
    const BILLING_REASON_SUBSCRIPTION_THRESHOLD = 'subscription_threshold';
36
37
    const STATUS_DRAFT = 'draft';
38
    const STATUS_OPEN = 'open';
39
    const STATUS_PAID = 'paid';
40
    const STATUS_UNCOLLECTIBLE = 'uncollectible';
41
    const STATUS_VOID = 'void';
42
43
    /**
44
     * @var string
45
     */
46
    private $id;
47
48
    /**
49
     * @var Money
50
     */
51
    private $amountDue;
52
53
    /**
54
     * @var Money
55
     */
56
    private $amountPaid;
57
58
    /**
59
     * @var Money
60
     */
61
    private $amountRemaining;
62
63
    /**
64
     * @var ?Money
65
     */
66
    private $applicationFeeAmount;
67
68
    /**
69
     * @var int
70
     */
71
    private $attemptCount;
72
73
    /**
74
     * @var bool
75
     */
76
    private $attempted;
77
78
    /**
79
     * @var bool
80
     */
81
    private $autoAdvance;
82
83
    /**
84
     * @var string
85
     */
86
    private $billing;
87
88
    /**
89
     * @var string
90
     */
91
    private $billingReason;
92
93
    /**
94
     * @var ?string
95
     */
96
    private $charge;
97
98
    /**
99
     * @var \DateTimeImmutable
100
     */
101
    private $createdAt;
102
103
    /**
104
     * @var Currency
105
     */
106
    private $currency;
107
108
    /**
109
     * @var array
110
     */
111
    private $customFields;
112
113
    /**
114
     * @var string
115
     */
116
    private $customer;
117
118
    /**
119
     * @var ?string
120
     */
121
    private $defaultSource;
122
123
    /**
124
     * @var string
125
     */
126
    private $description;
127
128
    /**
129
     * @var ?Discount
130
     */
131
    private $discount;
132
133
    /**
134
     * @var ?\DateTimeImmutable
135
     */
136
    private $dueAt;
137
138
    /**
139
     * @var ?Money
140
     */
141
    private $endingBalance;
142
143
    /**
144
     * @var ?\DateTimeImmutable
145
     */
146
    private $finalizedAt;
147
148
    /**
149
     * @var ?string
150
     */
151
    private $footer;
152
153
    /**
154
     * @var ?string
155
     */
156
    private $hostedInvoiceUrl;
157
158
    /**
159
     * @var ?string
160
     */
161
    private $invoicePdf;
162
163
    /**
164
     * @var LineItemCollection
165
     */
166
    private $lines;
167
168
    /**
169
     * @var ?\DateTimeImmutable
170
     */
171
    private $nextPaymentAttempt;
172
173
    /**
174
     * @var string
175
     */
176
    private $number;
177
178
    /**
179
     * @var bool
180
     */
181
    private $paid;
182
183
    /**
184
     * @var \DateTimeImmutable
185
     */
186
    private $periodEndAt;
187
188
    /**
189
     * @var \DateTimeImmutable
190
     */
191
    private $periodStartAt;
192
193
    /**
194
     * @var ?string
195
     */
196
    private $receiptNumber;
197
198
    /**
199
     * @var Money
200
     */
201
    private $startingBalance;
202
203
    /**
204
     * @var ?string
205
     */
206
    private $statementDescriptor;
207
208
    /**
209
     * @var ?string
210
     */
211
    private $status;
212
213
    /**
214
     * @var string
215
     */
216
    private $subscription;
217
218
    /**
219
     * @var ?\DateTimeImmutable
220
     */
221
    private $subscriptionProrationAt;
222
223
    /**
224
     * @var Money
225
     */
226
    private $subtotal;
227
228
    /**
229
     * @var Money
230
     */
231
    private $tax;
232
233
    /**
234
     * @var float
235
     */
236
    private $taxPercent;
237
238
    /**
239
     * @var ?ThresholdReason
240
     */
241
    private $thresholdReason;
242
243
    /**
244
     * @var Money
245
     */
246
    private $total;
247
248
    /**
249
     * @var \DateTimeImmutable
250
     */
251
    private $webhooksDeliveredAt;
252
253 12
    public static function createFromArray(array $data): self
254
    {
255 12
        $currency = new Currency(strtoupper($data['currency']));
256
257 12
        $customFields = [];
258 12
        if (isset($data['custom_fields'])) {
259
            foreach ($data['custom_fields'] as $customField) {
260
                $customFields[] = new CustomField($customField['name'], $customField['value']);
261
            }
262
        }
263
264 12
        $model = new self();
265 12
        $model->id = $data['id'];
266 12
        $model->amountDue = new Money($data['amount_due'], $currency);
267 12
        $model->amountPaid = new Money($data['amount_paid'], $currency);
268 12
        $model->amountRemaining = new Money($data['amount_remaining'], $currency);
269 12
        $model->applicationFeeAmount = isset($data['application_fee_amount']) ? new Money($data['application_fee_amount'], $currency) : null;
270 12
        $model->attemptCount = (int) $data['attempt_count'];
271 12
        $model->attempted = (bool) $data['attempted'];
272 12
        $model->autoAdvance = (bool) $data['auto_advance'];
273 12
        $model->billing = $data['billing'];
274 12
        $model->billingReason = $data['billing_reason'];
275 12
        $model->charge = $data['charge'];
276 12
        $model->createdAt = new \DateTimeImmutable('@'.$data['date']);
277 12
        $model->currency = $currency;
278 12
        $model->customFields = $customFields;
279 12
        $model->customer = $data['customer'];
280 12
        $model->defaultSource = $data['default_source'] ?? null;
281 12
        $model->description = $data['description'];
282 12
        $model->discount = isset($data['discount']) ? Discount::createFromArray($data['discount']) : null;
283 12
        $model->dueAt = isset($data['due_at']) ? new \DateTimeImmutable('@'.$data['due_at']) : null;
284 12
        $model->endingBalance = isset($data['ending_balance']) ? new Money($data['ending_balance'], $currency) : null;
285 12
        $model->finalizedAt = isset($data['finalized_at']) ? new \DateTimeImmutable('@'.$data['finalized_at']) : null;
286 12
        $model->footer = $data['footer'] ?? null;
287 12
        $model->hostedInvoiceUrl = $data['hosted_invoice_url'];
288 12
        $model->invoicePdf = $data['invoice_pdf'];
289 12
        $model->lines = LineItemCollection::createFromArray($data['lines']);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Shapin\Stripe\Model\Inv...omArray($data['lines']) of type object<self> is incompatible with the declared type object<Shapin\Stripe\Mod...ice\LineItemCollection> of property $lines.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
290 12
        $model->live = $data['livemode'];
291 12
        $model->metadata = MetadataCollection::createFromArray($data['metadata']);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Shapin\Stripe\Model\Met...rray($data['metadata']) of type object<self> is incompatible with the declared type object<Shapin\Stripe\Model\MetadataCollection> of property $metadata.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
292 12
        $model->nextPaymentAttempt = isset($data['next_payment_attempt']) ? new \DateTimeImmutable('@'.$data['next_payment_attempt']) : null;
293 12
        $model->number = $data['number'];
294 12
        $model->paid = (bool) $data['paid'];
295 12
        $model->periodEndAt = new \DateTimeImmutable('@'.$data['period_end']);
296 12
        $model->periodStartAt = new \DateTimeImmutable('@'.$data['period_start']);
297 12
        $model->receiptNumber = $data['receipt_number'];
298 12
        $model->startingBalance = new Money($data['starting_balance'], $currency);
299 12
        $model->statementDescriptor = $data['statement_descriptor'];
300 12
        $model->status = $data['status'] ?? null;
301 12
        $model->subscription = $data['subscription'];
302 12
        $model->subscriptionProrationAt = isset($data['subscription_proration_at']) ? new \DateTimeImmutable('@'.$data['subscription_proration_at']) : null;
303 12
        $model->subtotal = new Money($data['subtotal'], $currency);
304 12
        $model->tax = new Money($data['tax'], $currency);
305 12
        $model->taxPercent = (float) $data['tax_percent'];
306 12
        $model->thresholdReason = isset($data['threshold_reason']) ? ThresholdReason::createFromArray($data['threshold_reason']) : null;
307 12
        $model->total = new Money($data['total'], $currency);
308 12
        $model->webhooksDeliveredAt = new \DateTimeImmutable('@'.$data['webhooks_delivered_at']);
309
310 12
        return $model;
311
    }
312
313 1
    public function getId(): string
314
    {
315 1
        return $this->id;
316
    }
317
318 1
    public function getAmountDue(): Money
319
    {
320 1
        return $this->amountDue;
321
    }
322
323 1
    public function getAmountPaid(): Money
324
    {
325 1
        return $this->amountPaid;
326
    }
327
328 1
    public function getAmountRemaining(): Money
329
    {
330 1
        return $this->amountRemaining;
331
    }
332
333 1
    public function getApplicationFeeAmount(): ?Money
334
    {
335 1
        return $this->applicationFeeAmount;
336
    }
337
338 1
    public function getAttemptCount(): int
339
    {
340 1
        return $this->attemptCount;
341
    }
342
343 1
    public function isAttempted(): bool
344
    {
345 1
        return $this->attempted;
346
    }
347
348 1
    public function isAutoAdvance(): bool
349
    {
350 1
        return $this->autoAdvance;
351
    }
352
353 1
    public function getBilling(): string
354
    {
355 1
        return $this->billing;
356
    }
357
358 1
    public function getBillingReason(): string
359
    {
360 1
        return $this->billingReason;
361
    }
362
363 1
    public function getCharge(): ?string
364
    {
365 1
        return $this->charge;
366
    }
367
368 1
    public function getCreatedAt(): \DateTimeImmutable
369
    {
370 1
        return $this->createdAt;
371
    }
372
373 1
    public function getCurrency(): Currency
374
    {
375 1
        return $this->currency;
376
    }
377
378 1
    public function getCustomFields(): array
379
    {
380 1
        return $this->customFields;
381
    }
382
383 1
    public function getCustomer(): string
384
    {
385 1
        return $this->customer;
386
    }
387
388 1
    public function getDefaultSource(): ?string
389
    {
390 1
        return $this->defaultSource;
391
    }
392
393 1
    public function getDescription(): ?string
394
    {
395 1
        return $this->description;
396
    }
397
398 1
    public function getDiscount(): ?Discount
399
    {
400 1
        return $this->discount;
401
    }
402
403 1
    public function getDueAt(): ?\DateTimeImmutable
404
    {
405 1
        return $this->dueAt;
406
    }
407
408 1
    public function getEndingBalance(): ?Money
409
    {
410 1
        return $this->endingBalance;
411
    }
412
413 1
    public function getFinalizedAt(): ?\DateTimeImmutable
414
    {
415 1
        return $this->finalizedAt;
416
    }
417
418 1
    public function getFooter(): ?string
419
    {
420 1
        return $this->footer;
421
    }
422
423 1
    public function getHostedInvoiceUrl(): ?string
424
    {
425 1
        return $this->hostedInvoiceUrl;
426
    }
427
428 1
    public function getInvoicePdf(): ?string
429
    {
430 1
        return $this->invoicePdf;
431
    }
432
433 1
    public function getLines(): LineItemCollection
434
    {
435 1
        return $this->lines;
436
    }
437
438 1
    public function getNextPaymentAttempt(): ?\DateTimeImmutable
439
    {
440 1
        return $this->nextPaymentAttempt;
441
    }
442
443 1
    public function getNumber(): string
444
    {
445 1
        return $this->number;
446
    }
447
448 1
    public function isPaid(): bool
449
    {
450 1
        return $this->paid;
451
    }
452
453 1
    public function getPeriodEndAt(): \DateTimeImmutable
454
    {
455 1
        return $this->periodEndAt;
456
    }
457
458 1
    public function getPeriodStartAt(): \DateTimeImmutable
459
    {
460 1
        return $this->periodStartAt;
461
    }
462
463 1
    public function getReceiptNumber(): ?string
464
    {
465 1
        return $this->receiptNumber;
466
    }
467
468 1
    public function getStartingBalance(): Money
469
    {
470 1
        return $this->startingBalance;
471
    }
472
473 1
    public function getStatementDescriptor(): ?string
474
    {
475 1
        return $this->statementDescriptor;
476
    }
477
478 1
    public function getStatus(): ?string
479
    {
480 1
        return $this->status;
481
    }
482
483 1
    public function getSubscription(): ?string
484
    {
485 1
        return $this->subscription;
486
    }
487
488 1
    public function getSubscriptionProrationAt(): ?\DateTimeImmutable
489
    {
490 1
        return $this->subscriptionProrationAt;
491
    }
492
493 1
    public function getSubtotal(): Money
494
    {
495 1
        return $this->subtotal;
496
    }
497
498 1
    public function getTax(): Money
499
    {
500 1
        return $this->tax;
501
    }
502
503 1
    public function getTaxPercent(): float
504
    {
505 1
        return $this->taxPercent;
506
    }
507
508 1
    public function getThresholdReason(): ?ThresholdReason
509
    {
510 1
        return $this->thresholdReason;
511
    }
512
513 1
    public function getTotal(): Money
514
    {
515 1
        return $this->total;
516
    }
517
518 1
    public function getWebhooksDeliveredAt(): \DateTimeImmutable
519
    {
520 1
        return $this->webhooksDeliveredAt;
521
    }
522
}
523