Completed
Push — master ( 8d8c5c...633ae1 )
by Arthur
02:18
created

PlaceSerializer::setFormattedAddress()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 2
1
<?php
2
3
namespace Arthem\GoogleApi\Infrastructure\Place\Serializer;
4
5
use Arthem\GoogleApi\Domain\Place\Place;
6
use Arthem\GoogleApi\Domain\Place\VO\Type;
7
use Arthem\GoogleApi\Domain\Place\VO\TypeCollection;
8
9
class PlaceSerializer
10
{
11
    /**
12
     * @param Place $place
13
     *
14
     * @return array
15
     */
16 View Code Duplication
    public function serialize(Place $place)
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...
17
    {
18
        $data = [];
19
20
        $this->setId($place, $data);
21
        $this->setName($place, $data);
22
        $this->setLocation($place, $data);
23
        $this->setTypes($place, $data);
24
        $this->setIcon($place, $data);
25
        $this->setVicinity($place, $data);
26
        $this->setFormattedAddress($place, $data);
27
        $this->setFormattedPhoneNumber($place, $data);
28
        $this->setInternationalPhoneNumber($place, $data);
29
        $this->setUrl($place, $data);
30
        $this->setWebsite($place, $data);
31
32
        return $data;
33
    }
34
35
    /**
36
     * @param Place $place
37
     * @param array $data
38
     */
39
    private function setId(Place $place, array &$data)
40
    {
41
        if (null !== $place->getId()) {
42
            $data['place_id'] = $place->getId()->getId();
43
        }
44
    }
45
46
    /**
47
     * @param Place $place
48
     * @param array $data
49
     */
50
    private function setName(Place $place, array &$data)
51
    {
52
        if (null !== $place->getName()) {
53
            $data['name'] = $place->getName()->getName();
54
        }
55
    }
56
57
    /**
58
     * @param Place $place
59
     * @param array $data
60
     */
61
    private function setLocation(Place $place, array &$data)
62
    {
63
        if (null !== $place->getLocation()) {
64
            $this->createGeometryNode($data);
65
66
            $data['geometry']['location'] = [
67
                'lat' => $place->getLocation()->getLatitude(),
68
                'lng' => $place->getLocation()->getLongitude(),
69
            ];
70
        }
71
    }
72
73
    private function createGeometryNode(array &$data)
74
    {
75
        if (!isset($data['geometry'])) {
76
            $data['geometry'] = [];
77
        }
78
    }
79
80
    /**
81
     * @param Place $place
82
     * @param array $data
83
     */
84
    private function setTypes(Place $place, array &$data)
85
    {
86
        $types = $this->serializeTypes($place->getTypes());
87
        if (!empty($types)) {
88
            $data['types'] = $types;
89
        }
90
    }
91
92
    /**
93
     * @param Place $place
94
     * @param array $data
95
     */
96
    private function setIcon(Place $place, array &$data)
97
    {
98
        if (null !== $place->getIcon()) {
99
            $data['icon'] = $place->getIcon()->getUrl();
100
        }
101
    }
102
103
    /**
104
     * @param Place $place
105
     * @param array $data
106
     */
107
    private function setVicinity(Place $place, array &$data)
108
    {
109
        if (null !== $place->getVicinity()) {
110
            $data['vicinity'] = $place->getVicinity()->getAddress();
111
        }
112
    }
113
114
    /**
115
     * @param Place $place
116
     * @param array $data
117
     */
118
    private function setFormattedAddress(Place $place, array &$data)
119
    {
120
        if (null !== $place->getFormattedAddress()) {
121
            $data['formatted_address'] = $place->getFormattedAddress()->getAddress();
122
        }
123
    }
124
125
    /**
126
     * @param Place $place
127
     * @param array $data
128
     */
129
    private function setFormattedPhoneNumber(Place $place, array &$data)
130
    {
131
        if (null !== $place->getFormattedPhoneNumber()) {
132
            $data['formatted_phone_number'] = $place->getFormattedPhoneNumber()->getNumber();
133
        }
134
    }
135
136
    /**
137
     * @param Place $place
138
     * @param array $data
139
     */
140
    private function setInternationalPhoneNumber(Place $place, array &$data)
141
    {
142
        if (null !== $place->getInternationalPhoneNumber()) {
143
            $data['international_phone_number'] = $place->getInternationalPhoneNumber()->getNumber();
144
        }
145
    }
146
147
    /**
148
     * @param Place $place
149
     * @param array $data
150
     */
151
    private function setUrl(Place $place, array &$data)
152
    {
153
        if (null !== $place->getUrl()) {
154
            $data['url'] = $place->getUrl()->getUrl();
155
        }
156
    }
157
158
    /**
159
     * @param Place $place
160
     * @param array $data
161
     */
162
    private function setWebsite(Place $place, array &$data)
163
    {
164
        if (null !== $place->getWebsite()) {
165
            $data['website'] = $place->getWebsite()->getUrl();
166
        }
167
    }
168
169
    /**
170
     * @param TypeCollection $types
171
     *
172
     * @return array
173
     */
174
    private function serializeTypes(TypeCollection $types)
175
    {
176
        $typesData = [];
177
        /** @var Type $type */
178
        foreach ($types as $type) {
179
            $typesData[] = $type->getType();
180
        }
181
182
        return $typesData;
183
    }
184
}
185