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

MobilePhoneNumberValidator::getPhoneNumber()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 4
nop 1
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
ccs 8
cts 8
cp 1
crap 3
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LoginCidadao\ValidationBundle\Validator\Constraints;
12
13
use libphonenumber\PhoneNumber;
14
use libphonenumber\PhoneNumberType;
15
use libphonenumber\PhoneNumberUtil;
16
use Symfony\Component\Validator\Constraint;
17
use Symfony\Component\Validator\ConstraintValidator;
18
19
/**
20
 * @Annotation
21
 */
22
class MobilePhoneNumberValidator extends ConstraintValidator
23
{
24
    /**
25
     * Checks if the passed value is valid.
26
     *
27
     * @param mixed $value The value that should be validated
28
     * @param Constraint $constraint The constraint for the validation
29
     */
30 3
    public function validate($value, Constraint $constraint)
31
    {
32 3
        if ($constraint instanceof MobilePhoneNumber) {
33 3
            $phoneNumber = $this->getPhoneNumber($value);
34
35
            // Check length
36 3
            if ($phoneNumber !== null && false === self::isMobile($phoneNumber)) {
37 1
                $this->context->addViolation($constraint->missing9thDigit);
38
            }
39
        }
40 3
    }
41
42
    /**
43
     * This checks if the given PhoneNumber instance is a Mobile phone.
44
     * Additionally, it checks for the 9th digit in case the number is from Brazil (+55)
45
     *
46
     * @param $phone
47
     * @return bool
48
     */
49 6
    public static function isMobile($phone)
50
    {
51 6
        if (!$phone instanceof PhoneNumber) {
52 1
            return false;
53
        }
54
55 5
        $phoneUtil = PhoneNumberUtil::getInstance();
56
57 5
        $allowedTypes = [PhoneNumberType::MOBILE, PhoneNumberType::FIXED_LINE_OR_MOBILE];
58 5
        if (false === array_search($phoneUtil->getNumberType($phone), $allowedTypes)) {
59 1
            return false;
60
        }
61
62
        // Brazilian mobile phone without 9th digit
63 4
        if ($phone->getCountryCode() == '55' && strlen($phone->getNationalNumber()) !== 11) {
64 2
            return false;
65
        }
66
67 2
        return true;
68
    }
69
70 3
    private function getPhoneNumber($value)
71
    {
72 3
        if ($value instanceof PhoneNumber) {
73 1
            return $value;
74
        }
75
76 2
        $number = preg_replace('/[^0-9+]/', '', $value);
77
        try {
78 2
            $phoneUtil = PhoneNumberUtil::getInstance();
79
80 2
            return $phoneUtil->parse($number, null);
81 1
        } catch (\Exception $e) {
82
            // This failure will be detected by another Validator
83 1
            return null;
84
        }
85
    }
86
}
87