GeoPhone::lookupNumber()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 3
nop 1
dl 0
loc 14
rs 9.2
c 0
b 0
f 0
1
<?php
2
namespace GeoPhone\Models;
3
4
/**
5
 * Class GeoPhone
6
 * @package GeoPhone\Models
7
 */
8
class GeoPhone
9
{
10
    /**
11
     * @var resource
12
     */
13
    protected $fileHandle;
14
15
    /**
16
     * @param string $filename
17
     */
18
    public function __construct($filename = 'data.csv')
19
    {
20
        $this->fileHandle = fopen($filename, 'r');
21
    }
22
23
    /**
24
     * @param String $phoneNumber A phone number in any format
25
     * @return LookupResponse
26
     * City and state are empty when not found.
27
     */
28
    public function lookupNumber($phoneNumber): LookupResponse
29
    {
30
        $phoneNumber = preg_replace('/[^0-9]/s', '', $phoneNumber);
31
        $areaCode = substr($phoneNumber, 0, 3);
32
        $nextThree = substr($phoneNumber, 3, 3);
33
34
        while (($data = fgetcsv($this->fileHandle)) !== false) {
35
            if ($areaCode == $data[0] && $nextThree == $data[1]) {
36
                return new LookupResponse($data[2], $data[3]);
37
            }
38
        }
39
40
        return new LookupResponse('', '');
41
    }
42
}
43