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

CardNumber   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
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 46
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A passes() 0 18 4
A message() 0 4 1
1
<?php
2
3
namespace LVR\CreditCard;
4
5
use Illuminate\Contracts\Validation\Rule;
6
use LVR\CreditCard\Exceptions\CreditCardException;
7
use LVR\CreditCard\Exceptions\CreditCardLengthException;
8
use LVR\CreditCard\Exceptions\CreditCardChecksumException;
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 44
    public function passes($attribute, $value)
28
    {
29
        try {
30 44
            return Factory::makeFromNumber($value)->isValidCardNumber();
31 33
        } catch (CreditCardLengthException $ex) {
32 11
            $this->message = self::MSG_CARD_LENGTH_INVALID;
33
34 11
            return false;
35 22
        } catch (CreditCardChecksumException $ex) {
36 11
            $this->message = self::MSG_CARD_CHECKSUM_INVALID;
37
38 11
            return false;
39 11
        } catch (CreditCardException $ex) {
40 11
            $this->message = self::MSG_CARD_INVALID;
41
42 11
            return false;
43
        }
44
    }
45
46
    /**
47
     * Get the validation error message.
48
     *
49
     * @return string
50
     */
51 33
    public function message()
52
    {
53 33
        return $this->message;
54
    }
55
}
56