Completed
Push — master ( e7da8f...4d8ba9 )
by Darius
09:19
created

ExpirationDateValidator::month()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace LVR\CreditCard;
4
5
use Carbon\Carbon;
6
use LVR\CreditCard\Exceptions\CreditCardExpirationDateException;
7
8
class ExpirationDateValidator
9
{
10
    /**
11
     * @var string
12
     */
13
    protected $year;
14
15
    /**
16
     * @var string
17
     */
18
    protected $month;
19
20
    /**
21
     * ExpirationDateValidator constructor.
22
     *
23
     * @param string $year
24
     * @param string $month
25
     *
26
     */
27
    public function __construct(string $year, string $month)
28 7
    {
29
        $this->year = trim($year);
30 7
        $this->month = trim($month);
31 3
    }
32
33
    /**
34 4
     * @param string $year
35 4
     * @param string $month
36 4
     *
37
     * @return mixed
38
     */
39
    public static function validate(string $year, string $month)
40
    {
41
        return (new static($year, $month))->isValid();
42
    }
43
44 4
    /**
45
     * @return bool
46 4
     */
47
    public function isValid()
48
    {
49
        return $this->isValidYear()
50
            && $this->isValidMonth()
51
            && $this->isFeatureDate();
52 4
    }
53
54 4
    /**
55 4
     * @return string
56 4
     */
57
    protected function month()
58
    {
59
        return str_pad($this->month, 2, '0', STR_PAD_LEFT);
60
    }
61
62 4
    /**
63
     * @return bool
64 4
     */
65
    protected function isValidYear()
66
    {
67
        return $this->year != ''
68
            && preg_match('/^20\d\d$/', $this->year);
69
    }
70 4
71
    /**
72 4
     * @return bool
73
     */
74
    protected function isValidMonth()
75
    {
76
        return $this->month != ''
77
            && $this->month() != '00'
78 4
            && preg_match('/^(0[1-9]|1[0-2])$/', $this->month());
79
    }
80 4
81 4
    /**
82
     * @return bool
83
     */
84
    protected function isFeatureDate()
85
    {
86
        return Carbon::now()->startOfDay()->lte(
87
            Carbon::createFromFormat('Y-m', $this->year.'-'.$this->month())->endOfDay()
88
        );
89
    }
90
}
91