CaptureAction::execute()   A
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 9.0648
c 0
b 0
f 0
ccs 20
cts 20
cp 1
cc 5
nc 6
nop 1
crap 5
1
<?php
2
3
namespace PayumTW\Allpay\Action;
4
5
use Payum\Core\Request\Capture;
6
use Payum\Core\GatewayAwareTrait;
7
use Payum\Core\GatewayAwareInterface;
8
use Payum\Core\Action\ActionInterface;
9
use Payum\Core\Bridge\Spl\ArrayObject;
10
use Payum\Core\Request\GetHttpRequest;
11
use PayumTW\Allpay\Action\Api\BaseApiAwareAction;
12
use PayumTW\Allpay\Request\Api\CreateTransaction;
13
use Payum\Core\Exception\RequestNotSupportedException;
14
use Payum\Core\Security\GenericTokenFactoryAwareTrait;
15
use Payum\Core\Security\GenericTokenFactoryAwareInterface;
16
17
class CaptureAction extends BaseApiAwareAction implements ActionInterface, GatewayAwareInterface, GenericTokenFactoryAwareInterface
18
{
19
    use GatewayAwareTrait;
20
    use GenericTokenFactoryAwareTrait;
21
22
    /**
23
     * {@inheritdoc}
24
     *
25
     * @param Capture $request
26
     */
27 2
    public function execute($request)
28
    {
29 2
        RequestNotSupportedException::assertSupports($this, $request);
30 2
        $details = ArrayObject::ensureArrayObject($request->getModel());
31
32 2
        $httpRequest = new GetHttpRequest();
33 2
        $this->gateway->execute($httpRequest);
34
35 2
        if (isset($httpRequest->request['RtnCode']) === true) {
36 1
            if ($this->api->verifyHash($httpRequest->request) === false) {
37 1
                $httpRequest->request['RtnCode'] = '10400002';
38
            }
39 1
            $details->replace($httpRequest->request);
40
41 1
            return;
42
        }
43
44 1
        $token = $request->getToken();
45 1
        $targetUrl = $token->getTargetUrl();
46
47 1
        if (empty($details['OrderResultURL']) === true) {
48 1
            $details['OrderResultURL'] = $targetUrl;
49
        }
50
51 1
        if (empty($details['ReturnURL']) === true) {
52 1
            $notifyToken = $this->tokenFactory->createNotifyToken(
53 1
                $token->getGatewayName(), $token->getDetails()
54
            );
55
56 1
            $details['ReturnURL'] = $notifyToken->getTargetUrl();
57
        }
58
59 1
        $this->gateway->execute(new CreateTransaction($details));
60 1
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 2
    public function supports($request)
66
    {
67
        return
68 2
            $request instanceof Capture &&
69 2
            $request->getModel() instanceof \ArrayAccess;
70
    }
71
}
72