CardExpirationDate   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 64
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A passes() 0 20 3
A message() 0 4 1
1
<?php
2
3
namespace LVR\CreditCard;
4
5
use Carbon\Carbon;
6
use Illuminate\Contracts\Validation\Rule;
7
8
class CardExpirationDate implements Rule
9
{
10
    const MSG_CARD_EXPIRATION_DATE_INVALID = 'validation.credit_card.card_expiration_date_invalid';
11
    const MSG_CARD_EXPIRATION_DATE_FORMAT_INVALID = 'validation.credit_card.card_expiration_date_format_invalid';
12
13
    protected $message;
14
15
    /**
16
     * Date field format.
17
     *
18
     * @var string
19
     */
20
    protected $format;
21
22
    /**
23
     * CardExpirationDate constructor.
24
     *
25
     * @param string $format Date format
26
     */
27 1
    public function __construct(string $format)
28
    {
29 1
        $this->message = static::MSG_CARD_EXPIRATION_DATE_INVALID;
30 1
        $this->format = $format;
31 1
    }
32
33
    /**
34
     * Determine if the validation rule passes.
35
     *
36
     * @param  string $attribute
37
     * @param  mixed $value
38
     *
39
     * @return bool
40
     */
41 1
    public function passes($attribute, $value)
42
    {
43
        try {
44
            // This can throw Invalid Date Exception if format is not supported.
45 1
            Carbon::parse($value);
46
47 1
            $date = Carbon::createFromFormat($this->format, $value);
48
49 1
            return (new ExpirationDateValidator($date->year, $date->month))
50 1
                ->isValid();
51 1
        } catch (\InvalidArgumentException $ex) {
52 1
            $this->message = static::MSG_CARD_EXPIRATION_DATE_FORMAT_INVALID;
53
54 1
            return false;
55 1
        } catch (\Exception $ex) {
56 1
            $this->message = static::MSG_CARD_EXPIRATION_DATE_INVALID;
57
58 1
            return false;
59
        }
60
    }
61
62
    /**
63
     * Get the validation error message.
64
     *
65
     * @return string
66
     */
67
    public function message()
68
    {
69 1
        return trans($this->message);
0 ignored issues
show
Bug Compatibility introduced by
The expression trans($this->message); of type Illuminate\Contracts\Tra...lator|string|array|null adds the type Illuminate\Contracts\Translation\Translator to the return on line 69 which is incompatible with the return type declared by the interface Illuminate\Contracts\Validation\Rule::message of type string|array.
Loading history...
70
    }
71
}
72