CaptureAction   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 24.62 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 11
dl 16
loc 65
ccs 33
cts 33
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B execute() 16 44 5
A supports() 0 6 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace PayumTW\Collect\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\Collect\Action\Api\BaseApiAwareAction;
12
use PayumTW\Collect\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 3
    public function execute($request)
28
    {
29 3
        RequestNotSupportedException::assertSupports($this, $request);
30
31 3
        $details = ArrayObject::ensureArrayObject($request->getModel());
32
33 3
        $httpRequest = new GetHttpRequest();
34 3
        $this->gateway->execute($httpRequest);
35
36 3 View Code Duplication
        if (isset($httpRequest->request['ret']) === true) {
37 1
            if ($this->api->verifyHash($httpRequest->request) === false) {
38 1
                $httpRequest->request['ret'] = 'FAIL';
39 1
            }
40 1
            $details->replace($httpRequest->request);
41
42 1
            return;
43
        }
44
45
        // CVS
46 2 View Code Duplication
        if (isset($httpRequest->request['status']) === true) {
47 1
            if ($this->api->verifyHash($httpRequest->request) === false) {
48 1
                $httpRequest->request['status'] = 'ERROR';
49 1
            }
50 1
            $details->replace($httpRequest->request);
51
52 1
            return;
53
        }
54
55 1
        $token = $request->getToken();
56 1
        $targetUrl = rtrim($token->getTargetUrl(), '/');
57 1
        $targetUrl = substr($targetUrl, 0, strrpos($targetUrl, '/'));
58 1
        $token->setTargetUrl($targetUrl);
59 1
        $token->save();
60
61 1
        $notifyToken = $this->tokenFactory->createNotifyToken(
62 1
            $token->getGatewayName(),
63 1
            $token->getDetails()
64 1
        );
65
66 3
        $notifyToken->setHash(md5($details['cust_order_no']));
67 1
        $notifyToken->save();
68
69 1
        $this->gateway->execute(new CreateTransaction($details));
70 1
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 3
    public function supports($request)
76
    {
77
        return
78 3
            $request instanceof Capture &&
79 3
            $request->getModel() instanceof \ArrayAccess;
80
    }
81
}
82