Passed
Push — main ( 6e6ff6...082362 )
by Iain
05:02
created

DatabasePlanManager::convertPlan()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 14
rs 9.9332
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 Doctrine\Common\Collections\Collection;
18
use Parthenon\Billing\Entity\SubscriptionPlan;
19
use Parthenon\Billing\Entity\SubscriptionPlanLimit;
20
use Parthenon\Billing\Repository\SubscriptionPlanRepositoryInterface;
21
22
class DatabasePlanManager implements PlanManagerInterface
23
{
24
    public function __construct(private SubscriptionPlanRepositoryInterface $subscriptionPlanRepository)
25
    {
26
    }
27
28
    public function getPlans(): array
29
    {
30
        $planEntities = $this->subscriptionPlanRepository->getAll();
31
32
        $output = [];
33
        foreach ($planEntities as $planEntity) {
34
            $output[] = $this->convertPlan($planEntity);
35
        }
36
37
        return $output;
38
    }
39
40
    public function getPlanForUser(LimitedUserInterface $limitedUser): Plan
41
    {
42
        // TODO: Implement getPlanForUser() method.
43
    }
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return Parthenon\Billing\Plan\Plan. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
44
45
    public function getPlanByName(string $planName): Plan
46
    {
47
        // TODO: Implement getPlanByName() method.
48
    }
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return Parthenon\Billing\Plan\Plan. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
49
50
    protected function convertPlan(SubscriptionPlan $subscriptionPlan): Plan
51
    {
52
        $plan = new Plan(
53
            $subscriptionPlan->getName(),
54
            $this->convertLimits($subscriptionPlan->getLimits()),
55
            $this->convertFeatures(),
56
            $this->convertPrices(),
57
            $subscriptionPlan->isFree(),
58
            $subscriptionPlan->isPerSeat(),
59
            $subscriptionPlan->getUserCount(),
60
            $subscriptionPlan->isPublic(),
61
        );
62
63
        return $plan;
64
    }
65
66
    /**
67
     * @param SubscriptionPlanLimit[] $limits
68
     */
69
    protected function convertLimits(array|Collection $limits): array
70
    {
71
        $output = [];
72
73
        foreach ($limits as $limit) {
74
            $output[$limit->getSubscriptionLimit()->getCode()] = [
75
                'name' => $limit->getSubscriptionLimit()->getName(),
76
                'code' => $limit->getSubscriptionLimit()->getCode(),
77
                'limit' => $limit->getLimit(),
78
                'description' => $limit->getSubscriptionLimit()->getDescription(),
79
            ];
80
        }
81
82
        return $output;
83
    }
84
85
    protected function convertFeatures(): array
86
    {
87
        $output = [];
88
89
        return $output;
90
    }
91
92
    protected function convertPrices(): array
93
    {
94
        $output = [];
95
96
        return $output;
97
    }
98
}
99