Passed
Push — main ( b21561...f38fd7 )
by Iain
09:13 queued 04:17
created

BillaBearPlanManager::convertPlan()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 14
rs 9.8666
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * Copyright (C) 2020-2024 Iain Cambridge
7
 *
8
 *     This program is free software: you can redistribute it and/or modify
9
 *     it under the terms of the GNU General Public License as published by
10
 *     the Free Software Foundation, either version 3 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 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\BillaBear;
23
24
use BillaBear\Model\Feature;
0 ignored issues
show
Bug introduced by
The type BillaBear\Model\Feature was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
use BillaBear\Model\Limit;
0 ignored issues
show
Bug introduced by
The type BillaBear\Model\Limit was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
use BillaBear\Model\Price;
0 ignored issues
show
Bug introduced by
The type BillaBear\Model\Price was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
use BillaBear\Model\SubscriptionPlan;
0 ignored issues
show
Bug introduced by
The type BillaBear\Model\SubscriptionPlan was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
28
use Doctrine\Common\Collections\Collection;
29
use Parthenon\Billing\Exception\NoPlanFoundException;
30
use Parthenon\Billing\Plan\LimitedUserInterface;
31
use Parthenon\Billing\Plan\Plan;
32
use Parthenon\Billing\Plan\PlanManagerInterface;
33
34
final class BillaBearPlanManager implements PlanManagerInterface
35
{
36
    private ?array $plans = null;
37
38
    public function __construct(
39
        private SdkFactory $sdkFactory,
40
    ) {
41
    }
42
43
    public function getPlans(): array
44
    {
45
        if (!isset($this->plans)) {
46
            $data = $this->sdkFactory->createSubscriptionsApi()->listSubscriptionPlans(limit: 100);
47
48
            $output = [];
49
            foreach ($data->getData() as $planEntity) {
50
                $output[] = $this->convertPlan($planEntity);
51
            }
52
            $this->plans = $output;
53
        }
54
55
        return $output;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $output does not seem to be defined for all execution paths leading up to this point.
Loading history...
56
    }
57
58
    public function getPlanForUser(LimitedUserInterface $limitedUser): Plan
59
    {
60
        return $this->getPlanByName($limitedUser->getPlanName());
0 ignored issues
show
Bug introduced by
It seems like $limitedUser->getPlanName() can also be of type null; however, parameter $planName of Parthenon\Billing\BillaB...anager::getPlanByName() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

60
        return $this->getPlanByName(/** @scrutinizer ignore-type */ $limitedUser->getPlanName());
Loading history...
61
    }
62
63
    public function getPlanByName(string $planName): Plan
64
    {
65
        $plans = $this->getPlans();
66
67
        foreach ($plans as $plan) {
68
            if ($plan->getName() === $planName) {
69
                return $plan;
70
            }
71
        }
72
        throw new NoPlanFoundException();
73
    }
74
75
    protected function convertPlan(SubscriptionPlan $subscriptionPlan): Plan
76
    {
77
        return new Plan(
78
            $subscriptionPlan->getName(),
79
            $this->convertLimits($subscriptionPlan->getLimits()),
80
            $this->convertFeatures($subscriptionPlan->getFeatures()),
81
            $this->convertPrices($subscriptionPlan->getPrices()),
82
            $subscriptionPlan->getFree(),
83
            $subscriptionPlan->getPerSeat(),
84
            $subscriptionPlan->getUserCount(),
85
            $subscriptionPlan->getPublic(),
86
            $subscriptionPlan->getHasTrial(),
87
            $subscriptionPlan->getTrialLengthDays(),
88
            $subscriptionPlan->getId()
89
        );
90
    }
91
92
    /**
93
     * @param Limit[] $limits
94
     */
95
    protected function convertLimits(array $limits): array
96
    {
97
        $output = [];
98
99
        foreach ($limits as $limit) {
100
            $output[$limit->getFeature()->getCode()] = [
101
                'name' => $limit->getFeature()->getName(),
102
                'code' => $limit->getFeature()->getCode(),
103
                'limit' => $limit->getLimit(),
104
                'description' => $limit->getFeature()->getDescription(),
105
            ];
106
        }
107
108
        return $output;
109
    }
110
111
    /**
112
     * @param Feature[]|Collection $features
113
     */
114
    protected function convertFeatures(array $features): array
115
    {
116
        $output = [];
117
118
        foreach ($features as $feature) {
119
            $output[$feature->getCode()] = [
120
                'code' => $feature->getCode(),
121
                'name' => $feature->getName(),
122
                'description' => $feature->getDescription(),
123
            ];
124
        }
125
126
        return $output;
127
    }
128
129
    /**
130
     * @param Price[]|Collection $prices
131
     */
132
    protected function convertPrices(array $prices): array
133
    {
134
        $output = [];
135
136
        foreach ($prices as $price) {
137
            $schedule = $price->getSchedule();
138
139
            if (!isset($output[$schedule])) {
140
                $output[$schedule] = [];
141
            }
142
143
            $output[$schedule][$price->getCurrency()] = [
144
                'amount' => (string) $price->getAmount(),
145
                'currency' => $price->getCurrency(),
146
                'price_id' => $price->getExternalReference(),
147
                'public' => $price->getPublic(),
148
                'entity_id' => $price->getId(),
149
            ];
150
        }
151
152
        return $output;
153
    }
154
}
155