VoterRegistrationValidator   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 64
ccs 0
cts 53
cp 0
rs 10
c 0
b 0
f 0
wmc 15

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 7 5
B isValid() 0 45 10
1
<?php
2
namespace PROCERGS\Generic\ValidationBundle\Validator\Constraints;
3
4
use Symfony\Component\Validator\Constraint;
5
use Symfony\Component\Validator\ConstraintValidator;
6
7
/**
8
 * @Annotation
9
 */
10
class VoterRegistrationValidator extends ConstraintValidator
11
{
12
13
    /**
14
     * Currently only checks if the value is numeric.
15
     *
16
     * @param string $cep            
17
     * @return boolean
18
     */
19
    public static function isValid($var)
20
    {
21
        $paddedInsc = str_pad($var, 12, "0", STR_PAD_LEFT);
22
        
23
        $dig1 = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $dig1 is dead and can be removed.
Loading history...
24
        $dig2 = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $dig2 is dead and can be removed.
Loading history...
25
        $tam = strlen($paddedInsc);
26
        $digitos = substr($paddedInsc, $tam - 2, 2);
27
        $estado = substr($paddedInsc, $tam - 4, 2);
28
        $titulo = substr($paddedInsc, 0, $tam - 2);
29
        $exce = ($estado == '01') || ($estado == '02');
30
        $dig1 = (ord($titulo[0]) - 48) * 9 + (ord($titulo[1]) - 48) * 8 + (ord($titulo[2]) - 48) * 7 + (ord($titulo[3]) - 48) * 6 + (ord($titulo[4]) - 48) * 5 + (ord($titulo[5]) - 48) * 4 + (ord($titulo[6]) - 48) * 3 + (ord($titulo[7]) - 48) * 2;
31
        $resto = ($dig1 % 11);
32
        if ($resto == 0) {
33
            if ($exce) {
34
                $dig1 = 1;
35
            } else {
36
                $dig1 = 0;
37
            }
38
        } else {
39
            if ($resto == 1) {
40
                $dig1 = 0;
41
            } else {
42
                $dig1 = 11 - $resto;
43
            }
44
        }
45
        $dig2 = (ord($titulo[8]) - 48) * 4 + (ord($titulo[9]) - 48) * 3 + $dig1 * 2;
46
        $resto = ($dig2 % 11);
47
        if ($resto == 0) {
48
            if ($exce) {
49
                $dig2 = 1;
50
            } else {
51
                $dig2 = 0;
52
            }
53
        } else {
54
            if ($resto == 1) {
55
                $dig2 = 0;
56
            } else {
57
                $dig2 = 11 - $resto;
58
            }
59
        }
60
        if ((ord($digitos[0]) - 48 == $dig1) && (ord($digitos[1]) - 48 == $dig2)) {
61
            return true;
62
        } else {
63
            return false;
64
        }
65
    }
66
67
    public function validate($value, Constraint $constraint)
68
    {
69
        if (! isset($value) || $value === null || ! strlen(trim($value))) {
70
            return;
71
        }
72
        if (! self::isValid($value)) {
73
            $this->context->addViolation($constraint->message);
74
        }
75
    }
76
}
77