Completed
Pull Request — master (#17)
by Romain
03:33
created

Address   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 176
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 95.35%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 0
dl 0
loc 176
ccs 41
cts 43
cp 0.9535
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A getStreet() 0 4 1
A setAdditionalStreet() 0 6 1
A getAdditionalStreet() 0 4 1
A getCity() 0 4 1
A getPostalCode() 0 4 1
A getState() 0 4 1
A getCountry() 0 4 1
A setId() 0 6 1
A getId() 0 4 1
A jsonSerialize() 0 14 1
A create() 0 14 3
1
<?php
2
namespace Kerox\Messenger\Model\Common;
3
4
class Address implements \JsonSerializable
5
{
6
7
    /**
8
     * @var string
9
     */
10
    protected $street;
11
12
    /**
13
     * @var null|string
14
     */
15
    protected $additionalStreet;
16
17
    /**
18
     * @var string
19
     */
20
    protected $city;
21
22
    /**
23
     * @var string
24
     */
25
    protected $postalCode;
26
27
    /**
28
     * @var string
29
     */
30
    protected $state;
31
32
    /**
33
     * @var string
34
     */
35
    protected $country;
36
37
    /**
38
     * @var null|int
39
     */
40
    protected $id;
41
42
    /**
43
     * Address constructor.
44
     *
45
     * @param string $street
46
     * @param string $city
47
     * @param string $postalCode
48
     * @param string $state
49
     * @param string $country
50
     */
51 11
    public function __construct(
52
        string $street,
53
        string $city,
54
        string $postalCode,
55
        string $state,
56
        string $country
57
    ) {
58 11
        $this->street = $street;
59 11
        $this->city = $city;
60 11
        $this->postalCode = $postalCode;
61 11
        $this->state = $state;
62 11
        $this->country = $country;
63 11
    }
64
65
    /**
66
     * @return string
67
     */
68 1
    public function getStreet(): string
69
    {
70 1
        return $this->street;
71
    }
72
73
    /**
74
     * @param string $additionalStreet
75
     * @return Address
76
     */
77 8
    public function setAdditionalStreet(string $additionalStreet): Address
78
    {
79 8
        $this->additionalStreet = $additionalStreet;
80
81 8
        return $this;
82
    }
83
84
    /**
85
     * @return null|string
86
     */
87 1
    public function getAdditionalStreet()
88
    {
89 1
        return $this->additionalStreet;
90
    }
91
92
    /**
93
     * @return string
94
     */
95 1
    public function getCity(): string
96
    {
97 1
        return $this->city;
98
    }
99
100
    /**
101
     * @return string
102
     */
103 1
    public function getPostalCode(): string
104
    {
105 1
        return $this->postalCode;
106
    }
107
108
    /**
109
     * @return string
110
     */
111 1
    public function getState(): string
112
    {
113 1
        return $this->state;
114
    }
115
116
    /**
117
     * @return string
118
     */
119 1
    public function getCountry(): string
120
    {
121 1
        return $this->country;
122
    }
123
124
    /**
125
     * @param int $id
126
     * @return Address
127
     */
128 1
    public function setId(int $id)
129
    {
130 1
        $this->id = $id;
131
132 1
        return $this;
133
    }
134
135
    /**
136
     * @return null|int
137
     */
138
    public function getId()
139
    {
140
        return $this->id;
141
    }
142
143
    /**
144
     * @return array
145
     */
146 5
    public function jsonSerialize(): array
147
    {
148
        $json = [
149 5
            'street_1' => $this->street,
150 5
            'street_2' => $this->additionalStreet,
151 5
            'city' => $this->city,
152 5
            'postal_code' => $this->postalCode,
153 5
            'state' => $this->state,
154 5
            'country' => $this->country,
155 5
            'id' => $this->id,
156
        ];
157
158 5
        return array_filter($json);
159
    }
160
161
    /**
162
     * @param array $payload
163
     * @return static
164
     */
165 6
    public static function create(array $payload)
166
    {
167 6
        $address = new static($payload['street_1'], $payload['city'], $payload['postal_code'], $payload['state'], $payload['country']);
168
169 6
        if (isset($payload['street_2'])) {
170 6
            $address->setAdditionalStreet($payload['street_2']);
171
        }
172
173 6
        if (isset($payload['id'])) {
174 1
            $address->setId($payload['id']);
175
        }
176
177 6
        return $address;
178
    }
179
}
180