Passed
Push — main ( 097c61...c108e4 )
by Iain
04:20
created

ChargeBackSyncer::sync()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 13
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 18
rs 9.8333
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\ChargeBack;
16
17
use Obol\Model\Events\AbstractDispute;
18
use Parthenon\Billing\Entity\ChargeBack;
19
use Parthenon\Billing\Enum\ChargeBackReason;
20
use Parthenon\Billing\Enum\ChargeBackStatus;
21
use Parthenon\Billing\Factory\EntityFactory;
22
use Parthenon\Billing\Repository\ChargeBackRepositoryInterface;
23
use Parthenon\Billing\Repository\PaymentRepositoryInterface;
24
use Parthenon\Common\Exception\NoEntityFoundException;
25
26
class ChargeBackSyncer implements ChargeBackSyncerInterface
27
{
28
    public function __construct(
29
        private ChargeBackRepositoryInterface $chargeBackRepository,
30
        private PaymentRepositoryInterface $paymentRepository,
31
        private EntityFactory $entityFactory,
32
    ) {
33
    }
34
35
    public function sync(AbstractDispute|\Obol\Model\ChargeBack\ChargeBack $event): ChargeBack
36
    {
37
        try {
38
            $chargeBack = $this->chargeBackRepository->getByExternalReference($event->getId());
39
        } catch (NoEntityFoundException $e) {
40
            $chargeBack = $this->entityFactory->getChargeBackEntity();
41
            $payment = $this->paymentRepository->getPaymentForReference($event->getPaymentReference());
42
            $chargeBack->setExternalReference($event->getId());
43
            $chargeBack->setPayment($payment);
44
            $chargeBack->setCustomer($payment->getCustomer());
45
            $chargeBack->setCreatedAt(new \DateTime('now'));
46
        }
47
48
        $chargeBack->setStatus(ChargeBackStatus::fromName($event->getStatus()));
49
        $chargeBack->setReason(ChargeBackReason::fromName($event->getReason()));
50
        $chargeBack->setUpdatedAt(new \DateTime('now'));
51
52
        return $chargeBack;
53
    }
54
}
55