PaymentLinker::__construct()   A
last analyzed

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 (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\Subscription;
23
24
use Obol\Model\Events\AbstractCharge;
25
use Obol\Model\PaymentDetails;
26
use Obol\Provider\ProviderInterface;
27
use Parthenon\Billing\Entity\Payment;
28
use Parthenon\Billing\Repository\SubscriptionRepositoryInterface;
29
use Parthenon\Common\Exception\NoEntityFoundException;
30
use Parthenon\Common\LoggerAwareTrait;
31
32
class PaymentLinker implements PaymentLinkerInterface
33
{
34
    use LoggerAwareTrait;
35
36
    public function __construct(
37
        private ProviderInterface $provider,
38
        private SubscriptionRepositoryInterface $subscriptionRepository,
39
    ) {
40
    }
41
42
    public function linkPaymentDetailsToSubscription(Payment $payment, PaymentDetails $charge): void
43
    {
44
        if (!$charge->getInvoiceReference()) {
45
            return;
46
        }
47
48
        $invoice = $this->provider->invoices()->fetch($charge->getInvoiceReference());
49
50
        if (!$invoice) {
51
            $this->getLogger()->warning('No invoice found to link payment details', ['invoice_reference' => $charge->getInvoiceReference()]);
52
53
            return;
54
        }
55
56
        foreach ($invoice->getLines() as $line) {
57
            if (!$line->hasReferences()) {
58
                continue;
59
            }
60
            try {
61
                $subscription = $this->subscriptionRepository->getForMainAndChildExternalReference($line->getMainSubscriptionReference(), $line->getChildSubscriptionReference());
62
                $payment->addSubscription($subscription);
63
            } catch (NoEntityFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
64
            }
65
        }
66
    }
67
68
    public function linkToSubscription(Payment $payment, AbstractCharge $charge): void
69
    {
70
        if (!$charge->hasExternalInvoiceId()) {
71
            $this->getLogger()->warning('Charge does not have an external id');
72
73
            return;
74
        }
75
76
        $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

76
        $invoice = $this->provider->invoices()->fetch(/** @scrutinizer ignore-type */ $charge->getExternalInvoiceId());
Loading history...
77
78
        if (!$invoice) {
79
            $this->getLogger()->info('No invoice found to link to subscription');
80
81
            return;
82
        }
83
84
        foreach ($invoice->getLines() as $line) {
85
            if (!$line->hasReferences()) {
86
                $this->getLogger()->info("Don't have references");
87
                continue;
88
            }
89
            try {
90
                $subscription = $this->subscriptionRepository->getForMainAndChildExternalReference($line->getMainSubscriptionReference(), $line->getChildSubscriptionReference());
91
                $payment->addSubscription($subscription);
92
            } catch (NoEntityFoundException $e) {
93
                $this->getLogger()->warning('Unable to find subscription for invoice', ['main_subscription_reference' => $line->getMainSubscriptionReference(), 'child_subscription_reference' => $line->getChildSubscriptionReference()]);
94
            }
95
        }
96
    }
97
}
98