Passed
Pull Request — master (#5)
by Steven
02:13
created

AllowedRecipientsSanitiser::getDigits()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Digitonic\Validation\Services;
4
5
use libphonenumber\NumberParseException;
6
use libphonenumber\PhoneNumber;
7
use libphonenumber\PhoneNumberUtil;
8
use libphonenumber\PhoneNumberFormat;
9
10
class AllowedRecipientsSanitiser
11
{
12
    protected $libPhoneNumber;
13
14 4
    public function __construct()
15
    {
16 4
        $this->libPhoneNumber = PhoneNumberUtil::getInstance();
17 4
    }
18
19
    /**
20
     * Get formatted phone number in E.164 standard without (+).
21
     * This method will return false if the number is not valid.
22
     *
23
     * @param string $phoneNumber
24
     *
25
     * @return mixed
26
     */
27 1
    public function getFormattedNumber(string $phoneNumber)
28
    {
29 1
        $phoneNumberObject = $this->getPhoneNumberObject($phoneNumber);
30
31 1
        if ($phoneNumberObject) {
32 1
            if (in_array(
33 1
                $this->libPhoneNumber->getRegionCodeForNumber($phoneNumberObject),
34 1
                config('digitonic.validation.allowed_mobile_origins')
35
            )) {
36
                // Format number to E.164 standard and remove '+'
37 1
                $formattedPhone = str_replace(
38 1
                    '+',
39 1
                    '',
40 1
                    $this->libPhoneNumber->format($phoneNumberObject, PhoneNumberFormat::E164)
41
                );
42
43 1
                return $formattedPhone;
44
            }
45
        }
46
47 1
        return false;
48
    }
49
50
    /**
51
     * @param PhoneNumber $phoneNumber
52
     * @return string
53
     */
54 2
    public function getRegionCodeForNumber(PhoneNumber $phoneNumber): string
55
    {
56 2
        return $this->libPhoneNumber->getRegionCodeForNumber($phoneNumber);
57
    }
58
59
    /**
60
     * Get phoneNumberObject.
61
     *
62
     * @param string $phoneNumber
63
     *
64
     * @return mixed
65
     */
66 4
    public function getPhoneNumberObject(string $phoneNumber)
67
    {
68
        try {
69 4
            if (!preg_match('/[A-Za-z]/', $phoneNumber)) {
70 4
                $phoneNumberObject = $this->libPhoneNumber->parse($this->sanitisePhoneNumber($phoneNumber), null);
71 4
                if ($this->libPhoneNumber->isPossibleNumber($phoneNumberObject)) {
72 4
                    return $phoneNumberObject;
73
                }
74
            }
75 1
        } catch (NumberParseException $exception) {
76
            // Do nothing
77
        }
78
79 2
        return false;
80
    }
81
82
    /**
83
     * @param string $phoneNumber
84
     * @return string
85
     */
86 4
    protected function sanitisePhoneNumber(string $phoneNumber): string
87
    {
88 4
        $phoneNumber = $this->getDigits($phoneNumber);
89
90 4
        return $this->formatNumber($phoneNumber);
91
    }
92
93
    /**
94
     * @param string $phoneNumber
95
     * @return string
96
     */
97 4
    protected function getDigits(string $phoneNumber): string
98
    {
99 4
        return preg_replace('#\D+#', '', $phoneNumber);
100
    }
101
102
    /**
103
     * @param string $phoneNumber
104
     * @return string
105
     */
106 4
    protected function formatNumber(string $phoneNumber): string
107
    {
108
        // Format UK numbers starting with 07 or 7
109 4
        preg_match('/^0*(7\d{9})$/', $phoneNumber, $outputUkPhone);
110
111 4
        if ($outputUkPhone) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $outputUkPhone of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
112 2
            return '+44' . $outputUkPhone[1];
113
        }
114
115 4
        return '+' . $phoneNumber;
116
    }
117
}
118