Passed
Push — main ( 7eccc2...45f186 )
by Iain
08:08 queued 03:41
created

PaymentLinker   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 52
rs 10
c 0
b 0
f 0
wmc 13

3 Methods

Rating   Name   Duplication   Size   Complexity  
A linkPaymentDetailsToSubscription() 0 20 6
A linkToSubscription() 0 20 6
A __construct() 0 4 1
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\Subscription;
16
17
use Obol\Model\Events\AbstractCharge;
18
use Obol\Model\PaymentDetails;
19
use Obol\Provider\ProviderInterface;
20
use Parthenon\Billing\Entity\Payment;
21
use Parthenon\Billing\Repository\SubscriptionRepositoryInterface;
22
use Parthenon\Common\Exception\NoEntityFoundException;
23
24
class PaymentLinker implements PaymentLinkerInterface
25
{
26
    public function __construct(
27
        private ProviderInterface $provider,
28
        private SubscriptionRepositoryInterface $subscriptionRepository,
29
    ) {
30
    }
31
32
    public function linkPaymentDetailsToSubscription(Payment $payment, PaymentDetails $charge): void
33
    {
34
        if (!$charge->getInvoiceReference()) {
35
            return;
36
        }
37
38
        $invoice = $this->provider->invoices()->fetch($charge->getInvoiceReference());
39
40
        if (!$invoice) {
41
            return;
42
        }
43
44
        foreach ($invoice->getLines() as $line) {
45
            if (!$line->hasReferences()) {
46
                continue;
47
            }
48
            try {
49
                $subscription = $this->subscriptionRepository->getForMainAndChildExternalReference($line->getMainSubscriptionReference(), $line->getChildSubscriptionReference());
50
                $payment->addSubscription($subscription);
51
            } catch (NoEntityFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
52
            }
53
        }
54
    }
55
56
    public function linkToSubscription(Payment $payment, AbstractCharge $charge): void
57
    {
58
        if (!$charge->hasExternalInvoiceId()) {
59
            return;
60
        }
61
62
        $invoice = $this->provider->invoices()->fetch($charge->getExternalInvoiceId());
0 ignored issues
show
Bug introduced by
It seems like $charge->getExternalInvoiceId() can also be of type null; however, parameter $id of Obol\InvoiceServiceInterface::fetch() 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

62
        $invoice = $this->provider->invoices()->fetch(/** @scrutinizer ignore-type */ $charge->getExternalInvoiceId());
Loading history...
63
64
        if (!$invoice) {
65
            return;
66
        }
67
68
        foreach ($invoice->getLines() as $line) {
69
            if (!$line->hasReferences()) {
70
                continue;
71
            }
72
            try {
73
                $subscription = $this->subscriptionRepository->getForMainAndChildExternalReference($line->getMainSubscriptionReference(), $line->getChildSubscriptionReference());
74
                $payment->addSubscription($subscription);
75
            } catch (NoEntityFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
76
            }
77
        }
78
    }
79
}
80