Completed
Pull Request — master (#3)
by Arthur
02:31
created

PlaceHydrator   C

Complexity

Total Complexity 33

Size/Duplication

Total Lines 223
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 19

Importance

Changes 3
Bugs 0 Features 2
Metric Value
wmc 33
c 3
b 0
f 2
lcom 1
cbo 19
dl 0
loc 223
rs 6.4625

15 Methods

Rating   Name   Duplication   Size   Complexity  
A hydratePlaces() 0 21 3
A hydratePlace() 0 20 1
A setPlaceId() 0 6 2
A setName() 0 6 2
A setLocation() 0 7 2
A setTypes() 0 10 3
A setAddressComponents() 0 15 3
A setPhotos() 0 15 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
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\AddressComponent;
7
use Arthem\GoogleApi\Domain\Place\VO\FormattedAddress;
8
use Arthem\GoogleApi\Domain\Place\VO\FormattedPhoneNumber;
9
use Arthem\GoogleApi\Domain\Place\VO\Icon;
10
use Arthem\GoogleApi\Domain\Place\VO\InternationalPhoneNumber;
11
use Arthem\GoogleApi\Domain\Place\VO\Location;
12
use Arthem\GoogleApi\Domain\Place\VO\Photo;
13
use Arthem\GoogleApi\Domain\Place\VO\PlaceCollection;
14
use Arthem\GoogleApi\Domain\Place\VO\PlaceId;
15
use Arthem\GoogleApi\Domain\Place\VO\PlaceName;
16
use Arthem\GoogleApi\Domain\Place\VO\Type;
17
use Arthem\GoogleApi\Domain\Place\VO\TypeCollection;
18
use Arthem\GoogleApi\Domain\Place\VO\Url;
19
use Arthem\GoogleApi\Domain\Place\VO\Vicinity;
20
use Arthem\GoogleApi\Domain\Place\VO\Website;
21
use Arthem\GooglePlaces\Infrastructure\Place\Exception\ResponseErrorException;
22
23
class PlaceHydrator
24
{
25
    /**
26
     * @param array $placesData
27
     *
28
     * @return PlaceCollection
29
     *
30
     * @throws ResponseErrorException When data is malformed
31
     */
32
    public function hydratePlaces(array $placesData)
33
    {
34
        $places = new PlaceCollection();
35
36
        foreach ($placesData as $placeData) {
37
            if (!is_array($placeData)) {
38
                throw new ResponseErrorException(
39
                    sprintf(
40
                        'place data is expected to be array, got %s',
41
                        gettype(
42
                            $placeData
43
                        )
44
                    )
45
                );
46
            }
47
48
            $places->addPlace($this->hydratePlace($placeData));
49
        }
50
51
        return $places;
52
    }
53
54
    /**
55
     * @param array $data
56
     *
57
     * @return Place
58
     */
59
    public function hydratePlace(array $data)
60
    {
61
        $place = new Place();
62
63
        $this->setPlaceId($place, $data);
64
        $this->setName($place, $data);
65
        $this->setLocation($place, $data);
66
        $this->setTypes($place, $data);
67
        $this->setVicinity($place, $data);
68
        $this->setFormattedAddress($place, $data);
69
        $this->setFormattedPhoneNumber($place, $data);
70
        $this->setIcon($place, $data);
71
        $this->setInternationalPhoneNumber($place, $data);
72
        $this->setUrl($place, $data);
73
        $this->setWebsite($place, $data);
74
        $this->setAddressComponents($place, $data);
75
        $this->setPhotos($place, $data);
76
77
        return $place;
78
    }
79
80
    /**
81
     * @param Place $place
82
     * @param array $data
83
     */
84
    private function setPlaceId(Place $place, array $data)
85
    {
86
        if (!empty($data['place_id'])) {
87
            $place->setId(new PlaceId($data['place_id']));
88
        }
89
    }
90
91
    /**
92
     * @param Place $place
93
     * @param array $data
94
     */
95
    private function setName(Place $place, array $data)
96
    {
97
        if (!empty($data['name'])) {
98
            $place->setName(new PlaceName($data['name']));
99
        }
100
    }
101
102
    /**
103
     * @param Place $place
104
     * @param array $data
105
     */
106
    private function setLocation(Place $place, array $data)
107
    {
108
        if (!empty($data['geometry']['location'])) {
109
            $location = $data['geometry']['location'];
110
            $place->setLocation(new Location($location['lat'], $location['lng']));
111
        }
112
    }
113
114
    /**
115
     * @param Place $place
116
     * @param array $data
117
     */
118
    private function setTypes(Place $place, array $data)
119
    {
120
        if (!empty($data['types'])) {
121
            $types = [];
122
            foreach ($data['types'] as $typeData) {
123
                $types[] = new Type($typeData);
124
            }
125
            $place->setTypes(new TypeCollection($types));
126
        }
127
    }
128
129
    /**
130
     * @param Place $place
131
     * @param array $data
132
     */
133
    private function setAddressComponents(Place $place, array $data)
134
    {
135
        if (!empty($data['address_components'])) {
136
            $components = $place->getAddressComponents();
137
            foreach ($data['address_components'] as $componentData) {
138
                $components->addComponent(
139
                    new AddressComponent(
140
                        $componentData['long_name'],
141
                        $componentData['short_name'],
142
                        $componentData['types']
143
                    )
144
                );
145
            }
146
        }
147
    }
148
149
    /**
150
     * @param Place $place
151
     * @param array $data
152
     */
153
    private function setPhotos(Place $place, array $data)
154
    {
155
        if (!empty($data['photos'])) {
156
            $photos = $place->getPhotos();
157
            foreach ($data['photos'] as $photoData) {
158
                $photos->addPhoto(
159
                    new Photo(
160
                        $photoData['photo_reference'],
161
                        $photoData['width'],
162
                        $photoData['height']
163
                    )
164
                );
165
            }
166
        }
167
    }
168
169
    /**
170
     * @param Place $place
171
     * @param array $data
172
     */
173
    private function setVicinity(Place $place, array $data)
174
    {
175
        if (!empty($data['vicinity'])) {
176
            $place->setVicinity(new Vicinity($data['vicinity']));
177
        }
178
    }
179
180
    /**
181
     * @param Place $place
182
     * @param array $data
183
     */
184
    private function setFormattedAddress(Place $place, array $data)
185
    {
186
        if (!empty($data['formatted_address'])) {
187
            $place->setFormattedAddress(new FormattedAddress($data['formatted_address']));
188
        }
189
    }
190
191
    /**
192
     * @param Place $place
193
     * @param array $data
194
     */
195
    private function setFormattedPhoneNumber(Place $place, array $data)
196
    {
197
        if (!empty($data['formatted_phone_number'])) {
198
            $place->setFormattedPhoneNumber(new FormattedPhoneNumber($data['formatted_phone_number']));
199
        }
200
    }
201
202
    /**
203
     * @param Place $place
204
     * @param array $data
205
     */
206
    private function setIcon(Place $place, array $data)
207
    {
208
        if (!empty($data['icon'])) {
209
            $place->setIcon(new Icon($data['icon']));
210
        }
211
    }
212
213
    /**
214
     * @param Place $place
215
     * @param array $data
216
     */
217
    private function setInternationalPhoneNumber(Place $place, array $data)
218
    {
219
        if (!empty($data['international_phone_number'])) {
220
            $place->setInternationalPhoneNumber(new InternationalPhoneNumber($data['international_phone_number']));
221
        }
222
    }
223
224
    /**
225
     * @param Place $place
226
     * @param array $data
227
     */
228
    private function setUrl(Place $place, array $data)
229
    {
230
        if (!empty($data['url'])) {
231
            $place->setUrl(new Url($data['url']));
232
        }
233
    }
234
235
    /**
236
     * @param Place $place
237
     * @param array $data
238
     */
239
    private function setWebsite(Place $place, array $data)
240
    {
241
        if (!empty($data['website'])) {
242
            $place->setWebsite(new Website($data['website']));
243
        }
244
    }
245
}
246