Passed
Push — master ( 9d8e5a...c85e00 )
by Romain
01:15
created

Address::create()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4

Importance

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