SubscriptionFactory   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 24
c 2
b 0
f 0
dl 0
loc 49
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createSubscription() 0 22 3
A createSubscriptionFromEntity() 0 18 2
A __construct() 0 3 1
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\Billing\Obol;
23
24
use Obol\Model\BillingDetails;
25
use Obol\Model\Subscription;
26
use Parthenon\Billing\Entity\Price;
27
use Parthenon\Billing\Plan\PlanPrice;
28
29
class SubscriptionFactory implements SubscriptionFactoryInterface
30
{
31
    public function __construct(
32
        private BillingDetailsFactoryInterface $billingDetailsFactory,
33
    ) {
34
    }
35
36
    public function createSubscription(
37
        BillingDetails $billingDetails,
38
        PlanPrice|Price $planPrice,
39
        int $seatNumbers,
40
        bool $hasTrial = false,
41
        int $trialLengthDays = 0,
42
    ): Subscription {
43
        $obolSubscription = new Subscription();
44
        $obolSubscription->setBillingDetails($billingDetails);
45
        $obolSubscription->setSeats($seatNumbers);
46
        $obolSubscription->setCostPerSeat($planPrice->getAsMoney());
47
48
        $obolSubscription->setHasTrial($hasTrial);
49
        $obolSubscription->setTrialLengthDays($trialLengthDays);
50
51
        if ($planPrice instanceof Price) {
52
            $obolSubscription->setPriceId($planPrice->getExternalReference());
0 ignored issues
show
Bug introduced by
It seems like $planPrice->getExternalReference() can also be of type null; however, parameter $priceId of Obol\Model\Subscription::setPriceId() 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

52
            $obolSubscription->setPriceId(/** @scrutinizer ignore-type */ $planPrice->getExternalReference());
Loading history...
53
        } elseif ($planPrice->hasPriceId()) {
54
            $obolSubscription->setPriceId($planPrice->getPriceId());
55
        }
56
57
        return $obolSubscription;
58
    }
59
60
    public function createSubscriptionFromEntity(\Parthenon\Billing\Entity\Subscription $subscription, bool $fullyBuilt = true): Subscription
61
    {
62
        $obolSubscription = new Subscription();
63
64
        if ($fullyBuilt) {
65
            $paymentDetails = $subscription->getPaymentDetails();
66
            $billingDetails = $this->billingDetailsFactory->createFromCustomerAndPaymentDetails($subscription->getCustomer(), $paymentDetails);
0 ignored issues
show
Bug introduced by
It seems like $paymentDetails can also be of type null; however, parameter $paymentDetails of Parthenon\Billing\Obol\B...omerAndPaymentDetails() does only seem to accept Parthenon\Billing\Entity\PaymentCard, 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

66
            $billingDetails = $this->billingDetailsFactory->createFromCustomerAndPaymentDetails($subscription->getCustomer(), /** @scrutinizer ignore-type */ $paymentDetails);
Loading history...
67
            $obolSubscription->setBillingDetails($billingDetails);
68
        }
69
70
        $obolSubscription->setId($subscription->getMainExternalReference());
0 ignored issues
show
Bug introduced by
It seems like $subscription->getMainExternalReference() can also be of type null; however, parameter $id of Obol\Model\Subscription::setId() 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

70
        $obolSubscription->setId(/** @scrutinizer ignore-type */ $subscription->getMainExternalReference());
Loading history...
71
        $obolSubscription->setLineId($subscription->getChildExternalReference());
72
        $obolSubscription->setCostPerSeat($subscription->getMoneyAmount());
73
        $obolSubscription->setSeats($subscription->getSeats());
0 ignored issues
show
Bug introduced by
It seems like $subscription->getSeats() can also be of type null; however, parameter $seats of Obol\Model\Subscription::setSeats() does only seem to accept integer, 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

73
        $obolSubscription->setSeats(/** @scrutinizer ignore-type */ $subscription->getSeats());
Loading history...
74
        $obolSubscription->setHasTrial($subscription->getSubscriptionPlan()?->getHasTrial() ?? false);
75
        $obolSubscription->setTrialLengthDays($subscription->getSubscriptionPlan()?->getTrialLengthDays() ?? 0);
76
77
        return $obolSubscription;
78
    }
79
}
80