Passed
Push — main ( 3919a2...0705b8 )
by Iain
15:39
created

SubscriptionFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 2
dl 0
loc 4
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\Plan\PlanPrice;
21
use Parthenon\Billing\Repository\PaymentDetailsRepositoryInterface;
22
23
class SubscriptionFactory implements SubscriptionFactoryInterface
24
{
25
    public function __construct(
26
        private BillingDetailsFactoryInterface $billingDetailsFactory,
27
        private PaymentDetailsRepositoryInterface $paymentDetailsRepository,
28
    ) {
29
    }
30
31
    public function createSubscription(
32
        BillingDetails $billingDetails,
33
        PlanPrice $planPrice,
34
        int $seatNumbers,
35
    ): Subscription {
36
        $obolSubscription = new \Obol\Model\Subscription();
37
        $obolSubscription->setBillingDetails($billingDetails);
38
        $obolSubscription->setSeats($seatNumbers);
39
        $obolSubscription->setCostPerSeat($planPrice->getPriceAsMoney());
40
        if ($planPrice->hasPriceId()) {
41
            $obolSubscription->setPriceId($planPrice->getPriceId());
0 ignored issues
show
Bug introduced by
It seems like $planPrice->getPriceId() 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

41
            $obolSubscription->setPriceId(/** @scrutinizer ignore-type */ $planPrice->getPriceId());
Loading history...
42
        }
43
44
        return $obolSubscription;
45
    }
46
47
    public function createSubscriptionWithPrice(BillingDetails $billingDetails, Price $price, int $seatNumbers): Subscription
48
    {
49
        $obolSubscription = new \Obol\Model\Subscription();
50
        $obolSubscription->setBillingDetails($billingDetails);
51
        $obolSubscription->setSeats($seatNumbers);
52
        $obolSubscription->setCostPerSeat($price->getAsMoney());
53
        $obolSubscription->setPriceId($price->getExternalReference());
0 ignored issues
show
Bug introduced by
It seems like $price->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

53
        $obolSubscription->setPriceId(/** @scrutinizer ignore-type */ $price->getExternalReference());
Loading history...
54
55
        return $obolSubscription;
56
    }
57
58
    public function createSubscriptionFromEntity(\Parthenon\Billing\Entity\Subscription $subscription): Subscription
59
    {
60
        $paymentDetails = $this->paymentDetailsRepository->getPaymentDetailsForCustomerAndReference($subscription->getCustomer(), $subscription->getPaymentExternalReference());
61
        $billingDetails = $this->billingDetailsFactory->createFromCustomerAndPaymentDetails($subscription->getCustomer(), $paymentDetails);
62
63
        $obolSubscription = new \Obol\Model\Subscription();
64
        $obolSubscription->setBillingDetails($billingDetails);
65
        $obolSubscription->setId($subscription->getMainExternalReference());
66
        $obolSubscription->setLineId($subscription->getChildExternalReference());
67
68
        return $obolSubscription;
69
    }
70
}
71