Completed
Push — master ( 71a222...c936d6 )
by Darius
06:35
created

ExpirationDateValidator::validate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 1
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
     * @throws \LVR\CreditCard\Exceptions\CreditCardExpirationDateException
27
     */
28 7
    public function __construct(string $year, string $month)
29
    {
30 7
        if ($year == '' || $month == '') {
31 3
            throw new CreditCardExpirationDateException;
32
        }
33
34 4
        $this->year = $year;
35 4
        $this->month = str_pad($month, 2, '0', STR_PAD_LEFT);
36 4
    }
37
38
    /**
39
     * @param string $year
40
     * @param string $month
41
     *
42
     * @return mixed
43
     */
44 4
    public static function validate(string $year, string $month)
45
    {
46 4
        return (new static($year, $month))->isValid();
47
    }
48
49
    /**
50
     * @return bool
51
     */
52 4
    public function isValid()
53
    {
54 4
        return $this->isValidYear()
55 4
            && $this->isValidMonth()
56 4
            && $this->isFeatureDate();
57
    }
58
59
    /**
60
     * @return bool
61
     */
62 4
    protected function isValidYear()
63
    {
64 4
        return (bool) preg_match('/^20\d\d$/', $this->year);
65
    }
66
67
    /**
68
     * @return bool
69
     */
70 4
    protected function isValidMonth()
71
    {
72 4
        return (bool) preg_match('/^(0[1-9]|1[0-2])$/', $this->month);
73
    }
74
75
    /**
76
     * @return bool
77
     */
78 4
    protected function isFeatureDate()
79
    {
80 4
        return Carbon::now()->startOfDay()->lte(
81 4
            Carbon::createFromFormat('Y-m', $this->year.'-'.$this->month)->endOfDay()
82
        );
83
    }
84
}
85