CardNumber::passes()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 18
c 0
b 0
f 0
ccs 11
cts 11
cp 1
rs 9.6666
cc 4
nc 4
nop 2
crap 4
1
<?php
2
3
namespace LVR\CreditCard;
4
5
use Illuminate\Contracts\Validation\Rule;
6
use LVR\CreditCard\Exceptions\CreditCardChecksumException;
7
use LVR\CreditCard\Exceptions\CreditCardException;
8
use LVR\CreditCard\Exceptions\CreditCardLengthException;
9
10
class CardNumber implements Rule
11
{
12
    const MSG_CARD_INVALID = 'validation.credit_card.card_invalid';
13
    const MSG_CARD_PATTER_INVALID = 'validation.credit_card.card_pattern_invalid';
14
    const MSG_CARD_LENGTH_INVALID = 'validation.credit_card.card_length_invalid';
15
    const MSG_CARD_CHECKSUM_INVALID = 'validation.credit_card.card_checksum_invalid';
16
17
    protected $message = '';
18
19
    /**
20
     * Determine if the validation rule passes.
21
     *
22
     * @param  string $attribute
23
     * @param  mixed $value
24
     *
25
     * @return bool
26
     */
27 56
    public function passes($attribute, $value)
28
    {
29
        try {
30 56
            return Factory::makeFromNumber($value)->isValidCardNumber();
31 42
        } catch (CreditCardLengthException $ex) {
32 14
            $this->message = self::MSG_CARD_LENGTH_INVALID;
33
34 14
            return false;
35 28
        } catch (CreditCardChecksumException $ex) {
36 14
            $this->message = self::MSG_CARD_CHECKSUM_INVALID;
37
38 14
            return false;
39 14
        } catch (CreditCardException $ex) {
40 14
            $this->message = self::MSG_CARD_INVALID;
41
42 14
            return false;
43
        }
44
    }
45
46
    /**
47
     * Get the validation error message.
48
     *
49
     * @return string
50
     */
51 42
    public function message()
52
    {
53 42
        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 53 which is incompatible with the return type declared by the interface Illuminate\Contracts\Validation\Rule::message of type string|array.
Loading history...
54
    }
55
}
56