1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* Copyright (C) 2020-2025 Iain Cambridge |
7
|
|
|
* |
8
|
|
|
* This program is free software: you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as published by |
10
|
|
|
* the Free Software Foundation, either version 2.1 of the License, or |
11
|
|
|
* (at your option) any later version. |
12
|
|
|
* |
13
|
|
|
* This program is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
* GNU Lesser General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU General Public License |
19
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace Parthenon\Billing\Plan; |
23
|
|
|
|
24
|
|
|
use Parthenon\Billing\Exception\NoPlanFoundException; |
25
|
|
|
use Parthenon\Billing\Repository\CustomerRepositoryInterface; |
26
|
|
|
|
27
|
|
|
final class PlanManager implements PlanManagerInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var Plan[] |
31
|
|
|
*/ |
32
|
|
|
private array $plans = []; |
33
|
|
|
|
34
|
|
|
public function __construct(array $rawPlans, private CustomerRepositoryInterface $customerRepository) |
35
|
|
|
{ |
36
|
|
|
foreach ($rawPlans as $planName => $planInfo) { |
37
|
|
|
$plan = new Plan( |
38
|
|
|
$planName, |
39
|
|
|
$planInfo['limit'], |
40
|
|
|
$planInfo['features'] ?? [], |
41
|
|
|
$planInfo['prices'] ?? [], |
42
|
|
|
$planInfo['is_free'] ?? false, |
43
|
|
|
$planInfo['is_per_seat'] ?? false, |
44
|
|
|
$planInfo['user_count'] ?? 1, |
45
|
|
|
$planInfo['public'] ?? true, |
46
|
|
|
$planInfo['has_trial'] ?? false, |
47
|
|
|
$planName['trial_length_days'] ?? 0, |
48
|
|
|
); |
49
|
|
|
$this->plans[] = $plan; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return Plan[] |
55
|
|
|
*/ |
56
|
|
|
public function getPlans(): array |
57
|
|
|
{ |
58
|
|
|
return $this->plans; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function getPlanForUser(LimitedUserInterface $limitedUser): Plan |
62
|
|
|
{ |
63
|
|
|
$subscription = $this->customerRepository->getSubscriptionForUser($limitedUser); |
|
|
|
|
64
|
|
|
|
65
|
|
|
foreach ($this->plans as $plan) { |
66
|
|
|
if ($plan->getName() === $subscription->getPlanName()) { |
67
|
|
|
return $plan; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
throw new NoPlanFoundException(sprintf("No plan '%s' found for user", $limitedUser->getPlanName())); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function getPlanByName(string $planName): Plan |
75
|
|
|
{ |
76
|
|
|
foreach ($this->plans as $plan) { |
77
|
|
|
if ($plan->getName() === $planName) { |
78
|
|
|
return $plan; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
throw new NoPlanFoundException(sprintf("No plan '%s' found for user", $planName)); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.