HasCardFields::convertExpiryDate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Omnipay\Paystation\Message;
4
5
/**
6
 * Provide additional information to look up card fields on a response object
7
 */
8
trait HasCardFields
9
{
10
    /** @var string|null  The property of $this->data to look up response fields by, or null if directly on $this->data */
11
    protected $lookupField;
12
13
    abstract public function isSuccessful();
14
15
    public function getCardNumber()
16
    {
17
        return $this->getResponseField('CardNo');
18
    }
19
20
    public function getCardExpiryYear()
21
    {
22
        $expiry = $this->convertExpiryDate($this->getResponseField('CardExpiry'));
23
        return $expiry[0];
24
    }
25
26
    public function getCardExpiryMonth()
27
    {
28
        $expiry = $this->convertExpiryDate($this->getResponseField('CardExpiry'));
29
        return $expiry[1];
30
    }
31
32
    public function getCardholderName()
33
    {
34
        return $this->getResponseField('CardholderName');
35
    }
36
37
    public function getCardType()
38
    {
39
        $type = $this->getResponseField('CardType');
40
        if ($type === null) {
41
            $type = $this->getResponseField('Cardtype');
42
        }
43
        return $type;
44
    }
45
46
    /**
47
     * @param string $key  Field to look for in the response (case-sensitive)
48
     * @return null|string  Value if it's present in the response, null if it's not
49
     */
50
    protected function getResponseField($key)
51
    {
52
        if (!$this->isSuccessful()) {
53
            return null;
54
        }
55
        $lookup = $this->lookupField === null ? $this->data : $this->data->{$this->lookupField};
56
        return isset($lookup->$key) ? (string) $lookup->$key : null;
57
    }
58
59
    /**
60
     * @param string $yymm  Expiry date in "yymm" format
61
     * @return int[]|null[]  Array with two elements, year and month
62
     */
63
    protected function convertExpiryDate($yymm)
64
    {
65
        if (preg_match('/([0-9]{2})([0-9]{2})/', $yymm, $match)) {
66
            return array((int) $match[1], (int) $match[2]);
67
        } else {
68
            return array(null, null);
69
        }
70
    }
71
}
72