|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Terox\SubscriptionBundle\Strategy; |
|
4
|
|
|
|
|
5
|
|
|
use Terox\SubscriptionBundle\Exception\PermanentSubscriptionException; |
|
6
|
|
|
use Terox\SubscriptionBundle\Model\ProductInterface; |
|
7
|
|
|
use Terox\SubscriptionBundle\Model\SubscriptionInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* End Last Subscription Strategy. |
|
11
|
|
|
* |
|
12
|
|
|
* Starts a new subscription at the end of the latest if there isn't any permanent subscription with the current |
|
13
|
|
|
* product. |
|
14
|
|
|
*/ |
|
15
|
|
|
class SubscriptionEndLastStrategy extends AbstractSubscriptionStrategy |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* {@inheritdoc} |
|
19
|
|
|
*/ |
|
20
|
12 |
|
public function createSubscription(ProductInterface $product, array $subscriptions = []) |
|
21
|
|
|
{ |
|
22
|
12 |
|
if (empty($subscriptions)) { |
|
23
|
4 |
|
return $this->create($this->createCurrentDate(), $product); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
8 |
|
$startDate = null; |
|
27
|
8 |
|
foreach ($subscriptions as $subscription) { |
|
28
|
|
|
|
|
29
|
|
|
// Subscription is permanent, don't continue |
|
30
|
8 |
|
if (null === $subscription->getEndDate()) { |
|
31
|
4 |
|
$startDate = null; |
|
32
|
4 |
|
break; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
// Catch the subscription with higher end date |
|
36
|
5 |
|
if (null === $startDate || $startDate < $subscription->getEndDate()) { |
|
37
|
5 |
|
$startDate = $subscription->getEndDate(); |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
// It's a permanent subscription |
|
42
|
8 |
|
if (null === $startDate) { |
|
43
|
|
|
|
|
44
|
4 |
|
if (count($subscriptions) > 1) { |
|
45
|
2 |
|
throw new PermanentSubscriptionException( |
|
46
|
2 |
|
'More than one subscription per product is not allowed when there is a permanent subscription |
|
47
|
|
|
enabled. Maybe you are mixing different strategies?' |
|
48
|
|
|
); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
2 |
|
return $subscriptions[0]; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
// Check if subscription is expired |
|
55
|
4 |
|
if (time() > $startDate->getTimestamp()) { |
|
56
|
1 |
|
$startDate = $this->createCurrentDate(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
// Date should use the \DateTimeImmutable (a little fix) |
|
60
|
4 |
|
if (!$startDate instanceof \DateTimeImmutable) { |
|
61
|
|
|
$startDate = (new \DateTimeImmutable())->setTimestamp($startDate->getTimestamp()); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
4 |
|
return $this->create($startDate, $product); |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Create subscription. |
|
69
|
|
|
* |
|
70
|
|
|
* @param \DateTimeImmutable $startDate |
|
71
|
|
|
* @param ProductInterface $product |
|
72
|
|
|
* |
|
73
|
|
|
* @return SubscriptionInterface |
|
74
|
|
|
*/ |
|
75
|
8 |
|
private function create($startDate, $product) |
|
76
|
|
|
{ |
|
77
|
8 |
|
$endDate = null !== $product->getDuration() ? |
|
78
|
8 |
|
$startDate->modify(sprintf('+%s seconds', $product->getDuration())) : null; |
|
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
// Create the new subscription |
|
81
|
8 |
|
$subscription = $this->createSubscriptionInstance(); |
|
82
|
8 |
|
$subscription->setProduct($product); |
|
83
|
8 |
|
$subscription->setStartDate($startDate); |
|
84
|
8 |
|
$subscription->setEndDate($endDate); |
|
85
|
8 |
|
$subscription->setAutoRenewal($product->isAutoRenewal()); |
|
86
|
|
|
|
|
87
|
8 |
|
return $subscription; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|