Completed
Push — master ( 07bfac...b814cf )
by Kamran
02:37
created

LocationInfo::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
1
<?php
2
namespace kamranahmedse;
3
4
class LocationInfo
5
{
6
    private $address = '';
7
    private $latitude = '';
8
    private $longitude = '';
9
    private $country = '';
10
    private $locality = '';
11
    private $district = '';
12
    private $postcode = '';
13
    private $town = '';
14
    private $streetNumber = '';
15
    private $streetAddress = '';
16
17
    private $isValidInfo = true;
18
19 3
    public function __construct($address, \stdClass $dataFromGoogleMaps)
20
    {
21 3
        $this->address = $address;
22 3
        $this->mapKeys($dataFromGoogleMaps);
23 3
    }
24
25 3
    public function isValid()
26
    {
27 3
        return $this->isValidInfo;
28
    }
29
30 3
    private function mapKeys(\stdClass $info)
31
    {
32 3
        if (!property_exists($info, 'results')) {
33 1
            $this->isValidInfo = false;
34 1
            return;
35
        }
36 2
        $this->latitude = $info->results[0]->geometry->location->lat;
37 2
        $this->longitude = $info->results[0]->geometry->location->lng;
38 2
        foreach ($info->results[0]->address_components as $component) {
39 2
            if (in_array('street_number', $component->types)) {
40 2
                $this->streetNumber = $component->long_name;
41 2
            } elseif (in_array('locality', $component->types)) {
42 2
                $this->locality = $component->long_name;
43 2
            } elseif (in_array('postal_town', $component->types)) {
44
                $this->town = $component->long_name;
45 2
            } elseif (in_array('administrative_area_level_2', $component->types)) {
46 2
                $this->country = $component->long_name;
47 2
            } elseif (in_array('country', $component->types)) {
48 2
                $this->country = $component->long_name;
49 2
            } elseif (in_array('administrative_area_level_1', $component->types)) {
50 2
                $this->district = $component->long_name;
51 2
            } elseif (in_array('postal_code', $component->types)) {
52 2
                $this->postcode = $component->long_name;
53 2
            } elseif (in_array('route', $component->types)) {
54 2
                $this->streetAddress = $component->long_name;
55 2
            }
56 2
        }
57 2
    }
58
59 2
    public function getAddress()
60
    {
61 2
        return $this->address;
62
    }
63
64
    public function getLatitude()
65
    {
66
        return $this->latitude;
67
    }
68
69
    public function getLongitude()
70
    {
71
        return $this->longitude;
72
    }
73
74 2
    public function getCountry()
75
    {
76 2
        return $this->country;
77
    }
78
79 2
    public function getLocality()
80
    {
81 2
        return $this->locality;
82
    }
83
84 2
    public function getDistrict()
85
    {
86 2
        return $this->district;
87
    }
88
89 2
    public function getPostcode()
90
    {
91 2
        return $this->postcode;
92
    }
93
94 1
    public function getTown()
95
    {
96 1
        return $this->town;
97
    }
98
99 2
    public function getStreetNumber()
100
    {
101 2
        return $this->streetNumber;
102
    }
103
104 1
    public function getStreetAddress()
105
    {
106 1
        return $this->streetAddress;
107
    }
108
}
109