Failed Conditions
Push — issue#774 ( 057f04...ee9377 )
by Guilherme
05:15
created

CEPValidator::validate()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 6.105

Importance

Changes 0
Metric Value
cc 6
eloc 6
nc 5
nop 2
dl 0
loc 10
rs 8.8571
c 0
b 0
f 0
ccs 6
cts 7
cp 0.8571
crap 6.105
1
<?php
2
3
namespace LoginCidadao\ValidationBundle\Validator\Constraints;
4
5
use Symfony\Component\Validator\Constraint;
6
use Symfony\Component\Validator\ConstraintValidator;
7
8
/**
9
 * @Annotation
10
 */
11
class CEPValidator extends ConstraintValidator
12
{
13
    /**
14
     * Currently only checks if the value is numeric.
15
     * @param string $cep
16
     * @return boolean
17
     */
18 2
    public static function isCEPValid($cep)
19
    {
20 2
        $cep = preg_replace('/[^0-9]/', '', $cep);
21
22 2
        return is_numeric($cep);
23
    }
24
25 2
    public static function checkLength($cep)
26
    {
27 2
        $cep = preg_replace('/[^0-9]/', '', $cep);
28
29 2
        return strlen($cep) == 8;
30
    }
31
32 2
    public function validate($value, Constraint $constraint)
33
    {
34 2
        if (!isset($value) || $value === null || !strlen(trim($value))) {
35 1
            return;
36
        }
37 2
        if (!self::checkLength($value)) {
38 1
            $this->context->addViolation($constraint->lengthMessage);
39
        }
40 2
        if (!self::isCEPValid($value)) {
41
            $this->context->addViolation($constraint->message);
42
        }
43 2
    }
44
}
45