1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PH\Component\Core\Model; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
8
|
|
|
use Doctrine\Common\Collections\Collection; |
9
|
|
|
use PH\Component\Core\SubscriptionPurchaseStates; |
10
|
|
|
use PH\Component\Core\SubscriptionPaymentStates; |
11
|
|
|
use PH\Component\Subscription\Model\Subscription as BaseSubscription; |
12
|
|
|
|
13
|
|
|
class Subscription extends BaseSubscription implements SubscriptionInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var Collection|PaymentInterface[] |
17
|
|
|
*/ |
18
|
|
|
protected $payments; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $purchaseState = SubscriptionPurchaseStates::STATE_NEW; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $paymentState = SubscriptionPaymentStates::STATE_NEW; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var null|string |
32
|
|
|
*/ |
33
|
|
|
protected $tokenValue; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var PaymentMethodInterface |
37
|
|
|
*/ |
38
|
|
|
protected $method; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
protected $metadata = []; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Subscription constructor. |
47
|
|
|
*/ |
48
|
|
|
public function __construct() |
49
|
|
|
{ |
50
|
|
|
parent::__construct(); |
51
|
|
|
|
52
|
|
|
$this->payments = new ArrayCollection(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
|
|
public function getPayments(): Collection |
59
|
|
|
{ |
60
|
|
|
return $this->payments; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
|
|
public function setPayments(Collection $payments): void |
67
|
|
|
{ |
68
|
|
|
$this->payments = $payments; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
|
|
public function hasPayments(): bool |
75
|
|
|
{ |
76
|
|
|
return !$this->payments->isEmpty(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
82
|
|
|
public function addPayment(PaymentInterface $payment): void |
83
|
|
|
{ |
84
|
|
|
/** @var PaymentInterface $payment */ |
85
|
|
|
if (!$this->hasPayment($payment)) { |
86
|
|
|
$this->payments->add($payment); |
87
|
|
|
$payment->setSubscription($this); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
|
|
public function removePayment(PaymentInterface $payment): void |
95
|
|
|
{ |
96
|
|
|
/** @var PaymentInterface $payment */ |
97
|
|
|
if ($this->hasPayment($payment)) { |
98
|
|
|
$this->payments->removeElement($payment); |
99
|
|
|
$payment->setSubscription(null); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritdoc} |
105
|
|
|
*/ |
106
|
|
|
public function hasPayment(PaymentInterface $payment): bool |
107
|
|
|
{ |
108
|
|
|
return $this->payments->contains($payment); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param string|null $state |
113
|
|
|
* |
114
|
|
|
* @return PaymentInterface|null |
115
|
|
|
*/ |
116
|
|
|
public function getLastPayment(string $state = null): ?PaymentInterface |
117
|
|
|
{ |
118
|
|
|
if ($this->payments->isEmpty()) { |
119
|
|
|
return null; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$payment = $this->payments->filter(function (PaymentInterface $payment) use ($state) { |
123
|
|
|
return null === $state || $payment->getState() === $state; |
124
|
|
|
})->last(); |
125
|
|
|
|
126
|
|
|
return false !== $payment ? $payment : null; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* {@inheritdoc} |
131
|
|
|
*/ |
132
|
|
|
public function getPurchaseState(): string |
133
|
|
|
{ |
134
|
|
|
return $this->purchaseState; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* {@inheritdoc} |
139
|
|
|
*/ |
140
|
|
|
public function setPurchaseState(string $purchaseState): void |
141
|
|
|
{ |
142
|
|
|
$this->purchaseState = $purchaseState; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* {@inheritdoc} |
147
|
|
|
*/ |
148
|
|
|
public function getPaymentState(): string |
149
|
|
|
{ |
150
|
|
|
return $this->paymentState; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* {@inheritdoc} |
155
|
|
|
*/ |
156
|
|
|
public function setPaymentState(string $paymentState): void |
157
|
|
|
{ |
158
|
|
|
$this->paymentState = $paymentState; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* {@inheritdoc} |
163
|
|
|
*/ |
164
|
|
|
public function getTokenValue(): ?string |
165
|
|
|
{ |
166
|
|
|
return $this->tokenValue; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* {@inheritdoc} |
171
|
|
|
*/ |
172
|
|
|
public function setTokenValue(string $tokenValue): void |
173
|
|
|
{ |
174
|
|
|
$this->tokenValue = $tokenValue; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* {@inheritdoc} |
179
|
|
|
*/ |
180
|
|
|
public function getMethod(): ?PaymentMethodInterface |
181
|
|
|
{ |
182
|
|
|
return $this->method; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* {@inheritdoc} |
187
|
|
|
*/ |
188
|
|
|
public function setMethod(?PaymentMethodInterface $method): void |
189
|
|
|
{ |
190
|
|
|
$this->method = $method; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* {@inheritdoc} |
195
|
|
|
*/ |
196
|
|
|
public function getMetadata(): array |
197
|
|
|
{ |
198
|
|
|
return $this->metadata; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* {@inheritdoc} |
203
|
|
|
*/ |
204
|
|
|
public function setMetadata(array $metadata): void |
205
|
|
|
{ |
206
|
|
|
$this->metadata = $metadata; |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|