Completed
Push — master ( 02b308...987bda )
by Darius
01:28
created

CardExpirationDate   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 58
ccs 12
cts 12
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 14 2
A message() 0 4 1
1
<?php
2
3
namespace LVR\CreditCard;
4
5
use Illuminate\Support\Carbon;
6
use LVR\CreditCard\Cards\Card;
7
use Illuminate\Contracts\Validation\Rule;
8
9
class CardExpirationDate implements Rule
10
{
11
    const MSG_CARD_EXPIRATION_DATE_INVALID = 'validation.credit_card.card_expiation_date_invalid';
12
    const MSG_CARD_EXPIRATION_DATE_FORMAT_INVALID = 'validation.credit_card.card_expiation_date_format_invalid';
13
14
    protected $message;
15
16
    /**
17
     * Date field format.
18
     *
19
     * @var string
20
     */
21
    protected $format;
22
23
    /**
24
     * CardExpirationDate constructor.
25
     *
26
     * @param string $format Date format
27
     */
28 1
    public function __construct(string $format)
29
    {
30 1
        $this->message = static::MSG_CARD_EXPIRATION_DATE_INVALID;
31 1
        $this->format = $format;
32 1
    }
33
34
    /**
35
     * Determine if the validation rule passes.
36
     *
37
     * @param  string $attribute
38
     * @param  mixed $value
39
     *
40
     * @return bool
41
     */
42 1
    public function passes($attribute, $value)
43
    {
44
        try {
45 1
            $date = Carbon::createFromFormat($this->format, $value);
46
47 1
            return Card::isValidExpirationDate($date->year, $date->month);
48 1
        } catch (\InvalidArgumentException $ex) {
49 1
            $this->message = static::MSG_CARD_EXPIRATION_DATE_FORMAT_INVALID;
50
51 1
            return false;
52
        }
53
54
        return false;
0 ignored issues
show
Unused Code introduced by
return false; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
55
    }
56
57
    /**
58
     * Get the validation error message.
59
     *
60
     * @return string
61
     */
62 1
    public function message()
63
    {
64 1
        return $this->message;
65
    }
66
}
67