Completed
Branch master (b9a551)
by Arthur
04:27 queued 02:10
created

PlaceHydrator   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 181
Duplicated Lines 9.94 %

Coupling/Cohesion

Components 1
Dependencies 15

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 18
loc 181
wmc 27
lcom 1
cbo 15
rs 9.1666

13 Methods

Rating   Name   Duplication   Size   Complexity  
A hydratePlaces() 0 21 3
A hydratePlace() 18 18 1
A setPlaceId() 0 6 2
A setName() 0 6 2
A setLocation() 0 7 2
A setTypes() 0 10 3
A setVicinity() 0 6 2
A setFormattedAddress() 0 6 2
A setFormattedPhoneNumber() 0 6 2
A setIcon() 0 6 2
A setInternationalPhoneNumber() 0 6 2
A setUrl() 0 6 2
A setWebsite() 0 6 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Arthem\GoogleApi\Infrastructure\Place\Hydrator;
4
5
use Arthem\GoogleApi\Domain\Place\Place;
6
use Arthem\GoogleApi\Domain\Place\VO\FormattedAddress;
7
use Arthem\GoogleApi\Domain\Place\VO\FormattedPhoneNumber;
8
use Arthem\GoogleApi\Domain\Place\VO\Icon;
9
use Arthem\GoogleApi\Domain\Place\VO\InternationalPhoneNumber;
10
use Arthem\GoogleApi\Domain\Place\VO\Location;
11
use Arthem\GoogleApi\Domain\Place\VO\PlaceCollection;
12
use Arthem\GoogleApi\Domain\Place\VO\PlaceId;
13
use Arthem\GoogleApi\Domain\Place\VO\PlaceName;
14
use Arthem\GoogleApi\Domain\Place\VO\Type;
15
use Arthem\GoogleApi\Domain\Place\VO\TypeCollection;
16
use Arthem\GoogleApi\Domain\Place\VO\Url;
17
use Arthem\GoogleApi\Domain\Place\VO\Vicinity;
18
use Arthem\GoogleApi\Domain\Place\VO\Website;
19
use Arthem\GooglePlaces\Infrastructure\Place\Exception\ResponseErrorException;
20
21
class PlaceHydrator
22
{
23
    /**
24
     * @param array $placesData
25
     *
26
     * @return PlaceCollection
27
     *
28
     * @throws ResponseErrorException When data is malformed
29
     */
30
    public function hydratePlaces(array $placesData)
31
    {
32
        $places = new PlaceCollection();
33
34
        foreach ($placesData as $placeData) {
35
            if (!is_array($placeData)) {
36
                throw new ResponseErrorException(
37
                    sprintf(
38
                        'place data is expected to be array, got %s',
39
                        gettype(
40
                            $placeData
41
                        )
42
                    )
43
                );
44
            }
45
46
            $places->addPlace($this->hydratePlace($placeData));
47
        }
48
49
        return $places;
50
    }
51
52
    /**
53
     * @param array $data
54
     *
55
     * @return Place
56
     */
57 View Code Duplication
    public function hydratePlace(array $data)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
    {
59
        $place = new Place();
60
61
        $this->setPlaceId($place, $data);
62
        $this->setName($place, $data);
63
        $this->setLocation($place, $data);
64
        $this->setTypes($place, $data);
65
        $this->setVicinity($place, $data);
66
        $this->setFormattedAddress($place, $data);
67
        $this->setFormattedPhoneNumber($place, $data);
68
        $this->setIcon($place, $data);
69
        $this->setInternationalPhoneNumber($place, $data);
70
        $this->setUrl($place, $data);
71
        $this->setWebsite($place, $data);
72
73
        return $place;
74
    }
75
76
    /**
77
     * @param Place $place
78
     * @param array $data
79
     */
80
    private function setPlaceId(Place $place, array $data)
81
    {
82
        if (!empty($data['place_id'])) {
83
            $place->setId(new PlaceId($data['place_id']));
84
        }
85
    }
86
87
    /**
88
     * @param Place $place
89
     * @param array $data
90
     */
91
    private function setName(Place $place, array $data)
92
    {
93
        if (!empty($data['name'])) {
94
            $place->setName(new PlaceName($data['name']));
95
        }
96
    }
97
98
    /**
99
     * @param Place $place
100
     * @param array $data
101
     */
102
    private function setLocation(Place $place, array $data)
103
    {
104
        if (!empty($data['geometry']['location'])) {
105
            $location = $data['geometry']['location'];
106
            $place->setLocation(new Location($location['lat'], $location['lng']));
107
        }
108
    }
109
110
    /**
111
     * @param Place $place
112
     * @param array $data
113
     */
114
    private function setTypes(Place $place, array $data)
115
    {
116
        if (!empty($data['types'])) {
117
            $types = [];
118
            foreach ($data['types'] as $typeData) {
119
                $types[] = new Type($typeData);
120
            }
121
            $place->setTypes(new TypeCollection($types));
122
        }
123
    }
124
125
    /**
126
     * @param Place $place
127
     * @param array $data
128
     */
129
    private function setVicinity(Place $place, array $data)
130
    {
131
        if (!empty($data['vicinity'])) {
132
            $place->setVicinity(new Vicinity($data['vicinity']));
133
        }
134
    }
135
136
    /**
137
     * @param Place $place
138
     * @param array $data
139
     */
140
    private function setFormattedAddress(Place $place, array $data)
141
    {
142
        if (!empty($data['formatted_address'])) {
143
            $place->setFormattedAddress(new FormattedAddress($data['formatted_address']));
144
        }
145
    }
146
147
    /**
148
     * @param Place $place
149
     * @param array $data
150
     */
151
    private function setFormattedPhoneNumber(Place $place, array $data)
152
    {
153
        if (!empty($data['formatted_phone_number'])) {
154
            $place->setFormattedPhoneNumber(new FormattedPhoneNumber($data['formatted_phone_number']));
155
        }
156
    }
157
158
    /**
159
     * @param Place $place
160
     * @param array $data
161
     */
162
    private function setIcon(Place $place, array $data)
163
    {
164
        if (!empty($data['icon'])) {
165
            $place->setIcon(new Icon($data['icon']));
166
        }
167
    }
168
169
    /**
170
     * @param Place $place
171
     * @param array $data
172
     */
173
    private function setInternationalPhoneNumber(Place $place, array $data)
174
    {
175
        if (!empty($data['international_phone_number'])) {
176
            $place->setInternationalPhoneNumber(new InternationalPhoneNumber($data['international_phone_number']));
177
        }
178
    }
179
180
    /**
181
     * @param Place $place
182
     * @param array $data
183
     */
184
    private function setUrl(Place $place, array $data)
185
    {
186
        if (!empty($data['url'])) {
187
            $place->setUrl(new Url($data['url']));
188
        }
189
    }
190
191
    /**
192
     * @param Place $place
193
     * @param array $data
194
     */
195
    private function setWebsite(Place $place, array $data)
196
    {
197
        if (!empty($data['website'])) {
198
            $place->setWebsite(new Website($data['website']));
199
        }
200
    }
201
}
202