|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace LauLamanApps\eCurring\Resource\Factory; |
|
6
|
|
|
|
|
7
|
|
|
use DateTimeImmutable; |
|
8
|
|
|
use InvalidArgumentException; |
|
9
|
|
|
use LauLamanApps\eCurring\eCurringClientInterface; |
|
10
|
|
|
use LauLamanApps\eCurring\Resource\Curser\Pagination; |
|
11
|
|
|
use LauLamanApps\eCurring\Resource\Proxy\CustomerProxy; |
|
12
|
|
|
use LauLamanApps\eCurring\Resource\Proxy\SubscriptionPlanProxy; |
|
13
|
|
|
use LauLamanApps\eCurring\Resource\Proxy\TransactionProxy; |
|
14
|
|
|
use LauLamanApps\eCurring\Resource\Subscription; |
|
15
|
|
|
use LauLamanApps\eCurring\Resource\Subscription\Mandate; |
|
16
|
|
|
use LauLamanApps\eCurring\Resource\Subscription\Status; |
|
17
|
|
|
use LauLamanApps\eCurring\Resource\SubscriptionCollection; |
|
18
|
|
|
|
|
19
|
|
|
final class SubscriptionFactory extends AbstractFactory implements SubscriptionFactoryInterface |
|
20
|
|
|
{ |
|
21
|
|
|
public function fromArray(eCurringClientInterface $client, array $data, Pagination $page): SubscriptionCollection |
|
22
|
|
|
{ |
|
23
|
|
|
$subscriptions = []; |
|
24
|
|
|
foreach ($data['data'] as $data) { |
|
25
|
|
|
$subscriptions[] = $this->fromData($data); |
|
|
|
|
|
|
26
|
|
|
} |
|
27
|
|
|
$totalPages = $this->extractInteger('total', $data['meta']); |
|
28
|
|
|
|
|
29
|
|
|
return new SubscriptionCollection($client, $page->getNumber(), $totalPages, $subscriptions); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function fromData(eCurringClientInterface $client, array $data): Subscription |
|
33
|
|
|
{ |
|
34
|
|
|
return Subscription::fromData( |
|
35
|
|
|
$this->extractInteger('id', $data), |
|
36
|
|
|
$this->getMandate($data['attributes']), |
|
37
|
|
|
new DateTimeImmutable($data['attributes']['start_date']), |
|
38
|
|
|
Status::get($data['attributes']['status']), |
|
39
|
|
|
$data['attributes']['confirmation_page'], |
|
40
|
|
|
$this->extractBoolean('confirmation_sent', $data), |
|
41
|
|
|
$this->getCustomerProxy($client, $data['relationships']), |
|
42
|
|
|
$this->getSubscriptionPlanProxy($client, $data['relationships']), |
|
43
|
|
|
new DateTimeImmutable($data['attributes']['created_at']), |
|
44
|
|
|
new DateTimeImmutable($data['attributes']['updated_at']), |
|
45
|
|
|
$this->extractStringOrNull('subscription_webhook_url', $data['attributes']), |
|
46
|
|
|
$this->extractStringOrNull('transaction_webhook_url', $data['attributes']), |
|
47
|
|
|
$this->extractStringOrNull('success_redirect_url', $data['attributes']), |
|
48
|
|
|
$this->extractDateTimeImmutableOrNull('cancel_date', $data['attributes']), |
|
49
|
|
|
$this->extractDateTimeImmutableOrNull('resume_date', $data['attributes']), |
|
50
|
|
|
...$this->getTransactionProxies($client, $data['relationships']) |
|
51
|
|
|
); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
private function getMandate(array $data): Mandate |
|
55
|
|
|
{ |
|
56
|
|
|
return new Mandate( |
|
57
|
|
|
$data['mandate_code'], |
|
58
|
|
|
$this->extractBoolean('mandate_accepted', $data), |
|
59
|
|
|
new DateTimeImmutable($data['mandate_accepted_date']) |
|
60
|
|
|
); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
private function getSubscriptionPlanProxy(eCurringClientInterface $client, array $relationships): SubscriptionPlanProxy |
|
64
|
|
|
{ |
|
65
|
|
|
if (!isset($relationships['subscription-plan'])) { |
|
66
|
|
|
throw new InvalidArgumentException('customer not found in data'); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return new SubscriptionPlanProxy($client, $relationships['subscription-plan']['data']['id']); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
private function getCustomerProxy(eCurringClientInterface $client, array $relationships): CustomerProxy |
|
73
|
|
|
{ |
|
74
|
|
|
if (!isset($relationships['customer'])) { |
|
75
|
|
|
throw new InvalidArgumentException('customer not found in data'); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return new CustomerProxy($client, $relationships['customer']['data']['id']); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @return TransactionProxy[] |
|
83
|
|
|
*/ |
|
84
|
|
|
private function getTransactionProxies(eCurringClientInterface $client, array $transactions): array |
|
85
|
|
|
{ |
|
86
|
|
|
if (!isset($transactions['transactions'])) { |
|
87
|
|
|
return []; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$subscriptions = []; |
|
91
|
|
|
|
|
92
|
|
|
foreach ($transactions['transactions']['data'] as $subscription) { |
|
93
|
|
|
if ($subscription['type'] !== 'transaction') { |
|
94
|
|
|
continue; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$subscriptions[] = new TransactionProxy($client, $subscription['id']); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return $subscriptions; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
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.