PhoneNumber   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 28
rs 10
c 0
b 0
f 0
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A sanitizePhoneNumber() 0 17 4
1
<?php
2
3
/**
4
 * Phone number utilities for SMS-based OTP.
5
 *
6
 * @package tvdijen/simplesamlphp-module-cmdotcom
7
 */
8
9
declare(strict_types=1);
10
11
namespace SimpleSAML\Module\cmdotcom\Utils;
12
13
use libphonenumber\NumberParseException;
14
use libphonenumber\PhoneNumberUtil;
15
16
class PhoneNumber
17
{
18
    /**
19
     * Sanitize the mobile phone number for use with the cm.com Rest API
20
     *
21
     * @param string $number
22
     * @param string $defaultRegion
23
     * @return string
24
     * @throws \libphonenumber\NumberParseException
25
     *   if the mobile phone number contains illegal characters or is otherwise invalid.
26
     */
27
    public function sanitizePhoneNumber(string $number, string $defaultRegion = 'NL'): string
28
    {
29
        $util = PhoneNumberUtil::getInstance();
30
        $region = strpos($number, '+') === false ? $defaultRegion : 'ZZ';
31
        $proto = $util->parse($number, $region);
32
33
        if (
34
            $util->isViablePhoneNumber($number) === false
35
            || $util->isValidNumber($proto) === false
36
        ) {
37
            throw new NumberParseException(
38
                NumberParseException::NOT_A_NUMBER,
39
                "The string supplied does not seem to be a valid phone number.",
40
            );
41
        }
42
43
        return '00' . $proto->getCountryCode() . $proto->getNationalNumber();
44
    }
45
}
46