Completed
Push — master ( b7e254...bcf326 )
by
unknown
09:07
created

RefundOrderAction   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 25
c 1
b 0
f 0
dl 0
loc 55
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 29 2
A supports() 0 5 2
A __construct() 0 6 1
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
 * another great project.
7
 * You can find more information about us on https://bitbag.shop and write us
8
 * an email on [email protected].
9
 */
10
11
declare(strict_types=1);
12
13
namespace BitBag\SyliusMolliePlugin\Action;
14
15
use BitBag\SyliusMolliePlugin\Action\Api\BaseApiAwareAction;
16
use BitBag\SyliusMolliePlugin\Helper\ConvertRefundDataInterface;
17
use BitBag\SyliusMolliePlugin\Logger\MollieLoggerActionInterface;
18
use BitBag\SyliusMolliePlugin\Request\Api\RefundOrder;
19
use Mollie\Api\Exceptions\ApiException;
20
use Payum\Core\Action\ActionInterface;
21
use Payum\Core\ApiAwareInterface;
22
use Payum\Core\Bridge\Spl\ArrayObject;
23
use Payum\Core\Exception\RequestNotSupportedException;
24
use Payum\Core\GatewayAwareInterface;
25
use Payum\Core\GatewayAwareTrait;
26
use Sylius\Component\Core\Model\PaymentInterface;
27
use Sylius\Component\Resource\Exception\UpdateHandlingException;
28
29
final class RefundOrderAction extends BaseApiAwareAction implements ActionInterface, ApiAwareInterface, GatewayAwareInterface
30
{
31
    use GatewayAwareTrait;
32
33
    /** @var MollieLoggerActionInterface */
34
    private $loggerAction;
35
36
    /** @var ConvertRefundDataInterface */
37
    private $convertOrderRefundData;
38
39
    public function __construct(
40
        MollieLoggerActionInterface $loggerAction,
41
        ConvertRefundDataInterface $convertOrderRefundData
42
    ) {
43
        $this->loggerAction = $loggerAction;
44
        $this->convertOrderRefundData = $convertOrderRefundData;
45
    }
46
47
    public function execute($request): void
48
    {
49
        RequestNotSupportedException::assertSupports($this, $request);
50
51
        $details = ArrayObject::ensureArrayObject($request->getModel());
52
53
        /** @var PaymentInterface $payment */
54
        $payment = $request->getFirstModel();
55
        $refundData = $this->convertOrderRefundData->convert($details['metadata']['refund'], $payment->getCurrencyCode());
0 ignored issues
show
Bug introduced by
It seems like $payment->getCurrencyCode() can also be of type null; however, parameter $currency of BitBag\SyliusMolliePlugi...ataInterface::convert() 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

55
        $refundData = $this->convertOrderRefundData->convert($details['metadata']['refund'], /** @scrutinizer ignore-type */ $payment->getCurrencyCode());
Loading history...
56
57
        try {
58
            $order = $this->mollieApiClient->orders->get($details['order_mollie_id'], ['embed' => 'payments']);
59
            $payments = $order->_embedded->payments;
60
            $payment = current($payments);
61
62
            $molliePayment = $this->mollieApiClient->payments->get($payment->id);
63
64
            $this->mollieApiClient->payments->refund(
65
                $molliePayment,
66
                [
67
                    'amount' => $refundData,
68
                ]
69
            );
70
71
            $this->loggerAction->addLog(sprintf('Refund order action with order id: %s', $order->id));
72
        } catch (ApiException $e) {
73
            $this->loggerAction->addNegativeLog(sprintf('Error refund order action with: %s', $e->getMessage()));
74
75
            throw new UpdateHandlingException(sprintf('API call failed: %s', htmlspecialchars($e->getMessage())));
76
        }
77
    }
78
79
    public function supports($request): bool
80
    {
81
        return
82
            $request instanceof RefundOrder &&
83
            $request->getModel() instanceof \ArrayAccess;
84
    }
85
}
86