CustomerFactory::fromArray()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 9
ccs 0
cts 6
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 3
crap 6
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 1
    public function fromData(eCurringClientInterface $client, array $data): Customer
31
    {
32 1
        return Customer::fromData(
33 1
            $this->extractInteger('id', $data),
34 1
            $data['attributes']['first_name'],
35 1
            $data['attributes']['last_name'],
36 1
            PaymentMethod::get($data['attributes']['payment_method']),
37 1
            $data['attributes']['email'],
38 1
            new DateTimeImmutable($data['attributes']['created_at']),
39 1
            new DateTimeImmutable($data['attributes']['updated_at']),
40 1
            $this->extractStringOrNull('card_holder', $data['attributes']),
41 1
            $this->extractStringOrNull('card_number', $data['attributes']),
42 1
            $data['attributes']['gender'] === null ? null : Gender::get($data['attributes']['gender']),
43 1
            $this->extractStringOrNull('middle_name', $data['attributes']),
44 1
            $this->extractStringOrNull('company_name', $data['attributes']),
45 1
            $this->extractStringOrNull('vat_number', $data['attributes']),
46 1
            $this->extractStringOrNull('postalcode', $data['attributes']),
47 1
            $this->extractStringOrNull('house_number', $data['attributes']),
48 1
            $this->extractStringOrNull('house_number_add', $data['attributes']),
49 1
            $this->extractStringOrNull('street', $data['attributes']),
50 1
            $this->extractStringOrNull('city', $data['attributes']),
51 1
            $this->extractStringOrNull('country_iso2', $data['attributes']),
52 1
            $this->extractStringOrNull('language', $data['attributes']),
53 1
            $this->extractStringOrNull('telephone', $data['attributes']),
54 1
            $data['attributes']['bank_verification_method'] === null ? null : VerificationMethod::get($data['attributes']['bank_verification_method']),
55 1
            ...$this->getSubscriptionProxies($client, $data['relationships'])
56
        );
57
    }
58
59
    /**
60
     * @return SubscriptionProxy[]
61
     */
62 1
    private function getSubscriptionProxies(eCurringClientInterface $client, array $relationships): array
63
    {
64 1
        if (!isset($relationships['subscriptions'])) {
65
            return [];
66
        }
67
68 1
        $subscriptions = [];
69
70 1
        foreach ($relationships['subscriptions']['data'] as $subscription) {
71 1
            if ($subscription['type'] !== 'subscription') {
72
                continue;
73
            }
74
75 1
            $subscriptions[] = new SubscriptionProxy($client, $subscription['id']);
76
        }
77
78 1
        return $subscriptions;
79
    }
80
}
81