RefundPaymentProcessor   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 9
dl 0
loc 53
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A process() 0 24 5
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\SyliusAdyenPlugin\PaymentProcessing;
14
15
use BitBag\SyliusAdyenPlugin\AdyenGatewayFactory;
16
use Payum\Core\Payum;
17
use Payum\Core\Request\Refund;
18
use Sylius\Component\Core\Model\PaymentInterface;
19
use Sylius\Component\Core\Model\PaymentMethodInterface;
20
use Sylius\Component\Resource\Exception\UpdateHandlingException;
21
use Symfony\Component\HttpFoundation\Session\Session;
22
23
final class RefundPaymentProcessor implements PaymentProcessorInterface
24
{
25
    /**
26
     * @var Payum
27
     */
28
    private $payum;
29
30
    /**
31
     * @var Session
32
     */
33
    private $session;
34
35
    /**
36
     * @param Payum $payum
37
     * @param Session $session
38
     */
39
    public function __construct(Payum $payum, Session $session)
40
    {
41
        $this->payum = $payum;
42
        $this->session = $session;
43
    }
44
45
    /**
46
     * @param PaymentInterface $payment
47
     *
48
     * @throws UpdateHandlingException
49
     * @throws \Payum\Core\Reply\ReplyInterface
50
     */
51
    public function process(PaymentInterface $payment): void
52
    {
53
        /** @var PaymentMethodInterface $paymentMethod */
54
        $paymentMethod = $payment->getMethod();
55
56
        if (AdyenGatewayFactory::FACTORY_NAME !== $paymentMethod->getGatewayConfig()->getFactoryName()) {
57
            return;
58
        }
59
60
        if (false === isset($payment->getDetails()['extraData'])) {
61
            $this->session->getFlashBag()->add("info", "The payment refund was made only locally.");
62
            return;
63
        }
64
65
        $hash = null !== $payment ? json_decode($payment->getDetails()['extraData'], true)['refundToken'] : '';
66
67
        if (false === $token = $this->payum->getTokenStorage()->find($hash)) {
68
            throw new UpdateHandlingException(sprintf("A token with hash `%s` could not be found.", $hash));
69
        }
70
71
        $gateway = $this->payum->getGateway($token->getGatewayName());
72
73
        $gateway->execute(new Refund($token));
74
    }
75
}
76