|
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\Product; |
|
11
|
|
|
use LauLamanApps\eCurring\Resource\Product\AuthenticationMethod; |
|
12
|
|
|
use LauLamanApps\eCurring\Resource\Product\Status; |
|
13
|
|
|
use LauLamanApps\eCurring\Resource\ProductCollection; |
|
14
|
|
|
use LauLamanApps\eCurring\Resource\Proxy\SubscriptionProxy; |
|
15
|
|
|
|
|
16
|
|
|
final class ProductFactory extends AbstractFactory implements ProductFactoryInterface |
|
17
|
|
|
{ |
|
18
|
|
|
public function fromArray(eCurringClientInterface $client, array $data, Pagination $page): ProductCollection |
|
19
|
|
|
{ |
|
20
|
|
|
$transactions = []; |
|
21
|
|
|
foreach ($data['data'] as $data) { |
|
22
|
|
|
$transactions[] = $this->fromData($client, $data); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
$totalPages = $data['meta']['total']; |
|
26
|
|
|
|
|
27
|
|
|
return new ProductCollection($client, $page->getNumber(), $totalPages ?? 1, $transactions, $page->getSize()); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
2 |
|
public function fromData(eCurringClientInterface $client, array $data): Product |
|
31
|
|
|
{ |
|
32
|
2 |
|
return new Product( |
|
33
|
2 |
|
$this->extractInteger('id', $data), |
|
34
|
2 |
|
$data['attributes']['name'], |
|
35
|
2 |
|
$data['attributes']['description'], |
|
36
|
2 |
|
new DateTimeImmutable($data['attributes']['start_date']), |
|
37
|
2 |
|
Status::get($data['attributes']['status']), |
|
38
|
2 |
|
AuthenticationMethod::get($data['attributes']['mandate_authentication_method']), |
|
39
|
2 |
|
$this->extractBoolean('send_invoice', $data['attributes']), |
|
40
|
2 |
|
$this->extractInteger('storno_retries', $data['attributes']), |
|
41
|
2 |
|
new DateTimeImmutable($data['attributes']['created_at']), |
|
42
|
2 |
|
new DateTimeImmutable($data['attributes']['updated_at']), |
|
43
|
2 |
|
$this->extractStringOrNull('terms', $data['attributes']), |
|
44
|
2 |
|
...$this->getSubscriptionProxies($client, $data['relationships']) |
|
45
|
|
|
); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @return SubscriptionProxy[] |
|
50
|
|
|
*/ |
|
51
|
2 |
|
private function getSubscriptionProxies(eCurringClientInterface $client, array $relationships): array |
|
52
|
|
|
{ |
|
53
|
2 |
|
if (!isset($relationships['subscriptions'])) { |
|
54
|
|
|
return []; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
2 |
|
$subscriptions = []; |
|
58
|
|
|
|
|
59
|
2 |
|
foreach ($relationships['subscriptions']['data'] as $subscription) { |
|
60
|
1 |
|
if ($subscription['type'] !== 'subscription') { |
|
61
|
|
|
continue; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
1 |
|
$subscriptions[] = new SubscriptionProxy($client, $subscription['id']); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
2 |
|
return $subscriptions; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|