Passed
Push — main ( 241451...172c2c )
by Iain
05:00
created

SubscriptionManager   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 35
c 0
b 0
f 0
dl 0
loc 96
rs 10
wmc 14

8 Methods

Rating   Name   Duplication   Size   Complexity  
A cancelSubscriptionAtEndOfCurrentPeriod() 0 2 1
A cancelSubscriptionOnDate() 0 2 1
A cancelSubscriptionInstantly() 0 2 1
A changeSubscriptionPrice() 0 2 1
A __construct() 0 5 1
A startSubscriptionWithDto() 0 6 1
B startSubscription() 0 53 7
A changeSubscriptionPlan() 0 2 1
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 App\Parthenon\Billing\BillaBear\Subscription;
23
24
use BillaBear\ApiException;
0 ignored issues
show
Bug introduced by
The type BillaBear\ApiException 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\SubscriptionStartBody;
0 ignored issues
show
Bug introduced by
The type BillaBear\Model\SubscriptionStartBody 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 Obol\Model\Enum\ChargeFailureReasons;
27
use Parthenon\Billing\BillaBear\SdkFactory;
28
use Parthenon\Billing\Dto\StartSubscriptionDto;
29
use Parthenon\Billing\Entity\CustomerInterface;
30
use Parthenon\Billing\Entity\PaymentCard;
31
use Parthenon\Billing\Entity\Price;
32
use Parthenon\Billing\Entity\Subscription;
33
use Parthenon\Billing\Entity\SubscriptionPlan;
34
use Parthenon\Billing\Enum\BillingChangeTiming;
35
use Parthenon\Billing\Event\SubscriptionCreated;
36
use Parthenon\Billing\Exception\NoPaymentDetailsException;
37
use Parthenon\Billing\Exception\PaymentFailureException;
38
use Parthenon\Billing\Plan\Plan;
39
use Parthenon\Billing\Plan\PlanManagerInterface;
40
use Parthenon\Billing\Plan\PlanPrice;
41
use Parthenon\Billing\Subscription\SubscriptionManagerInterface;
42
use Parthenon\Common\Exception\GeneralException;
43
use Parthenon\Common\LoggerAwareTrait;
44
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
45
46
class SubscriptionManager implements SubscriptionManagerInterface
47
{
48
    use LoggerAwareTrait;
49
50
    public function __construct(
51
        private SdkFactory $sdkFactory,
52
        private PlanManagerInterface $planManager,
53
        private EventDispatcherInterface $dispatcher,
54
    ) {
55
    }
56
57
    public function startSubscription(
58
        CustomerInterface $customer,
59
        SubscriptionPlan|Plan $plan,
60
        Price|PlanPrice $planPrice,
61
        ?PaymentCard $paymentDetails = null,
62
        int $seatNumbers = 1,
63
        ?bool $hasTrial = null,
64
        ?int $trialLengthDays = 0,
65
    ): Subscription {
66
        if (!$plan instanceof Plan) {
67
            throw new GeneralException('Invalid type of plan given');
68
        }
69
70
        $customerId = $customer->getExternalCustomerReference();
71
        $payload = [
72
            'subscription_plan' => $plan->getEntityId(),
73
            'price' => $planPrice->getEntityId(),
0 ignored issues
show
Bug introduced by
The method getEntityId() does not exist on Parthenon\Billing\Entity\Price. ( Ignorable by Annotation )

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

73
            'price' => $planPrice->/** @scrutinizer ignore-call */ getEntityId(),

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.

Loading history...
74
            'seat_numbers' => $seatNumbers,
75
        ];
76
77
        if ($paymentDetails) {
78
            $payload['payment_details'] = $paymentDetails->getId();
79
        }
80
81
        $subscriptionStart = new SubscriptionStartBody($payload);
82
83
        try {
84
            $response = $this->sdkFactory->createSubscriptionsApi()->customerStartSubscription($subscriptionStart, $customerId);
85
        } catch (ApiException $apiException) {
86
            if (402 === $apiException->getCode()) {
87
                $body = $apiException->getResponseBody();
88
                $json = json_decode($body, true);
89
                throw new PaymentFailureException(ChargeFailureReasons::from($json['reason']), previous: $apiException);
90
            }
91
92
            if (406 === $apiException->getCode()) {
93
                throw new NoPaymentDetailsException('No payment details', previous: $apiException);
94
            }
95
96
            throw new GeneralException(previous: $apiException);
97
        } catch (\Throwable $exception) {
98
            new GeneralException($exception->getMessage(), previous: $exception);
99
        }
100
        $subscription = new Subscription();
101
        $subscription->setCustomer($customer);
102
        $subscription->setId($response->getId());
103
        $subscription->setValidUntil(new \DateTime($response->getValidUntil()));
104
        $subscription->setMainExternalReference($response->getMainExternalReference());
105
        $subscription->setChildExternalReference($response->getChildExternalReference());
106
107
        $this->dispatcher->dispatch(new SubscriptionCreated($subscription), SubscriptionCreated::NAME);
108
109
        return $subscription;
110
    }
111
112
    public function startSubscriptionWithDto(CustomerInterface $customer, StartSubscriptionDto $startSubscriptionDto): Subscription
113
    {
114
        $plan = $this->planManager->getPlanByName($startSubscriptionDto->getPlanName());
115
        $planPrice = $plan->getPriceForPaymentSchedule($startSubscriptionDto->getSchedule(), $startSubscriptionDto->getCurrency());
116
117
        return $this->startSubscription($customer, $plan, $planPrice, null, $startSubscriptionDto->getSeatNumbers());
118
    }
119
120
    public function cancelSubscriptionAtEndOfCurrentPeriod(Subscription $subscription): Subscription
121
    {
122
        // TODO: Implement cancelSubscriptionAtEndOfCurrentPeriod() method.
123
    }
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\Entity\Subscription. 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...
124
125
    public function cancelSubscriptionInstantly(Subscription $subscription): Subscription
126
    {
127
        // TODO: Implement cancelSubscriptionInstantly() method.
128
    }
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\Entity\Subscription. 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...
129
130
    public function cancelSubscriptionOnDate(Subscription $subscription, \DateTimeInterface $dateTime): Subscription
131
    {
132
        // TODO: Implement cancelSubscriptionOnDate() method.
133
    }
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\Entity\Subscription. 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...
134
135
    public function changeSubscriptionPrice(Subscription $subscription, Price $price, BillingChangeTiming $billingChangeTiming): void
136
    {
137
        // TODO: Implement changeSubscriptionPrice() method.
138
    }
139
140
    public function changeSubscriptionPlan(Subscription $subscription, SubscriptionPlan $plan, Price $price, BillingChangeTiming $billingChangeTiming): void
141
    {
142
        // TODO: Implement changeSubscriptionPlan() method.
143
    }
144
}
145