Passed
Push — main ( 01361c...fbe43b )
by Iain
04:54
created

PaymentFactory::createFromPaymentDetails()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 12
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 16
rs 9.8666
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
use Parthenon\Billing\Factory\EntityFactoryInterface;
25
26
final class PaymentFactory implements PaymentFactoryInterface
27
{
28
    public function __construct(
29
        private CustomerProviderInterface $customerProvider,
30
        private ProviderInterface $provider,
31
        private EntityFactoryInterface $entityFactory,
32
    ) {
33
    }
34
35
    public function createFromPaymentDetails(PaymentDetails $paymentDetails, ?CustomerInterface $customer = null): Payment
36
    {
37
        $payment = $this->entityFactory->getPaymentEntity();
38
        $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

38
        $payment->setPaymentReference(/** @scrutinizer ignore-type */ $paymentDetails->getPaymentReference());
Loading history...
39
        $payment->setPaymentProviderDetailsUrl($paymentDetails->getPaymentReferenceLink());
40
        $payment->setMoneyAmount($paymentDetails->getAmount());
41
        if (isset($customer)) {
42
            $payment->setCustomer($customer);
43
        }
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 fromSubscriptionCreation(PaymentDetails $paymentDetails, ?CustomerInterface $customer = null): Payment
54
    {
55
        if (!$customer) {
56
            $customer = $this->customerProvider->getCurrentCustomer();
57
        }
58
59
        $payment = $this->createFromPaymentDetails($paymentDetails, $customer);
60
61
        return $payment;
62
    }
63
64
    public function fromChargeEvent(AbstractCharge $charge): Payment
65
    {
66
        $payment = $this->entityFactory->getPaymentEntity();
67
        $payment->setPaymentReference($charge->getPaymentReference());
68
        $payment->setPaymentProviderDetailsUrl($charge->getDetailsLink());
69
        $payment->setAmount($charge->getAmount());
70
        $payment->setCurrency($charge->getCurrency());
71
        $payment->setCreatedAt(new \DateTime('now'));
72
        $payment->setUpdatedAt(new \DateTime('now'));
73
        $payment->setProvider($this->provider->getName());
74
75
        return $payment;
76
    }
77
}
78