GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Msisdn   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 177
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 94.44%

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 1
dl 0
loc 177
ccs 68
cts 72
cp 0.9444
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 2
A get() 0 21 4
A getPrefix() 0 8 2
A getOperator() 0 30 5
A setPrefixes() 0 14 4
A validate() 0 8 3
A clean() 0 15 3
A setCountryPrefix() 0 4 1
1
<?php
2
3
namespace Coreproc\MsisdnPh;
4
5
use Coreproc\MsisdnPh\Exceptions\InvalidMsisdnException;
6
7
class Msisdn
8
{
9
    private $msisdn;
10
11
    private $smartPrefixes = null;
12
13
    private $globePrefixes = null;
14
15
    private $sunPrefixes = null;
16
17
    private $prefix = null;
18
19
    private $operator = null;
20
21
    protected $countryPrefix = '+63';
22
23
    /**
24
     * Msisdn constructor.
25
     *
26
     * @param $msisdn
27
     * @throws InvalidMsisdnException
28
     */
29 12
    public function __construct($msisdn)
30
    {
31 12
        if (Msisdn::validate($msisdn) === false) {
32 3
            throw new InvalidMsisdnException(
33
                'The supplied MSISDN is not valid. ' .
34 1
                'You can use the `Msisdn::validate()` method ' .
35 3
                'to validate the MSISDN being passed.',
36 2
                400
37 1
            );
38
        }
39
40 9
        $this->msisdn = self::clean($msisdn);
41 9
    }
42
43
    /**
44
     * Returns a formatted mobile number
45
     *
46
     * @param bool|false $hasCountryCode
47
     * @param string $separator
48
     * @return mixed|string
49
     */
50 3
    function get($hasCountryCode = false, $separator = '')
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
51
    {
52 3
        if (! $hasCountryCode) {
53 3
            $formattedNumber = '0' . $this->msisdn;
54
55 3
            if (! empty($separator)) {
56 3
                $formattedNumber = substr_replace($formattedNumber, $separator, 4, 0);
57 3
                $formattedNumber = substr_replace($formattedNumber, $separator, 8, 0);
58 1
            }
59 1
        } else {
60 3
            $formattedNumber = $this->countryPrefix . $this->msisdn;
61
62 3
            if (! empty($separator)) {
63 3
                $formattedNumber = substr_replace($formattedNumber, $separator, strlen($this->countryPrefix), 0);
64 3
                $formattedNumber = substr_replace($formattedNumber, $separator, 7, 0);
65 3
                $formattedNumber = substr_replace($formattedNumber, $separator, 11, 0);
66 1
            }
67
        }
68
69 3
        return $formattedNumber;
70
    }
71
72
    /**
73
     * Returns the prefix of the MSISDN number.
74
     *
75
     * @return string The prefix of the MSISDN number
76
     */
77 6
    public function getPrefix()
78
    {
79 6
        if ($this->prefix == null) {
80 6
            $this->prefix = substr($this->msisdn, 0, 3);
81 2
        }
82
83 6
        return $this->prefix;
84
    }
85
86
    /**
87
     * Determines the operator of this number
88
     *
89
     * @return string The operator of this number
90
     */
91 3
    public function getOperator()
92
    {
93 3
        $this->setPrefixes();
94
95 3
        if (! empty($this->operator)) {
96
            return $this->operator;
97
        }
98
99 3
        if (in_array($this->getPrefix(), $this->globePrefixes)) {
100 3
            $this->operator = 'GLOBE';
101
102 3
            return $this->operator;
103
        }
104
105 3
        if (in_array($this->getPrefix(), $this->smartPrefixes)) {
106 3
            $this->operator = 'SMART';
107
108 3
            return $this->operator;
109
        }
110
111 3
        if (in_array($this->getPrefix(), $this->sunPrefixes)) {
112 3
            $this->operator = 'SUN';
113
114 3
            return $this->operator;
115
        }
116
117 3
        $this->operator = 'UNKNOWN';
118
119 3
        return $this->operator;
120
    }
121
122 3
    private function setPrefixes()
123
    {
124 3
        if (empty($this->smartPrefixes)) {
125 3
            $this->smartPrefixes = json_decode(file_get_contents(__DIR__ . '/prefixes/smart.json'));
126 1
        }
127
128 3
        if (empty($this->globePrefixes)) {
129 3
            $this->globePrefixes = json_decode(file_get_contents(__DIR__ . '/prefixes/globe.json'));
130 1
        }
131
132 3
        if (empty($this->sunPrefixes)) {
133 3
            $this->sunPrefixes = json_decode(file_get_contents(__DIR__ . '/prefixes/sun.json'));
134 1
        }
135 3
    }
136
137
    /**
138
     * Validate a given mobile number
139
     *
140
     * @param string $mobileNumber
141
     * @return bool
142
     */
143 18
    public static function validate($mobileNumber)
144
    {
145 18
        $mobileNumber = Msisdn::clean($mobileNumber);
146
147 18
        return ! empty($mobileNumber) &&
148 18
            strlen($mobileNumber) === 10 &&
149 18
            is_numeric($mobileNumber);
150
    }
151
152
    /**
153
     * Cleans the string
154
     *
155
     * @param string $msisdn
156
     * @return string The clean MSISDN
157
     */
158 18
    private static function clean($msisdn)
159
    {
160 18
        $msisdn = preg_replace("/[^0-9]/", "", $msisdn);
161
162
        // We remove the 0 or 63 from the number
163 18
        if (substr($msisdn, 0, 1) === '0') {
164 15
            $msisdn = substr($msisdn, 1, strlen($msisdn));
165 5
        } else {
166 9
            if (substr($msisdn, 0, 2) === '63') {
167 9
                $msisdn = substr($msisdn, 2, strlen($msisdn));
168 3
            }
169
        }
170
171 18
        return $msisdn;
172
    }
173
174
    /**
175
     * Sets the country prefix - this defaults to +63
176
     *
177
     * @param $countryPrefix
178
     */
179
    public function setCountryPrefix($countryPrefix)
180
    {
181
        $this->countryPrefix = $countryPrefix;
182
    }
183
}
184