Passed
Push — main ( ecb15a...097c61 )
by Iain
04:34
created

ChargeBackHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
nc 1
nop 2
dl 0
loc 4
rs 10
c 1
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\Webhook\Handler;
16
17
use Obol\Model\Events\AbstractDispute;
18
use Obol\Model\Events\EventInterface;
19
use Parthenon\Billing\ChargeBack\ChargeBackFactoryInterface;
20
use Parthenon\Billing\Enum\ChargeBackReason;
21
use Parthenon\Billing\Enum\ChargeBackStatus;
22
use Parthenon\Billing\Repository\ChargeBackRepositoryInterface;
23
use Parthenon\Billing\Webhook\HandlerInterface;
24
use Parthenon\Common\Exception\NoEntityFoundException;
25
26
class ChargeBackHandler implements HandlerInterface
27
{
28
    public function __construct(
29
        private ChargeBackRepositoryInterface $chargeBackRepository,
30
        private ChargeBackFactoryInterface $factory,
31
    ) {
32
    }
33
34
    public function supports(EventInterface $event): bool
35
    {
36
        return $event instanceof AbstractDispute;
37
    }
38
39
    /**
40
     * @param AbstractDispute $event
41
     */
42
    public function handle(EventInterface $event): void
43
    {
44
        try {
45
            $chargeBack = $this->chargeBackRepository->getByExternalReference($event->getId());
46
        } catch (NoEntityFoundException $e) {
47
            $chargeBack = $this->factory->buildFromEvent($event);
0 ignored issues
show
Bug introduced by
$event of type Obol\Model\Events\EventInterface is incompatible with the type Obol\Model\Events\AbstractDispute expected by parameter $event of Parthenon\Billing\Charge...rface::buildFromEvent(). ( Ignorable by Annotation )

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

47
            $chargeBack = $this->factory->buildFromEvent(/** @scrutinizer ignore-type */ $event);
Loading history...
48
        }
49
50
        $chargeBack->setStatus(ChargeBackStatus::fromName($event->getStatus()));
0 ignored issues
show
Bug introduced by
The method getStatus() does not exist on Obol\Model\Events\EventInterface. It seems like you code against a sub-type of Obol\Model\Events\EventInterface such as Obol\Model\Events\DisputeClosed or Obol\Model\Events\DisputeCreation. ( Ignorable by Annotation )

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

50
        $chargeBack->setStatus(ChargeBackStatus::fromName($event->/** @scrutinizer ignore-call */ getStatus()));
Loading history...
51
        $chargeBack->setReason(ChargeBackReason::fromName($event->getReason()));
0 ignored issues
show
Bug introduced by
The method getReason() does not exist on Obol\Model\Events\EventInterface. It seems like you code against a sub-type of Obol\Model\Events\EventInterface such as Obol\Model\Events\DisputeClosed or Obol\Model\Events\DisputeCreation. ( Ignorable by Annotation )

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

51
        $chargeBack->setReason(ChargeBackReason::fromName($event->/** @scrutinizer ignore-call */ getReason()));
Loading history...
52
        $chargeBack->setUpdatedAt(new \DateTime());
53
54
        $this->chargeBackRepository->save($chargeBack);
55
    }
56
}
57