Charge   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 98
ccs 40
cts 40
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A formDecode() 0 23 1
A validate() 0 6 2
A isSuccess() 0 7 2
A getMessage() 0 4 1
A toArray() 0 15 1
1
<?php
2
3
namespace PHPieces\ANZGateway;
4
5
use PHPieces\ANZGateway\exceptions\RequiredArgumentException;
6
use PHPieces\ANZGateway\models\ResponseCode;
7
use Exception;
8
9
class Charge
10
{
11
    const AMOUNT = 'vpc_Amount';
12
    const BATCH_NO = 'vpc_BatchNo';
13
    const CARD = 'vpc_Card';
14
    const LOCALE = 'vpc_Locale';
15
    const MERCH_TXN_REF = 'vpc_MerchTxnRef';
16
    const ORDER_INFO = 'vpc_OrderInfo';
17
    const RECEIPT_NO = 'vpc_ReceiptNo';
18
    const TRANSACTION_NO = 'vpc_TransactionNo';
19
20
    /**
21
     *
22
     * @var ResponseCode
23
     */
24
    public $responseCode;
25
    public $amount;
26
    public $batchNo;
27
    public $card;
28
    public $locale;
29
    public $merchTxnRef;
30
    public $orderInfo;
31
    public $receiptNo;
32
    public $transactionNo;
33
    public $responseContent;
34
    private $failedValidation = false;
35
36 15
    public function __construct($params)
37
    {
38 15
        $this->formDecode($params);
39 15
        $this->validate();
40 15
    }
41
42 15
    private function formDecode(string $params) : void
43
    {
44 15
        $output = [];
45 15
        parse_str($params, $output);
46 15
        $this->responseContent = $params;
47 15
        $this->responseCode = ResponseCode::create($output);
0 ignored issues
show
Bug introduced by
It seems like $output can also be of type null; however, PHPieces\ANZGateway\models\Model::create() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Documentation Bug introduced by
It seems like \PHPieces\ANZGateway\mod...seCode::create($output) of type object<self> is incompatible with the declared type object<PHPieces\ANZGateway\models\ResponseCode> of property $responseCode.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
48
49
        //Required
50 15
        $this->transactionNo = @$output[self::TRANSACTION_NO];
51
52
        //From Input
53 15
        $this->amount = @$output[self::AMOUNT];
54 15
        $this->locale = @$output[self::LOCALE];
55 15
        $this->merchTxnRef = @$output[self::MERCH_TXN_REF];
56 15
        $this->orderInfo = @$output[self::ORDER_INFO];
57
58
        //Optional
59 15
        $this->batchNo = @$output[self::BATCH_NO];
60 15
        $this->card = @$output[self::CARD];
61 15
        $this->receiptNo = @$output[self::RECEIPT_NO];
62
63 15
        $this->validate();
64 15
    }
65
66
    /**
67
     *
68
     * @throws RequiredArgumentException
69
     * @throws Exception
70
     */
71 15
    private function validate() : void
72
    {
73 15
        if (empty($this->transactionNo)) {
74 3
            $this->failedValidation = true;
75
        }
76 15
    }
77
78 6
    public function isSuccess() : bool
79
    {
80 6
        if ($this->failedValidation) {
81 3
            return false;
82
        }
83 3
        return $this->responseCode->isSuccess();
84
    }
85
86 3
    public function getMessage() : string
87
    {
88 3
        return $this->responseCode->getErrorMessage();
89
    }
90
91 3
    public function toArray() : array
92
    {
93
        return [
94 3
            'responseContent' => $this->responseContent,
95 3
            'responseCode'    => $this->responseCode->toJson(),
96 3
            'amount'          => $this->amount,
97 3
            'batchNo'         => $this->batchNo,
98 3
            'card'            => $this->card,
99 3
            'locale'          => $this->locale,
100 3
            'merchTxnRef'     => $this->merchTxnRef,
101 3
            'orderInfo'       => $this->orderInfo,
102 3
            'receiptNo'       => $this->receiptNo,
103 3
            'transactionNo'   => $this->transactionNo,
104
        ];
105
    }
106
}
107