|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* Copyright Iain Cambridge 2020-2023. |
|
7
|
|
|
* |
|
8
|
|
|
* Use of this software is governed by the Business Source License included in the LICENSE file and at https://getparthenon.com/docs/next/license. |
|
9
|
|
|
* |
|
10
|
|
|
* Change Date: TBD ( 3 years after 2.2.0 release ) |
|
11
|
|
|
* |
|
12
|
|
|
* On the date above, in accordance with the Business Source License, use of this software will be governed by the open source license specified in the LICENSE file. |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Parthenon\Billing\Plan; |
|
16
|
|
|
|
|
17
|
|
|
use Parthenon\Billing\Exception\NoPlanPriceFoundException; |
|
18
|
|
|
use Parthenon\Common\Exception\ParameterNotSetException; |
|
19
|
|
|
|
|
20
|
|
|
final class Plan |
|
21
|
|
|
{ |
|
22
|
|
|
public const PAY_YEARLY = 'yearly'; |
|
23
|
|
|
public const PAY_MONTHLY = 'monthly'; |
|
24
|
|
|
public const CHECK_FEATURE = 'feature'; |
|
25
|
|
|
|
|
26
|
|
|
public function __construct( |
|
27
|
|
|
private string $name, |
|
28
|
|
|
private array $limits, |
|
29
|
|
|
private array $features, |
|
30
|
|
|
private array $prices, |
|
31
|
|
|
private bool $isFree, |
|
32
|
|
|
private bool $isPerSeat, |
|
33
|
|
|
private int $userCount, |
|
34
|
|
|
private bool $public = false, |
|
35
|
|
|
private ?bool $hasTrial = false, |
|
36
|
|
|
private ?int $trialLengthDays = 0, |
|
37
|
|
|
) { |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function getName(): string |
|
41
|
|
|
{ |
|
42
|
|
|
return $this->name; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function hasFeature(string $featureName): bool |
|
46
|
|
|
{ |
|
47
|
|
|
return in_array($featureName, $this->features); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function getLimit(LimitableInterface $limitable): int |
|
51
|
|
|
{ |
|
52
|
|
|
foreach ($this->limits as $name => $limit) { |
|
53
|
|
|
if ($limitable->getLimitableName() === $name) { |
|
54
|
|
|
if (!isset($limit['limit'])) { |
|
55
|
|
|
throw new ParameterNotSetException('The limit is not set correctly'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $limit['limit']; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return -1; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function getLimits(): array |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->limits; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function getPriceId(): ?string |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->priceId; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function setPriceId(string $priceId): void |
|
76
|
|
|
{ |
|
77
|
|
|
$this->priceId = $priceId; |
|
|
|
|
|
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @throws NoPlanPriceFoundException |
|
82
|
|
|
*/ |
|
83
|
|
|
public function getPriceForPaymentSchedule(string $term, string $currency): PlanPrice |
|
84
|
|
|
{ |
|
85
|
|
|
if (!isset($this->prices[$term][$currency])) { |
|
86
|
|
|
throw new NoPlanPriceFoundException(sprintf("No currency '%s' found for '%s' schedule found", $currency, $term)); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return new PlanPrice($term, $this->prices[$term][$currency]['amount'], $currency, $this->prices[$term][$currency]['price_id'] ?? null); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @return PlanPrice[] |
|
94
|
|
|
*/ |
|
95
|
|
|
public function getPublicPrices(): array |
|
96
|
|
|
{ |
|
97
|
|
|
$output = []; |
|
98
|
|
|
foreach ($this->prices as $term => $currencyData) { |
|
99
|
|
|
foreach ($currencyData as $currency => $data) { |
|
100
|
|
|
if (!$data['public']) { |
|
101
|
|
|
continue; |
|
102
|
|
|
} |
|
103
|
|
|
$output[] = new PlanPrice($term, $data['amount'], $currency, $data['price_id'] ?? null); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $output; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function getFeatures(): array |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->features; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function isFree(): bool |
|
116
|
|
|
{ |
|
117
|
|
|
return $this->isFree; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function isPerSeat(): bool |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->isPerSeat; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function getUserCount(): int |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->userCount; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function setPrices(array $prices): void |
|
131
|
|
|
{ |
|
132
|
|
|
$this->prices = $prices; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function isPublic(): bool |
|
136
|
|
|
{ |
|
137
|
|
|
return $this->public; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
public function getHasTrial(): ?bool |
|
141
|
|
|
{ |
|
142
|
|
|
return $this->hasTrial; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
public function setHasTrial(?bool $hasTrial): void |
|
146
|
|
|
{ |
|
147
|
|
|
$this->hasTrial = $hasTrial; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
public function getTrialLengthDays(): ?int |
|
151
|
|
|
{ |
|
152
|
|
|
return $this->trialLengthDays; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
public function setTrialLengthDays(?int $trialLengthDays): void |
|
156
|
|
|
{ |
|
157
|
|
|
$this->trialLengthDays = $trialLengthDays; |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|