Payment   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 71
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A validate() 0 13 4
A toArray() 0 8 1
1
<?php
2
3
namespace PHPieces\ANZGateway\models;
4
5
use PHPieces\ANZGateway\enums\FormFields\PaymentFields;
6
use PHPieces\ANZGateway\exceptions\InvalidCostException;
7
use PHPieces\ANZGateway\exceptions\RequiredArgumentException;
8
9
class Payment extends Model
10
{
11
    protected static $fields = PaymentFields::class;
12
13
    /**
14
     * The amount of a transaction must be converted into units of
15
     * cents. For example, an amount of $1 must be entered as 100.
16
     * If the amount was $100.50, this must be entered as 10050.
17
     * @var int
18
     */
19
    private $purchaseAmount;
20
21
    /**
22
     *  This is a required field and may have a
23
     *  maximum of 34 alpha numeric characters. This will appear
24
     *  in the Shopping Transactions Report on the MA site and is
25
     *  a searchable field so should be used as the primary
26
     *  reference number for transactions, for example the order
27
     *  number, invoice number, or customer number. This field
28
     *  label is Transaction OrderInfo in the Sample Code.
29
     * @var String
30
     */
31
    private $orderInfo;
32
33
    /**
34
     * This is an optional field that can store up to 15 alpha
35
     * numeric characters.
36
     * @var String
37
     */
38
    private $ticketNo;
39
40
    /**
41
     *
42
     * @param int $purchaseAmount
43
     * @param String $orderInfo
44
     * @param String $ticketNo
45
     */
46 21
    public function __construct($purchaseAmount, $orderInfo, $ticketNo = '')
47
    {
48 21
        $this->purchaseAmount = (int) $purchaseAmount;
49
50 21
        $this->orderInfo = (string) $orderInfo;
51
52 21
        $this->ticketNo = (string) $ticketNo;
53
54 21
        $this->validate();
55 12
    }
56
57 21
    public function validate() : void
58
    {
59
        
60 21
        if ($this->purchaseAmount < 1) {
61 3
            throw new InvalidCostException("Cannot create a charge for less than 1 cent");
62
        }
63 18
        if (empty($this->orderInfo)) {
64 3
            throw new RequiredArgumentException();
65
        }
66 15
        if (strlen($this->orderInfo) > 34) {
67 3
            throw new \Exception("length of order info too long");
68
        }
69 12
    }
70
71 6
    public function toArray() : array
72
    {
73
        return [
74 6
            PaymentFields::PURCHASE_AMOUNT       => $this->purchaseAmount,
75 6
            PaymentFields::TRANSACTION_ORDER_INFO => $this->orderInfo,
76 6
            PaymentFields::TICKET_NO              => $this->ticketNo,
77
        ];
78
    }
79
}
80