RefundAction::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 2
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\Action;
13
14
use BitBag\SyliusMolliePlugin\Action\Api\BaseApiAwareAction;
15
use BitBag\SyliusMolliePlugin\Helper\ConvertRefundDataInterface;
16
use BitBag\SyliusMolliePlugin\Logger\MollieLoggerActionInterface;
17
use Mollie\Api\Exceptions\ApiException;
18
use Payum\Core\Action\ActionInterface;
19
use Payum\Core\ApiAwareInterface;
20
use Payum\Core\Bridge\Spl\ArrayObject;
21
use Payum\Core\Exception\RequestNotSupportedException;
22
use Payum\Core\GatewayAwareInterface;
23
use Payum\Core\GatewayAwareTrait;
24
use Payum\Core\Request\Refund;
25
use Sylius\Component\Core\Model\PaymentInterface;
26
use Sylius\Component\Resource\Exception\UpdateHandlingException;
27
28
final class RefundAction extends BaseApiAwareAction implements ActionInterface, ApiAwareInterface, GatewayAwareInterface
29
{
30
    use GatewayAwareTrait;
31
32
    /** @var MollieLoggerActionInterface */
33
    private $loggerAction;
34
35
    /** @var ConvertRefundDataInterface */
36
    private $convertOrderRefundData;
37
38
    public function __construct(
39
        MollieLoggerActionInterface $loggerAction,
40
        ConvertRefundDataInterface $convertOrderRefundData
41
    ) {
42
        $this->loggerAction = $loggerAction;
43
        $this->convertOrderRefundData = $convertOrderRefundData;
44
    }
45
46
    /** @param Refund $request */
47
    public function execute($request): void
48
    {
49
        RequestNotSupportedException::assertSupports($this, $request);
50
51
        $details = ArrayObject::ensureArrayObject($request->getModel());
52
        /** @var PaymentInterface $payment */
53
        $payment = $request->getFirstModel();
54
55
        try {
56
            $molliePayment = $this->mollieApiClient->payments->get($details['payment_mollie_id']);
57
            $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

57
            $refundData = $this->convertOrderRefundData->convert($details['metadata']['refund'], /** @scrutinizer ignore-type */ $payment->getCurrencyCode());
Loading history...
58
59
            if (true === $molliePayment->canBeRefunded()) {
60
                $molliePayment->refund(['amount' => $refundData]);
61
                $this->loggerAction->addLog(sprintf('Refund action with payment id %s', $molliePayment->id));
62
            } else {
63
                $this->loggerAction->addNegativeLog(sprintf('Payment %s can not be refunded.', $molliePayment->id));
64
65
                throw new UpdateHandlingException(sprintf('Payment %s can not be refunded.', $molliePayment->id));
66
            }
67
        } catch (ApiException $e) {
68
            $this->loggerAction->addNegativeLog(sprintf('API call failed: %s', htmlspecialchars($e->getMessage())));
69
70
            throw new \Exception(sprintf('API call failed: %s', htmlspecialchars($e->getMessage())));
71
        }
72
    }
73
74
    public function supports($request): bool
75
    {
76
        return
77
            $request instanceof Refund &&
78
            $request->getModel() instanceof \ArrayAccess
79
            ;
80
    }
81
}
82