PaymentFactory   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 26
c 2
b 1
f 0
dl 0
loc 50
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A fromSubscriptionCreation() 0 9 2
A fromChargeEvent() 0 12 1
A __construct() 0 5 1
A createFromPaymentDetails() 0 16 2
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\Events\AbstractCharge;
25
use Obol\Model\PaymentDetails;
26
use Obol\Provider\ProviderInterface;
27
use Parthenon\Billing\CustomerProviderInterface;
28
use Parthenon\Billing\Entity\CustomerInterface;
29
use Parthenon\Billing\Entity\Payment;
30
use Parthenon\Billing\Enum\PaymentStatus;
31
use Parthenon\Billing\Factory\EntityFactoryInterface;
32
33
final class PaymentFactory implements PaymentFactoryInterface
34
{
35
    public function __construct(
36
        private CustomerProviderInterface $customerProvider,
37
        private ProviderInterface $provider,
38
        private EntityFactoryInterface $entityFactory,
39
    ) {
40
    }
41
42
    public function createFromPaymentDetails(PaymentDetails $paymentDetails, ?CustomerInterface $customer = null): Payment
43
    {
44
        $payment = $this->entityFactory->getPaymentEntity();
45
        $payment->setPaymentReference($paymentDetails->getPaymentReference());
0 ignored issues
show
Bug introduced by
It seems like $paymentDetails->getPaymentReference() can also be of type null; however, parameter $paymentReference of Parthenon\Billing\Entity...::setPaymentReference() 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

45
        $payment->setPaymentReference(/** @scrutinizer ignore-type */ $paymentDetails->getPaymentReference());
Loading history...
46
        $payment->setPaymentProviderDetailsUrl($paymentDetails->getPaymentReferenceLink());
47
        $payment->setMoneyAmount($paymentDetails->getAmount());
48
        if (isset($customer)) {
49
            $payment->setCustomer($customer);
50
        }
51
        $payment->setCompleted(true);
52
        $payment->setCreatedAt(new \DateTime('now'));
53
        $payment->setUpdatedAt(new \DateTime('now'));
54
        $payment->setStatus(PaymentStatus::COMPLETED);
55
        $payment->setProvider($this->provider->getName());
56
57
        return $payment;
58
    }
59
60
    public function fromSubscriptionCreation(PaymentDetails $paymentDetails, ?CustomerInterface $customer = null): Payment
61
    {
62
        if (!$customer) {
63
            $customer = $this->customerProvider->getCurrentCustomer();
64
        }
65
66
        $payment = $this->createFromPaymentDetails($paymentDetails, $customer);
67
68
        return $payment;
69
    }
70
71
    public function fromChargeEvent(AbstractCharge $charge): Payment
72
    {
73
        $payment = $this->entityFactory->getPaymentEntity();
74
        $payment->setPaymentReference($charge->getPaymentReference());
75
        $payment->setPaymentProviderDetailsUrl($charge->getDetailsLink());
76
        $payment->setAmount($charge->getAmount());
77
        $payment->setCurrency($charge->getCurrency());
78
        $payment->setCreatedAt(new \DateTime('now'));
79
        $payment->setUpdatedAt(new \DateTime('now'));
80
        $payment->setProvider($this->provider->getName());
81
82
        return $payment;
83
    }
84
}
85