CaptureRequest::getData()   C
last analyzed

Complexity

Conditions 13
Paths 150

Size

Total Lines 64
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 42
dl 0
loc 64
rs 6.2
c 0
b 0
f 0
cc 13
nc 150
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Paytic\Omnipay\Paylike\Message;
4
5
use Paytic\Omnipay\Paylike\Helper;
6
use Paytic\Omnipay\Paylike\Traits\HasApiTrait;
7
8
/**
9
 * Class CaptureRequest
10
 * @package Paytic\Omnipay\Paylike\Message
11
 *
12
 * @method CaptureResponse send
13
 */
14
class CaptureRequest extends AbstractRequest
15
{
16
    use HasApiTrait;
17
18
    /**
19
     * @return array
20
     * @throws \Omnipay\Common\Exception\InvalidRequestException
21
     */
22
    public function getData()
23
    {
24
        $this->validate('privateKey', 'transactionId', 'amount');
25
26
        $data = ['success' => false];
27
28
        $parameters = [
29
            'amount' => $this->getAmount() * 100,
30
        ];
31
32
        //optionals
33
        $currency = $this->getParameter('currency');
34
        if (is_string($currency) && strlen($currency) === 3) {
35
            $parameters['currency'] = $currency;
36
        }
37
        $descriptor = $this->getParameter('descriptor');
38
        if (is_string($descriptor)) {
39
            if (Helper::validateDescriptor($descriptor)) {
40
                throw new \Omnipay\Common\Exception\InvalidRequestException("The descriptor does not conform to requirements.");
41
            }
42
            $parameters['descriptor'] = $descriptor;
43
        }
44
45
46
        try {
47
            $transactions = $this->getApi()->transactions();
48
            $transaction = $transactions->capture(
49
                $this->getTransactionId(),
50
                $parameters
51
            );
52
            if (is_array($transaction)) {
0 ignored issues
show
introduced by
The condition is_array($transaction) is always true.
Loading history...
53
                $data['transaction'] = $transaction;
54
                $data['success'] = true;
55
            }
56
        } catch (\Paylike\Exception\NotFound $e) {
57
            //The transaction was not found
58
            $data['exception_class'] = "\Paylike\Exception\NotFound";
59
            $data['message'] = "The transaction was not found";
60
        } catch (\Paylike\Exception\InvalidRequest $e) {
61
            // Bad (invalid) request - see $e->getJsonBody() for the error
62
            $data['exception_class'] = "\Paylike\Exception\InvalidRequest";
63
            $data['message'] = "Bad (invalid) request - " . substr(json_encode($e->getJsonBody()), 0, 250);
64
        } catch (\Paylike\Exception\Forbidden $e) {
65
            // You are correctly authenticated but do not have access.
66
            $data['exception_class'] = "\Paylike\Exception\Forbidden";
67
            $data['message'] = "You are correctly authenticated but do not have access.";
68
        } catch (\Paylike\Exception\Unauthorized $e) {
69
            // You need to provide credentials (an app's API key)
70
            $data['exception_class'] = "\Paylike\Exception\Unauthorized";
71
            $data['message'] = "You need to provide credentials (an app's API key)";
72
        } catch (\Paylike\Exception\Conflict $e) {
73
            // Everything you submitted was fine at the time of validation, but something changed in the meantime and came into conflict with this (e.g. double-capture).
74
            $data['exception_class'] = "\Paylike\Exception\Conflict";
75
            $data['message'] = "Everything you submitted was fine at the time of validation, but something changed in the meantime and came into conflict with this (e.g. double-capture)";
76
        } catch (\Paylike\Exception\ApiConnection $e) {
77
            // Network error on connecting via cURL
78
            $data['exception_class'] = "\Paylike\Exception\ApiConnection";
79
            $data['message'] = "Network error on connecting via cURL";
80
        } catch (\Paylike\Exception\ApiException $e) {
81
            $data['exception_class'] = "\Paylike\Exception\ApiException";
82
            $data['message'] = "Api Error:" . $e->getMessage();
83
        }
84
85
        return $data;
86
    }
87
}
88