PlanManager   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 53
rs 10
c 0
b 0
f 0
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getPlanForUser() 0 10 3
A getPlans() 0 3 1
A __construct() 0 14 2
A getPlanByName() 0 9 3
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\Payments\Plan;
23
24
use Parthenon\Payments\Exception\NoPlanFoundException;
25
use Parthenon\Payments\Repository\SubscriberRepositoryInterface;
26
27
final class PlanManager implements PlanManagerInterface
28
{
29
    /**
30
     * @var Plan[]
31
     */
32
    private array $plans = [];
33
34
    public function __construct(array $plans, private SubscriberRepositoryInterface $subscriberRepository)
35
    {
36
        foreach ($plans as $planName => $planInfo) {
37
            $plan = new Plan(
38
                $planName,
39
                $planInfo['limit'],
40
                $planInfo['features'] ?? [],
41
                $planInfo['yearly_price_id'] ?? '',
42
                $planInfo['monthly_price_id'] ?? '',
43
                $planInfo['is_free'] ?? false,
44
                $planInfo['is_per_seat'] ?? false,
45
                $planInfo['user_count'] ?? 1
46
            );
47
            $this->plans[] = $plan;
48
        }
49
    }
50
51
    /**
52
     * @return Plan[]
53
     */
54
    public function getPlans(): array
55
    {
56
        return $this->plans;
57
    }
58
59
    public function getPlanForUser(LimitedUserInterface $limitedUser): Plan
60
    {
61
        $subscription = $this->subscriberRepository->getSubscriptionForUser($limitedUser);
62
        foreach ($this->plans as $plan) {
63
            if ($plan->getName() === $subscription->getPlanName()) {
64
                return $plan;
65
            }
66
        }
67
68
        throw new NoPlanFoundException(sprintf("No plan '%s' found for user", $limitedUser->getPlanName()));
69
    }
70
71
    public function getPlanByName(string $planName): Plan
72
    {
73
        foreach ($this->plans as $plan) {
74
            if ($plan->getName() === $planName) {
75
                return $plan;
76
            }
77
        }
78
79
        throw new NoPlanFoundException(sprintf("No plan '%s' found for user", $planName));
80
    }
81
}
82