OrderPaymentRefund   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 69
rs 10
c 0
b 0
f 0
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
B refund() 0 48 7
1
<?php
2
3
/*
4
 * This file has been created by developers from BitBag.
5
 * Feel free to contact us once you face any issues or want to start
6
 * You can find more information about us on https://bitbag.io and write us
7
 * an email on [email protected].
8
 */
9
10
declare(strict_types=1);
11
12
namespace BitBag\SyliusMolliePlugin\Order;
13
14
use BitBag\SyliusMolliePlugin\Factory\MollieGatewayFactory;
15
use BitBag\SyliusMolliePlugin\Logger\MollieLoggerActionInterface;
16
use BitBag\SyliusMolliePlugin\Request\Api\RefundOrder;
17
use Payum\Core\Payum;
18
use Payum\Core\Request\Refund as RefundAction;
19
use Payum\Core\Security\TokenInterface;
20
use Sylius\Component\Core\Model\PaymentInterface;
21
use Sylius\Component\Core\Model\PaymentMethodInterface;
22
use Sylius\Component\Resource\Exception\UpdateHandlingException;
23
use Sylius\Component\Resource\Repository\RepositoryInterface;
24
use Sylius\RefundPlugin\Event\UnitsRefunded;
25
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
26
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
27
28
final class OrderPaymentRefund implements OrderPaymentRefundInterface
29
{
30
    /** @var RepositoryInterface */
31
    private $orderRepository;
32
33
    /** @var MollieLoggerActionInterface */
34
    private $loggerAction;
35
36
    /** @var Payum */
37
    private $payum;
38
39
    public function __construct(
40
        RepositoryInterface $orderRepository,
41
        MollieLoggerActionInterface $loggerAction,
42
        Payum $payum
43
    ) {
44
        $this->orderRepository = $orderRepository;
45
        $this->loggerAction = $loggerAction;
46
        $this->payum = $payum;
47
    }
48
49
    public function refund(UnitsRefunded $units): void
50
    {
51
        $order = $this->orderRepository->findOneBy(['number' => $units->orderNumber()]);
52
53
        /** @var PaymentInterface|null $payment */
54
        $payment = $order->getPayments()->last();
55
        if (null === $payment) {
56
            $this->loggerAction->addNegativeLog(sprintf('Not fount payment in refund'));
57
58
            throw new NotFoundHttpException();
59
        }
60
61
        /** @var PaymentMethodInterface $paymentMethod */
62
        $paymentMethod = $payment->getMethod();
63
64
        $factoryName = $paymentMethod->getGatewayConfig()->getFactoryName() ?? null;
0 ignored issues
show
Deprecated Code introduced by
The function Payum\Core\Model\Gateway...rface::getFactoryName() has been deprecated: since 1.3.3 will be removed in 2.0. set factory option inside the config ( Ignorable by Annotation )

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

64
        $factoryName = /** @scrutinizer ignore-deprecated */ $paymentMethod->getGatewayConfig()->getFactoryName() ?? null;

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
65
66
        if (MollieGatewayFactory::FACTORY_NAME !== $factoryName) {
67
            return;
68
        }
69
70
        $details = $payment->getDetails();
71
72
        $details['metadata']['refund']['items'] = $units->units();
73
        $details['metadata']['refund']['shipments'] = $units->shipments();
74
        $payment->setDetails($details);
75
76
        $hash = $details['metadata']['refund_token'];
77
78
        /** @var TokenInterface|null $token */
79
        $token = $this->payum->getTokenStorage()->find($hash);
80
81
        if (null === $token || !$token instanceof TokenInterface) {
82
            $this->loggerAction->addNegativeLog(sprintf('A token with hash `%s` could not be found.', $hash));
83
84
            throw new BadRequestHttpException(sprintf('A token with hash `%s` could not be found.', $hash));
85
        }
86
87
        $gateway = $this->payum->getGateway($token->getGatewayName());
88
89
        try {
90
            if (isset($payment->getDetails()['order_mollie_id'])) {
91
                $gateway->execute(new RefundOrder($token));
92
            } else {
93
                $gateway->execute(new RefundAction($token));
94
            }
95
        } catch (UpdateHandlingException $e) {
96
            $this->loggerAction->addNegativeLog(sprintf('Error with refund: %s', $e->getMessage()));
97
        }
98
    }
99
}
100