CaptureAction::execute()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 29
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 29
rs 9.7666
cc 2
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file was created by developers working at BitBag
5
 * Do you need more information about us and what we do? Visit our https://bitbag.io website!
6
 * We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
7
*/
8
9
declare(strict_types=1);
10
11
namespace BitBag\SyliusCoinbasePlugin\Action;
12
13
use BitBag\SyliusCoinbasePlugin\Action\Api\ApiAwareTrait;
14
use BitBag\SyliusCoinbasePlugin\ApiClient\CoinbaseApiClientInterface;
15
use Payum\Core\Action\ActionInterface;
16
use Payum\Core\ApiAwareInterface;
17
use Payum\Core\Bridge\Spl\ArrayObject;
18
use Payum\Core\Exception\RequestNotSupportedException;
19
use Payum\Core\GatewayAwareInterface;
20
use Payum\Core\GatewayAwareTrait;
21
use Payum\Core\Reply\HttpRedirect;
22
use Payum\Core\Request\Capture;
23
use Payum\Core\Security\GenericTokenFactoryAwareInterface;
24
use Payum\Core\Security\GenericTokenFactoryInterface;
25
use Payum\Core\Security\TokenInterface;
26
27
final class CaptureAction implements ActionInterface, ApiAwareInterface, GatewayAwareInterface, GenericTokenFactoryAwareInterface
28
{
29
    use GatewayAwareTrait, ApiAwareTrait;
30
31
    /** @var GenericTokenFactoryInterface|null */
32
    private $tokenFactory;
33
34
    public function setGenericTokenFactory(GenericTokenFactoryInterface $genericTokenFactory = null): void
35
    {
36
        $this->tokenFactory = $genericTokenFactory;
37
    }
38
39
    public function execute($request): void
40
    {
41
        RequestNotSupportedException::assertSupports($this, $request);
42
43
        $details = ArrayObject::ensureArrayObject($request->getModel());
44
45
        if (isset($details['status'], $details['payment_id'])) {
46
            $charge = $this->coinbaseApiClient->show($details['payment_id']);
47
48
            $timeline = (array)$charge->timeline;
49
            $timelineLast = end($timeline);
50
51
            $details['status'] = strtolower($timelineLast['status']);
52
53
            return;
54
        }
55
56
        /** @var TokenInterface $token */
57
        $token = $request->getToken();
58
59
        $details['redirect_url'] = $token->getTargetUrl();
60
        $details['cancel_url'] = $token->getTargetUrl();
61
62
        $charge = $this->coinbaseApiClient->create($details->getArrayCopy());
63
64
        $details['payment_id'] = $charge->id;
65
        $details['status'] = CoinbaseApiClientInterface::STATUS_CREATED;
66
67
        throw new HttpRedirect($charge->hosted_url);
68
    }
69
70
    public function supports($request): bool
71
    {
72
        return
73
            $request instanceof Capture &&
74
            $request->getModel() instanceof \ArrayAccess
75
        ;
76
    }
77
}
78