Completed
Pull Request — master (#77)
by Romain
03:43 queued 01:15
created

Address::getState()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Kerox\Messenger\Model\Common;
4
5
class Address implements \JsonSerializable
6
{
7
    /**
8
     * @var null|string
9
     */
10
    protected $name;
11
12
    /**
13
     * @var string
14
     */
15
    protected $street;
16
17
    /**
18
     * @var null|string
19
     */
20
    protected $additionalStreet;
21
22
    /**
23
     * @var string
24
     */
25
    protected $city;
26
27
    /**
28
     * @var string
29
     */
30
    protected $postalCode;
31
32
    /**
33
     * @var string
34
     */
35
    protected $state;
36
37
    /**
38
     * @var string
39
     */
40
    protected $country;
41
42
    /**
43
     * @var null|int
44
     */
45
    protected $id;
46
47
    /**
48
     * Address constructor.
49
     *
50
     * @param string $street
51
     * @param string $city
52
     * @param string $postalCode
53
     * @param string $state
54
     * @param string $country
55
     */
56 16
    public function __construct(
57
        string $street,
58
        string $city,
59
        string $postalCode,
60
        string $state,
61
        string $country
62
    ) {
63 16
        $this->street = $street;
64 16
        $this->city = $city;
65 16
        $this->postalCode = $postalCode;
66 16
        $this->state = $state;
67 16
        $this->country = $country;
68 16
    }
69
70
    /**
71
     * @param string $name
72
     *
73
     * @return \Kerox\Messenger\Model\Common\Address
74
     */
75 4
    public function setName(string $name): self
76
    {
77 4
        $this->name = $name;
78
79 4
        return $this;
80
    }
81
82
    /**
83
     * @return null|string
84
     */
85 1
    public function getName(): ?string
86
    {
87 1
        return $this->name;
88
    }
89
90
    /**
91
     * @return string
92
     */
93 3
    public function getStreet(): string
94
    {
95 3
        return $this->street;
96
    }
97
98
    /**
99
     * @param string $additionalStreet
100
     *
101
     * @return Address
102
     */
103 13
    public function setAdditionalStreet(string $additionalStreet): self
104
    {
105 13
        $this->additionalStreet = $additionalStreet;
106
107 13
        return $this;
108
    }
109
110
    /**
111
     * @return null|string
112
     */
113 3
    public function getAdditionalStreet(): ?string
114
    {
115 3
        return $this->additionalStreet;
116
    }
117
118
    /**
119
     * @return string
120
     */
121 3
    public function getCity(): string
122
    {
123 3
        return $this->city;
124
    }
125
126
    /**
127
     * @return string
128
     */
129 3
    public function getPostalCode(): string
130
    {
131 3
        return $this->postalCode;
132
    }
133
134
    /**
135
     * @return string
136
     */
137 3
    public function getState(): string
138
    {
139 3
        return $this->state;
140
    }
141
142
    /**
143
     * @return string
144
     */
145 3
    public function getCountry(): string
146
    {
147 3
        return $this->country;
148
    }
149
150
    /**
151
     * @param int $id
152
     *
153
     * @return Address
154
     */
155 3
    public function setId(int $id): self
156
    {
157 3
        $this->id = $id;
158
159 3
        return $this;
160
    }
161
162
    /**
163
     * @return null|int
164
     */
165 1
    public function getId(): ?int
166
    {
167 1
        return $this->id;
168
    }
169
170
    /**
171
     * @return array
172
     */
173 4
    public function jsonSerialize(): array
174
    {
175
        $json = [
176 4
            'name'        => $this->name,
177 4
            'street_1'    => $this->street,
178 4
            'street_2'    => $this->additionalStreet,
179 4
            'city'        => $this->city,
180 4
            'postal_code' => $this->postalCode,
181 4
            'state'       => $this->state,
182 4
            'country'     => $this->country,
183 4
            'id'          => $this->id,
184
        ];
185
186 4
        return array_filter($json);
187
    }
188
189
    /**
190
     * @param array $payload
191
     *
192
     * @return static
193
     */
194 12
    public static function create(array $payload)
195
    {
196 12
        $address = new static(
197 12
            $payload['street_1'],
198 12
            $payload['city'],
199 12
            $payload['postal_code'],
200 12
            $payload['state'],
201 12
            $payload['country']
202
        );
203
204 12
        if (isset($payload['street_2'])) {
205 12
            $address->setAdditionalStreet($payload['street_2']);
206
        }
207
208 12
        if (isset($payload['id'])) {
209 3
            $address->setId($payload['id']);
210
        }
211
212 12
        if (isset($payload['name'])) {
213 4
            $address->setName($payload['name']);
214
        }
215
216 12
        return $address;
217
    }
218
}
219