Passed
Push — master ( 3d0d87...075b69 )
by Antonio Carlos
09:13
created

Countries   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 340
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
lcom 1
cbo 6
dl 0
loc 340
ccs 25
cts 25
cp 1
rs 10
c 1
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A all() 0 4 1
A getGeoIp() 0 4 1
A isValid() 0 6 1
A validCountry() 0 12 4
A getCountryFromIp() 0 6 2
A makeCountryFromString() 0 16 4
1
<?php
2
3
namespace PragmaRX\Firewall\Repositories;
4
5
use Illuminate\Support\Collection;
6
use PragmaRX\Firewall\Support\ServiceInstances;
7
8
class Countries
9
{
10
    use ServiceInstances;
11
12
    protected $all = [
13
        'ad'   => 'Andorra, Principality of',
14
        'ae'   => 'United Arab Emirates',
15
        'af'   => 'Afghanistan, Islamic State of',
16
        'ag'   => 'Antigua and Barbuda',
17
        'ai'   => 'Anguilla',
18
        'al'   => 'Albania',
19
        'am'   => 'Armenia',
20
        'an'   => 'Netherlands Antilles',
21
        'ao'   => 'Angola',
22
        'aq'   => 'Antarctica',
23
        'ar'   => 'Argentina',
24
        'arpa' => 'Old style Arpanet',
25
        'as'   => 'American Samoa',
26
        'at'   => 'Austria',
27
        'au'   => 'Australia',
28
        'aw'   => 'Aruba',
29
        'az'   => 'Azerbaidjan',
30
        'ba'   => 'Bosnia-Herzegovina',
31
        'bb'   => 'Barbados',
32
        'bd'   => 'Bangladesh',
33
        'be'   => 'Belgium',
34
        'bf'   => 'Burkina Faso',
35
        'bg'   => 'Bulgaria',
36
        'bh'   => 'Bahrain',
37
        'bi'   => 'Burundi',
38
        'bj'   => 'Benin',
39
        'bm'   => 'Bermuda',
40
        'bn'   => 'Brunei Darussalam',
41
        'bo'   => 'Bolivia',
42
        'br'   => 'Brazil',
43
        'bs'   => 'Bahamas',
44
        'bt'   => 'Bhutan',
45
        'bv'   => 'Bouvet Island',
46
        'bw'   => 'Botswana',
47
        'by'   => 'Belarus',
48
        'bz'   => 'Belize',
49
        'ca'   => 'Canada',
50
        'cc'   => 'Cocos (Keeling) Islands',
51
        'cf'   => 'Central African Republic',
52
        'cd'   => 'Congo, The Democratic Republic of the',
53
        'cg'   => 'Congo',
54
        'ch'   => 'Switzerland',
55
        'ci'   => 'Ivory Coast (Cote D\'Ivoire)',
56
        'ck'   => 'Cook Islands',
57
        'cl'   => 'Chile',
58
        'cm'   => 'Cameroon',
59
        'cn'   => 'China',
60
        'co'   => 'Colombia',
61
        'com'  => 'Commercial',
62
        'cr'   => 'Costa Rica',
63
        'cs'   => 'Former Czechoslovakia',
64
        'cu'   => 'Cuba',
65
        'cv'   => 'Cape Verde',
66
        'cx'   => 'Christmas Island',
67
        'cy'   => 'Cyprus',
68
        'cz'   => 'Czech Republic',
69
        'de'   => 'Germany',
70
        'dj'   => 'Djibouti',
71
        'dk'   => 'Denmark',
72
        'dm'   => 'Dominica',
73
        'do'   => 'Dominican Republic',
74
        'dz'   => 'Algeria',
75
        'ec'   => 'Ecuador',
76
        'edu'  => 'Educational',
77
        'ee'   => 'Estonia',
78
        'eg'   => 'Egypt',
79
        'eh'   => 'Western Sahara',
80
        'er'   => 'Eritrea',
81
        'es'   => 'Spain',
82
        'et'   => 'Ethiopia',
83
        'fi'   => 'Finland',
84
        'fj'   => 'Fiji',
85
        'fk'   => 'Falkland Islands',
86
        'fm'   => 'Micronesia',
87
        'fo'   => 'Faroe Islands',
88
        'fr'   => 'France',
89
        'fx'   => 'France (European Territory)',
90
        'ga'   => 'Gabon',
91
        'gb'   => 'Great Britain',
92
        'gd'   => 'Grenada',
93
        'ge'   => 'Georgia',
94
        'gf'   => 'French Guyana',
95
        'gh'   => 'Ghana',
96
        'gi'   => 'Gibraltar',
97
        'gl'   => 'Greenland',
98
        'gm'   => 'Gambia',
99
        'gn'   => 'Guinea',
100
        'gov'  => 'USA Government',
101
        'gp'   => 'Guadeloupe (French)',
102
        'gq'   => 'Equatorial Guinea',
103
        'gr'   => 'Greece',
104
        'gs'   => 'S. Georgia & S. Sandwich Isls.',
105
        'gt'   => 'Guatemala',
106
        'gu'   => 'Guam (USA)',
107
        'gw'   => 'Guinea Bissau',
108
        'gy'   => 'Guyana',
109
        'hk'   => 'Hong Kong',
110
        'hm'   => 'Heard and McDonald Islands',
111
        'hn'   => 'Honduras',
112
        'hr'   => 'Croatia',
113
        'ht'   => 'Haiti',
114
        'hu'   => 'Hungary',
115
        'id'   => 'Indonesia',
116
        'ie'   => 'Ireland',
117
        'il'   => 'Israel',
118
        'in'   => 'India',
119
        'int'  => 'International',
120
        'io'   => 'British Indian Ocean Territory',
121
        'iq'   => 'Iraq',
122
        'ir'   => 'Iran',
123
        'is'   => 'Iceland',
124
        'it'   => 'Italy',
125
        'jm'   => 'Jamaica',
126
        'jo'   => 'Jordan',
127
        'jp'   => 'Japan',
128
        'ke'   => 'Kenya',
129
        'kg'   => 'Kyrgyz Republic (Kyrgyzstan)',
130
        'kh'   => 'Cambodia, Kingdom of',
131
        'ki'   => 'Kiribati',
132
        'km'   => 'Comoros',
133
        'kn'   => 'Saint Kitts & Nevis Anguilla',
134
        'kp'   => 'North Korea',
135
        'kr'   => 'South Korea',
136
        'kw'   => 'Kuwait',
137
        'ky'   => 'Cayman Islands',
138
        'kz'   => 'Kazakhstan',
139
        'la'   => 'Laos',
140
        'lb'   => 'Lebanon',
141
        'lc'   => 'Saint Lucia',
142
        'li'   => 'Liechtenstein',
143
        'lk'   => 'Sri Lanka',
144
        'lr'   => 'Liberia',
145
        'ls'   => 'Lesotho',
146
        'lt'   => 'Lithuania',
147
        'lu'   => 'Luxembourg',
148
        'lv'   => 'Latvia',
149
        'ly'   => 'Libya',
150
        'ma'   => 'Morocco',
151
        'mc'   => 'Monaco',
152
        'md'   => 'Moldavia',
153
        'mg'   => 'Madagascar',
154
        'mh'   => 'Marshall Islands',
155
        'mil'  => 'USA Military',
156
        'mk'   => 'Macedonia',
157
        'ml'   => 'Mali',
158
        'mm'   => 'Myanmar',
159
        'mn'   => 'Mongolia',
160
        'mo'   => 'Macau',
161
        'mp'   => 'Northern Mariana Islands',
162
        'mq'   => 'Martinique (French)',
163
        'mr'   => 'Mauritania',
164
        'ms'   => 'Montserrat',
165
        'mt'   => 'Malta',
166
        'mu'   => 'Mauritius',
167
        'mv'   => 'Maldives',
168
        'mw'   => 'Malawi',
169
        'mx'   => 'Mexico',
170
        'my'   => 'Malaysia',
171
        'mz'   => 'Mozambique',
172
        'na'   => 'Namibia',
173
        'nato' => 'NATO (this was purged in 1996 - see hq.nato.int)',
174
        'nc'   => 'New Caledonia (French)',
175
        'ne'   => 'Niger',
176
        'net'  => 'Network',
177
        'nf'   => 'Norfolk Island',
178
        'ng'   => 'Nigeria',
179
        'ni'   => 'Nicaragua',
180
        'nl'   => 'Netherlands',
181
        'no'   => 'Norway',
182
        'np'   => 'Nepal',
183
        'nr'   => 'Nauru',
184
        'nt'   => 'Neutral Zone',
185
        'nu'   => 'Niue',
186
        'nz'   => 'New Zealand',
187
        'om'   => 'Oman',
188
        'org'  => 'Non-Profit Making Organisations (sic)',
189
        'pa'   => 'Panama',
190
        'pe'   => 'Peru',
191
        'pf'   => 'Polynesia (French)',
192
        'pg'   => 'Papua New Guinea',
193
        'ph'   => 'Philippines',
194
        'pk'   => 'Pakistan',
195
        'pl'   => 'Poland',
196
        'pm'   => 'Saint Pierre and Miquelon',
197
        'pn'   => 'Pitcairn Island',
198
        'pr'   => 'Puerto Rico',
199
        'pt'   => 'Portugal',
200
        'pw'   => 'Palau',
201
        'py'   => 'Paraguay',
202
        'qa'   => 'Qatar',
203
        're'   => 'Reunion (French)',
204
        'ro'   => 'Romania',
205
        'ru'   => 'Russian Federation',
206
        'rw'   => 'Rwanda',
207
        'sa'   => 'Saudi Arabia',
208
        'sb'   => 'Solomon Islands',
209
        'sc'   => 'Seychelles',
210
        'sd'   => 'Sudan',
211
        'se'   => 'Sweden',
212
        'sg'   => 'Singapore',
213
        'sh'   => 'Saint Helena',
214
        'si'   => 'Slovenia',
215
        'sj'   => 'Svalbard and Jan Mayen Islands',
216
        'sk'   => 'Slovak Republic',
217
        'sl'   => 'Sierra Leone',
218
        'sm'   => 'San Marino',
219
        'sn'   => 'Senegal',
220
        'so'   => 'Somalia',
221
        'sr'   => 'Suriname',
222
        'st'   => 'Saint Tome (Sao Tome) and Principe',
223
        'su'   => 'Former USSR',
224
        'sv'   => 'El Salvador',
225
        'sy'   => 'Syria',
226
        'sz'   => 'Swaziland',
227
        'tc'   => 'Turks and Caicos Islands',
228
        'td'   => 'Chad',
229
        'tf'   => 'French Southern Territories',
230
        'tg'   => 'Togo',
231
        'th'   => 'Thailand',
232
        'tj'   => 'Tadjikistan',
233
        'tk'   => 'Tokelau',
234
        'tm'   => 'Turkmenistan',
235
        'tn'   => 'Tunisia',
236
        'to'   => 'Tonga',
237
        'tp'   => 'East Timor',
238
        'tr'   => 'Turkey',
239
        'tt'   => 'Trinidad and Tobago',
240
        'tv'   => 'Tuvalu',
241
        'tw'   => 'Taiwan',
242
        'tz'   => 'Tanzania',
243
        'ua'   => 'Ukraine',
244
        'ug'   => 'Uganda',
245
        'uk'   => 'United Kingdom',
246
        'um'   => 'USA Minor Outlying Islands',
247
        'us'   => 'United States',
248
        'uy'   => 'Uruguay',
249
        'uz'   => 'Uzbekistan',
250
        'va'   => 'Holy See (Vatican City State)',
251
        'vc'   => 'Saint Vincent & Grenadines',
252
        've'   => 'Venezuela',
253
        'vg'   => 'Virgin Islands (British)',
254
        'vi'   => 'Virgin Islands (USA)',
255
        'vn'   => 'Vietnam',
256
        'vu'   => 'Vanuatu',
257
        'wf'   => 'Wallis and Futuna Islands',
258
        'ws'   => 'Samoa',
259
        'ye'   => 'Yemen',
260
        'yt'   => 'Mayotte',
261
        'yu'   => 'Yugoslavia',
262
        'za'   => 'South Africa',
263
        'zm'   => 'Zambia',
264
        'zr'   => 'Zaire',
265
        'zw'   => 'Zimbabwe',
266
    ];
267
268 4
    public function all()
269
    {
270 4
        return new Collection($this->all);
271
    }
272
273
    /**
274
     * Get the GeoIp instance.
275
     *
276
     * @return \PragmaRX\Support\GeoIp\GeoIp
277
     */
278 4
    public function getGeoIp()
279
    {
280 4
        return $this->geoIp();
281
    }
282
283 4
    public function isValid($cc)
284
    {
285 4
        $cc = strtolower(str_replace('country:', '', $cc));
286
287 4
        return $this->all()->has($cc);
288
    }
289
290
    /**
291
     * Get country code from an IP address.
292
     *
293
     * @param $ip_address
294
     *
295
     * @return string|null
296
     */
297 21
    public function getCountryFromIp($ip_address)
298
    {
299 21
        if ($geo = $this->geoIp()->searchAddr($ip_address)) {
300 7
            return strtolower($geo['country_code']);
301
        }
302 14
    }
303
304
    /**
305
     * Make a country info from a string.
306
     *
307
     * @param $country
308
     *
309
     * @return string
310
     */
311 20
    public function makeCountryFromString($country)
312
    {
313 20
        if ($ips = $this->ipAddress()->isCidr($country)) {
314 1
            $country = $ips[0];
315
        }
316
317 20
        if ($this->validCountry($country)) {
318 3
            return $country;
319
        }
320
321 20
        if ($this->dataRepository()->ipIsValid($country)) {
322 20
            $country = $this->getCountryFromIp($this->ipAddress()->hostToIp($country));
323
        }
324
325 20
        return "country:{$country}";
326
    }
327
328
    /**
329
     * Check if a string is a valid country info.
330
     *
331
     * @param $country
332
     *
333
     * @return bool
334
     */
335 27
    public function validCountry($country)
336
    {
337 27
        $country = strtolower($country);
338
339 27
        if ($this->config()->get('enable_country_search')) {
340 21
            if (starts_with($country, 'country:') && $this->isValid($country)) {
341 4
                return true;
342
            }
343
        }
344
345 27
        return false;
346
    }
347
}
348