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

ChargeSucceededHandler   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
dl 0
loc 35
rs 10
c 1
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A handle() 0 18 2
A supports() 0 3 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\Webhook\Handler;
16
17
use Obol\Model\Events\ChargeSucceeded;
18
use Obol\Model\Events\EventInterface;
19
use Parthenon\Billing\Enum\PaymentStatus;
20
use Parthenon\Billing\Exception\InvalidEventException;
21
use Parthenon\Billing\Obol\PaymentFactoryInterface;
22
use Parthenon\Billing\Repository\CustomerRepositoryInterface;
23
use Parthenon\Billing\Repository\PaymentRepositoryInterface;
24
use Parthenon\Billing\Webhook\HandlerInterface;
25
use Parthenon\Common\Exception\NoEntityFoundException;
26
27
class ChargeSucceededHandler implements HandlerInterface
28
{
29
    public function __construct(
30
        private PaymentRepositoryInterface $paymentRepository,
31
        private CustomerRepositoryInterface $customerRepository,
32
        private PaymentFactoryInterface $paymentFactory,
33
    ) {
34
    }
35
36
    public function supports(EventInterface $event): bool
37
    {
38
        return $event instanceof ChargeSucceeded;
39
    }
40
41
    /**
42
     * @param ChargeSucceeded $event
43
     */
44
    public function handle(EventInterface $event): void
45
    {
46
        try {
47
            $payment = $this->paymentRepository->getPaymentForReference($event->getPaymentReference());
0 ignored issues
show
Bug introduced by
The method getPaymentReference() does not exist on Obol\Model\Events\EventInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Obol\Model\Events\DisputeClosed or Obol\Model\Events\DisputeCreation. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

47
            $payment = $this->paymentRepository->getPaymentForReference($event->/** @scrutinizer ignore-call */ getPaymentReference());
Loading history...
48
        } catch (NoEntityFoundException $exception) {
49
            $payment = $this->paymentFactory->fromChargeEvent($event);
0 ignored issues
show
Bug introduced by
$event of type Obol\Model\Events\EventInterface is incompatible with the type Obol\Model\Events\AbstractCharge expected by parameter $charge of Parthenon\Billing\Obol\P...face::fromChargeEvent(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

49
            $payment = $this->paymentFactory->fromChargeEvent(/** @scrutinizer ignore-type */ $event);
Loading history...
50
        }
51
        $payment->setStatus(PaymentStatus::COMPLETED);
52
        $payment->setUpdatedAt(new \DateTime('now'));
53
        /*
54
        try {
55
            $customer = $this->customerRepository->getByExternalReference($event->getExternalCustomerId());
56
        } catch (NoEntityFoundException $e) {
57
            throw new InvalidEventException('Customer not found', previous: $e);
58
        }
59
        $payment->setCustomer($customer);
60
*/
61
        $this->paymentRepository->save($payment);
62
    }
63
}
64