Completed
Push — master ( 4d8ba9...bf1e8c )
by Darius
10:29 queued 09:12
created

ExpirationDateValidator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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