Completed
Branch master (b9a551)
by Arthur
04:27 queued 02:10
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 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\Domain\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['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
            $data['location'] = $place->getLocation();
65
        }
66
    }
67
68
    /**
69
     * @param Place $place
70
     * @param array $data
71
     */
72
    private function setTypes(Place $place, array &$data)
73
    {
74
        $types = $this->serializeTypes($place->getTypes());
75
        if (!empty($types)) {
76
            $data['types'] = $types;
77
        }
78
    }
79
80
    /**
81
     * @param Place $place
82
     * @param array $data
83
     */
84
    private function setIcon(Place $place, array &$data)
85
    {
86
        if (null !== $place->getIcon()) {
87
            $data['icon'] = $place->getIcon()->getUrl();
88
        }
89
    }
90
91
    /**
92
     * @param Place $place
93
     * @param array $data
94
     */
95
    private function setVicinity(Place $place, array &$data)
96
    {
97
        if (null !== $place->getVicinity()) {
98
            $data['vicinity'] = $place->getVicinity()->getAddress();
99
        }
100
    }
101
102
    /**
103
     * @param Place $place
104
     * @param array $data
105
     */
106
    private function setFormattedAddress(Place $place, array &$data)
107
    {
108
        if (null !== $place->getFormattedAddress()) {
109
            $data['formatted_address'] = $place->getFormattedAddress()->getAddress();
110
        }
111
    }
112
113
    /**
114
     * @param Place $place
115
     * @param array $data
116
     */
117
    private function setFormattedPhoneNumber(Place $place, array &$data)
118
    {
119
        if (null !== $place->getFormattedPhoneNumber()) {
120
            $data['formatted_phone_number'] = $place->getFormattedPhoneNumber()->getNumber();
121
        }
122
    }
123
124
    /**
125
     * @param Place $place
126
     * @param array $data
127
     */
128
    private function setInternationalPhoneNumber(Place $place, array &$data)
129
    {
130
        if (null !== $place->getInternationalPhoneNumber()) {
131
            $data['international_phone_number'] = $place->getInternationalPhoneNumber()->getNumber();
132
        }
133
    }
134
135
    /**
136
     * @param Place $place
137
     * @param array $data
138
     */
139
    private function setUrl(Place $place, array &$data)
140
    {
141
        if (null !== $place->getUrl()) {
142
            $data['url'] = $place->getUrl()->getUrl();
143
        }
144
    }
145
146
    /**
147
     * @param Place $place
148
     * @param array $data
149
     */
150
    private function setWebsite(Place $place, array &$data)
151
    {
152
        if (null !== $place->getWebsite()) {
153
            $data['website'] = $place->getWebsite()->getUrl();
154
        }
155
    }
156
157
    /**
158
     * @param TypeCollection $types
159
     *
160
     * @return array
161
     */
162
    private function serializeTypes(TypeCollection $types)
163
    {
164
        $typesData = [];
165
        /** @var Type $type */
166
        foreach ($types as $type) {
167
            $typesData[] = $type->getType();
168
        }
169
170
        return $typesData;
171
    }
172
}
173