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

LocationInfo::mapKeys()   C

Complexity

Conditions 11
Paths 11

Size

Total Lines 28
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 11.0069

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 23
c 1
b 0
f 0
nc 11
nop 1
dl 0
loc 28
ccs 25
cts 26
cp 0.9615
crap 11.0069
rs 5.2653

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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