Passed
Push — main ( abb6be...7a1dde )
by Iain
04:38
created

PaymentFactory::fromChargeEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 1
dl 0
loc 12
rs 9.9666
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\Events\AbstractCharge;
18
use Obol\Model\PaymentDetails;
19
use Obol\Provider\ProviderInterface;
20
use Parthenon\Billing\CustomerProviderInterface;
21
use Parthenon\Billing\Entity\CustomerInterface;
22
use Parthenon\Billing\Entity\Payment;
23
use Parthenon\Billing\Enum\PaymentStatus;
24
25
final class PaymentFactory implements PaymentFactoryInterface
26
{
27
    public function __construct(
28
        private CustomerProviderInterface $customerProvider,
29
        private ProviderInterface $provider,
30
    ) {
31
    }
32
33
    public function fromSubscriptionCreation(PaymentDetails $paymentDetails, ?CustomerInterface $customer = null): Payment
34
    {
35
        if (!$customer) {
36
            $customer = $this->customerProvider->getCurrentCustomer();
37
        }
38
39
        $payment = new Payment();
40
        $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

40
        $payment->setPaymentReference(/** @scrutinizer ignore-type */ $paymentDetails->getPaymentReference());
Loading history...
41
        $payment->setPaymentProviderDetailsUrl($paymentDetails->getPaymentReferenceLink());
42
        $payment->setMoneyAmount($paymentDetails->getAmount());
43
        $payment->setCustomer($customer);
44
        $payment->setCompleted(true);
45
        $payment->setCreatedAt(new \DateTime('now'));
46
        $payment->setUpdatedAt(new \DateTime('now'));
47
        $payment->setStatus(PaymentStatus::COMPLETED);
48
        $payment->setProvider($this->provider->getName());
49
50
        return $payment;
51
    }
52
53
    public function fromChargeEvent(AbstractCharge $charge): Payment
54
    {
55
        $payment = new Payment();
56
        $payment->setPaymentReference($charge->getPaymentReference());
57
        $payment->setPaymentProviderDetailsUrl($charge->getDetailsLink());
58
        $payment->setAmount($charge->getAmount());
59
        $payment->setCurrency($charge->getCurrency());
60
        $payment->setCreatedAt(new \DateTime('now'));
61
        $payment->setUpdatedAt(new \DateTime('now'));
62
        $payment->setProvider($this->provider->getName());
63
64
        return $payment;
65
    }
66
}
67