Passed
Push — main ( b974be...b34ffe )
by Iain
04:12
created

SubscriptionFactory::createSubscriptionWithPrice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 3
dl 0
loc 9
rs 10
c 0
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\Obol;
16
17
use Obol\Model\BillingDetails;
18
use Obol\Model\Subscription;
19
use Parthenon\Billing\Entity\Price;
20
use Parthenon\Billing\Entity\SubscriptionPlan;
21
use Parthenon\Billing\Plan\Plan;
22
use Parthenon\Billing\Plan\PlanPrice;
23
use Parthenon\Billing\Repository\PaymentDetailsRepositoryInterface;
24
25
class SubscriptionFactory implements SubscriptionFactoryInterface
26
{
27
    public function __construct(
28
        private BillingDetailsFactoryInterface $billingDetailsFactory,
29
        private PaymentDetailsRepositoryInterface $paymentDetailsRepository,
30
    ) {
31
    }
32
33
    public function createSubscription(
34
        BillingDetails $billingDetails,
35
        SubscriptionPlan|Plan $plan,
36
        PlanPrice|Price $planPrice,
37
        int $seatNumbers,
38
        bool $enforceTrial = true
39
    ): Subscription {
40
        $obolSubscription = new \Obol\Model\Subscription();
41
        $obolSubscription->setBillingDetails($billingDetails);
42
        $obolSubscription->setSeats($seatNumbers);
43
        $obolSubscription->setCostPerSeat($planPrice->getAsMoney());
44
45
        if ($enforceTrial) {
46
            $obolSubscription->setHasTrial($plan->getHasTrial());
47
            $obolSubscription->setTrialLengthDays($plan->getTrialLengthDays());
48
        }
49
50
        if ($planPrice instanceof Price) {
51
            $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

51
            $obolSubscription->setPriceId(/** @scrutinizer ignore-type */ $planPrice->getExternalReference());
Loading history...
52
        } elseif ($planPrice->hasPriceId()) {
53
            $obolSubscription->setPriceId($planPrice->getPriceId());
54
        }
55
56
        return $obolSubscription;
57
    }
58
59
    public function createSubscriptionFromEntity(\Parthenon\Billing\Entity\Subscription $subscription): Subscription
60
    {
61
        $paymentDetails = $this->paymentDetailsRepository->getPaymentDetailsForCustomerAndReference($subscription->getCustomer(), $subscription->getPaymentExternalReference());
62
        $billingDetails = $this->billingDetailsFactory->createFromCustomerAndPaymentDetails($subscription->getCustomer(), $paymentDetails);
63
64
        $obolSubscription = new \Obol\Model\Subscription();
65
        $obolSubscription->setBillingDetails($billingDetails);
66
        $obolSubscription->setId($subscription->getMainExternalReference());
67
        $obolSubscription->setLineId($subscription->getChildExternalReference());
68
        $obolSubscription->setCostPerSeat($subscription->getMoneyAmount());
69
        $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

69
        $obolSubscription->setSeats(/** @scrutinizer ignore-type */ $subscription->getSeats());
Loading history...
70
        $obolSubscription->setHasTrial($subscription->getSubscriptionPlan()->getHasTrial());
71
        $obolSubscription->setTrialLengthDays($subscription->getSubscriptionPlan()->getTrialLengthDays());
72
73
        return $obolSubscription;
74
    }
75
}
76