RefundAction   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 7
dl 0
loc 75
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setApi() 0 8 2
A execute() 0 24 2
A supports() 0 7 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
 * 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\Action;
14
15
use BitBag\SyliusAdyenPlugin\Bridge\AdyenBridgeInterface;
16
use BitBag\SyliusAdyenPlugin\Bridge\ModificationRequestAdyenBridgeInteface;
17
use Payum\Core\Action\ActionInterface;
18
use Payum\Core\ApiAwareInterface;
19
use Payum\Core\Bridge\Spl\ArrayObject;
20
use Payum\Core\Exception\RequestNotSupportedException;
21
use Payum\Core\Exception\UnsupportedApiException;
22
use Payum\Core\GatewayAwareInterface;
23
use Payum\Core\GatewayAwareTrait;
24
use Payum\Core\Request\Refund;
25
use Sylius\Component\Resource\Exception\UpdateHandlingException;
26
27
final class RefundAction implements ActionInterface, ApiAwareInterface, GatewayAwareInterface
28
{
29
    use GatewayAwareTrait;
30
31
    /**
32
     * @var AdyenBridgeInterface
33
     */
34
    protected $api;
35
36
    /**
37
     * @var ModificationRequestAdyenBridgeInteface
38
     */
39
    private $modificationRequestAdyenBridge;
40
41
    /**
42
     * @param ModificationRequestAdyenBridgeInteface $modificationRequestAdyenBridge
43
     */
44
    public function __construct(ModificationRequestAdyenBridgeInteface $modificationRequestAdyenBridge)
45
    {
46
        $this->modificationRequestAdyenBridge = $modificationRequestAdyenBridge;
47
    }
48
49
    /**
50
     * {@inheritDoc}
51
     */
52
    public function setApi($api): void
53
    {
54
        if (false === $api instanceof AdyenBridgeInterface) {
55
            throw new UnsupportedApiException(sprintf('Not supported. Expected %s instance to be set as api.', AdyenBridgeInterface::class));
56
        }
57
58
        $this->api = $api;
59
    }
60
61
    /**
62
     * {@inheritDoc}
63
     *
64
     * @param Refund $request
65
     */
66
    public function execute($request): void
67
    {
68
        RequestNotSupportedException::assertSupports($this, $request);
69
70
        $details = ArrayObject::ensureArrayObject($request->getModel());
71
72
        try {
73
            $this->modificationRequestAdyenBridge->refundRequest(
74
                $this->api,
75
                [
76
                    "currency" => $details['currencyCode'],
77
                    "value" => $details['paymentAmount'],
78
                ],
79
                $details['pspReference'],
80
                $details['merchantReference']
81
            )->refundResult;
82
83
            $details['authResult'] = AdyenBridgeInterface::REFUND;
84
85
            $details['response_status'] = 200;
86
        } catch(\SoapFault $ex){
87
            throw new UpdateHandlingException($ex->getMessage());
88
        }
89
    }
90
91
    /**
92
     * {@inheritDoc}
93
     */
94
    public function supports($request): bool
95
    {
96
        return
97
            $request instanceof Refund &&
98
            $request->getModel() instanceof \ArrayAccess
99
        ;
100
    }
101
}
102