Passed
Push — main ( 31c5fd...5e12f5 )
by Iain
04:53
created

Plan::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
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
    ) {
35
    }
36
37
    public function getName(): string
38
    {
39
        return $this->name;
40
    }
41
42
    public function hasFeature(string $featureName): bool
43
    {
44
        return in_array($featureName, $this->features);
45
    }
46
47
    public function getLimit(LimitableInterface $limitable): int
48
    {
49
        foreach ($this->limits as $name => $limit) {
50
            if ($limitable->getLimitableName() === $name) {
51
                if (!isset($limit['limit'])) {
52
                    throw new ParameterNotSetException('The limit is not set correctly');
53
                }
54
55
                return $limit['limit'];
56
            }
57
        }
58
59
        return -1;
60
    }
61
62
    public function getLimits(): array
63
    {
64
        return $this->limits;
65
    }
66
67
    public function getPriceId(): ?string
68
    {
69
        return $this->priceId;
70
    }
71
72
    public function setPriceId(string $priceId): void
73
    {
74
        $this->priceId = $priceId;
0 ignored issues
show
Bug Best Practice introduced by
The property priceId does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
75
    }
76
77
    /**
78
     * @throws NoPlanPriceFoundException
79
     */
80
    public function getPriceForPaymentSchedule(string $term, string $currency): PlanPrice
81
    {
82
        if (!isset($this->prices[$term][$currency])) {
83
            throw new NoPlanPriceFoundException(sprintf("No currency '%s' found for '%s' schedule found", $currency, $term));
84
        }
85
86
        return new PlanPrice($term, $this->prices[$term][$currency]['amount'], $currency, $this->prices[$term][$currency]['price_id'] ?? null);
87
    }
88
89
    /**
90
     * @return PlanPrice[]
91
     */
92
    public function getPrices(): array
93
    {
94
        $output = [];
95
        foreach ($this->prices as $term => $currencyData) {
96
            foreach ($currencyData as $currency => $data) {
97
                $output[] = new PlanPrice($term, $data['amount'], $currency, $data['price_id'] ?? null);
98
            }
99
        }
100
101
        return $output;
102
    }
103
104
    public function getFeatures(): array
105
    {
106
        return $this->features;
107
    }
108
109
    public function isFree(): bool
110
    {
111
        return $this->isFree;
112
    }
113
114
    public function isPerSeat(): bool
115
    {
116
        return $this->isPerSeat;
117
    }
118
119
    public function getUserCount(): int
120
    {
121
        return $this->userCount;
122
    }
123
124
    public function setPrices(array $prices): void
125
    {
126
        $this->prices = $prices;
127
    }
128
}
129