Test Failed
Push — master ( 286c96...e21ea4 )
by Laurens
01:29
created

CustomerFactory::fromArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LauLamanApps\eCurring\Resource\Factory;
6
7
use DateTimeImmutable;
8
use LauLamanApps\eCurring\eCurringClientInterface;
9
use LauLamanApps\eCurring\Resource\Curser\Pagination;
10
use LauLamanApps\eCurring\Resource\Customer;
11
use LauLamanApps\eCurring\Resource\Customer\Gender;
12
use LauLamanApps\eCurring\Resource\Customer\VerificationMethod;
13
use LauLamanApps\eCurring\Resource\CustomerCollection;
14
use LauLamanApps\eCurring\Resource\Proxy\SubscriptionProxy;
15
use LauLamanApps\eCurring\Resource\Transaction\PaymentMethod;
16
17
final class CustomerFactory extends AbstractFactory implements CustomerFactoryInterface
18
{
19
    public function fromArray(eCurringClientInterface $client, array $data, Pagination $page): CustomerCollection
20
    {
21
        $subscriptions = [];
22
        foreach ($data['data'] as $data) {
23
            $subscriptions[] = $this->fromData($data);
0 ignored issues
show
Bug introduced by
The call to LauLamanApps\eCurring\Re...omerFactory::fromData() has too few arguments starting with data. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

23
            /** @scrutinizer ignore-call */ 
24
            $subscriptions[] = $this->fromData($data);

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
24
        }
25
        $totalPages = $this->extractInteger('total', $data['meta']);
26
27
        return new CustomerCollection($client, $page->getNumber(), $totalPages, $subscriptions);
28
    }
29
30
    public function fromData(eCurringClientInterface $client, array $data): Customer
31
    {
32
        return Customer::fromData(
33
            $this->extractInteger('id', $data),
34
            $data['attributes']['first_name'],
35
            $data['attributes']['last_name'],
36
            PaymentMethod::get($data['attributes']['payment_method']),
37
            $data['attributes']['card_holder'],
38
            $data['attributes']['card_number'],
39
            $data['attributes']['email'],
40
            new DateTimeImmutable($data['attributes']['created_at']),
41
            new DateTimeImmutable($data['attributes']['updated_at']),
42
            $data['attributes']['updated_at'] === null ? null : Gender::get($data['attributes']['gender']),
43
            $this->extractStringOrNull('middle_name', $data['attributes']),
44
            $this->extractStringOrNull('company_name', $data['attributes']),
45
            $this->extractStringOrNull('vat_number', $data['attributes']),
46
            $this->extractStringOrNull('postalcode', $data['attributes']),
47
            $this->extractStringOrNull('house_number', $data['attributes']),
48
            $this->extractStringOrNull('house_number_add', $data['attributes']),
49
            $this->extractStringOrNull('street', $data['attributes']),
50
            $this->extractStringOrNull('city', $data['attributes']),
51
            $this->extractStringOrNull('country_code', $data['attributes']),
52
            $this->extractStringOrNull('language', $data['attributes']),
53
            $this->extractStringOrNull('telephone', $data['attributes']),
54
            $data['attributes']['bank_verification_method'] === null ? null : VerificationMethod::get($data['attributes']['bank_verification_method']),
55
            ...$this->getSubscriptionProxies($client, $data['relationships'])
56
        );
57
    }
58
59
    /**
60
     * @return SubscriptionProxy[]
61
     */
62
    private function getSubscriptionProxies(eCurringClientInterface $client, array $relationships): array
63
    {
64
        if (!isset($relationships['subscriptions'])) {
65
            return [];
66
        }
67
68
        $subscriptions = [];
69
70
        foreach ($relationships['subscriptions']['data'] as $subscription) {
71
            if ($subscription['type'] !== 'subscription') {
72
                continue;
73
            }
74
75
            $subscriptions[] = new SubscriptionProxy($client, $subscription['id']);
76
        }
77
78
        return $subscriptions;
79
    }
80
}
81