Card::validate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
crap 2
1
<?php
2
3
namespace PHPieces\ANZGateway\models;
4
5
use PHPieces\ANZGateway\enums\FormFields\CardFields;
6
use PHPieces\ANZGateway\exceptions\CardExpiredException;
7
use DateTime;
8
9
class Card extends Model
10
{
11
    protected static $fields = CardFields::class;
12
13
    /**
14
     * Card number
15
     *
16
     * @var int
17
     */
18
    private $cardNumber;
19
20
    /**
21
     * Card expiry date in the format mm/YY
22
     *
23
     * @var int
24
     */
25
    private $expiryDate;
26
27
    /**
28
     * Security code from back of card
29
     *
30
     * @var int
31
     */
32
    private $securityCode;
33
34
    /**
35
     * Card type, eg. Mastercard, Visa...
36
     * @var string
37
     */
38
    private $cardType;
39
40
    /**
41
     *
42
     * @param int $cardNumber
43
     * @param int $expiryDate
44
     * @param int $securityCode
45
     * @param string $cardType
46
     */
47 15
    public function __construct(int $cardNumber, int $expiryDate, int $securityCode, string $cardType
48
    = '')
49
    {
50 15
        $this->cardType = (string) $cardType;
51
52 15
        $this->cardNumber = (int) $cardNumber;
53
54 15
        $this->expiryDate = (int) $expiryDate;
55
56 15
        $this->securityCode = (int) $securityCode;
57
58 15
        $this->validate();
59 12
    }
60
61 15
    public function validate() : void
62
    {
63 15
        $currentDate = new DateTime();
64
65 15
        $cardDate = DateTime::createFromFormat('ym', $this->expiryDate);
66
67 15
        if ($currentDate > $cardDate) {
68 3
            throw new CardExpiredException("This card has expired");
69
        }
70 12
    }
71
72 6
    public function toArray() : array
73
    {
74
        return [
75 6
            CardFields::CARD_NUMBER        => $this->cardNumber,
76 6
            CardFields::CARD_EXPIRY_DATE   => $this->expiryDate,
77 6
            CardFields::CARD_SECURITY_CODE => $this->securityCode,
78
        ];
79
    }
80
}
81