Completed
Push — master ( ba0dc9...449151 )
by John
01:49
created

GeoPhone   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 32
rs 10
c 0
b 0
f 0

2 Methods

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