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

PlaceHydrator::setPlaceId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 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
            return;
137
        }
138
139
        $components = $place->getAddressComponents();
140
        foreach ($data['address_components'] as $componentData) {
141
            $components->addComponent(
142
                new AddressComponent(
143
                    $componentData['long_name'],
144
                    $componentData['short_name'],
145
                    $componentData['types']
146
                )
147
            );
148
        }
149
    }
150
151
    /**
152
     * @param Place $place
153
     * @param array $data
154
     */
155
    private function setPhotos(Place $place, array $data)
156
    {
157
        if (empty($data['photos'])) {
158
            return;
159
        }
160
161
        $photos = $place->getPhotos();
162
        foreach ($data['photos'] as $photoData) {
163
            $photos->addPhoto(
164
                new Photo(
165
                    $photoData['photo_reference'],
166
                    $photoData['width'],
167
                    $photoData['height']
168
                )
169
            );
170
        }
171
    }
172
173
    /**
174
     * @param Place $place
175
     * @param array $data
176
     */
177
    private function setVicinity(Place $place, array $data)
178
    {
179
        if (!empty($data['vicinity'])) {
180
            $place->setVicinity(new Vicinity($data['vicinity']));
181
        }
182
    }
183
184
    /**
185
     * @param Place $place
186
     * @param array $data
187
     */
188
    private function setFormattedAddress(Place $place, array $data)
189
    {
190
        if (!empty($data['formatted_address'])) {
191
            $place->setFormattedAddress(new FormattedAddress($data['formatted_address']));
192
        }
193
    }
194
195
    /**
196
     * @param Place $place
197
     * @param array $data
198
     */
199
    private function setFormattedPhoneNumber(Place $place, array $data)
200
    {
201
        if (!empty($data['formatted_phone_number'])) {
202
            $place->setFormattedPhoneNumber(new FormattedPhoneNumber($data['formatted_phone_number']));
203
        }
204
    }
205
206
    /**
207
     * @param Place $place
208
     * @param array $data
209
     */
210
    private function setIcon(Place $place, array $data)
211
    {
212
        if (!empty($data['icon'])) {
213
            $place->setIcon(new Icon($data['icon']));
214
        }
215
    }
216
217
    /**
218
     * @param Place $place
219
     * @param array $data
220
     */
221
    private function setInternationalPhoneNumber(Place $place, array $data)
222
    {
223
        if (!empty($data['international_phone_number'])) {
224
            $place->setInternationalPhoneNumber(new InternationalPhoneNumber($data['international_phone_number']));
225
        }
226
    }
227
228
    /**
229
     * @param Place $place
230
     * @param array $data
231
     */
232
    private function setUrl(Place $place, array $data)
233
    {
234
        if (!empty($data['url'])) {
235
            $place->setUrl(new Url($data['url']));
236
        }
237
    }
238
239
    /**
240
     * @param Place $place
241
     * @param array $data
242
     */
243
    private function setWebsite(Place $place, array $data)
244
    {
245
        if (!empty($data['website'])) {
246
            $place->setWebsite(new Website($data['website']));
247
        }
248
    }
249
}
250