InstructionalAddress::toXml()   F
last analyzed

Complexity

Conditions 10
Paths 512

Size

Total Lines 43
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 21
c 1
b 0
f 0
nc 512
nop 1
dl 0
loc 43
rs 3.2187

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php namespace SimpleUPS;
2
3
/**
4
 * An address with additional address lines for room, floor, or department that
5
 * a carrier might need when delivering a package
6
 * @since 1.0
7
 */
8
class InstructionalAddress extends Address
9
{
10
    private
11
        /* @var string $addressee */
12
        $addressee,
13
14
        /* @var string $addressLine2 */
15
        $addressLine2,
16
17
        /* @var string $addressLine3 */
18
        $addressLine3,
19
20
        /* @var bool $isResidential */
21
        $isResidential = false;
22
23
    /**
24
     * Name of the addressee
25
     *
26
     * @param string $addressee
27
     *
28
     * @return InstructionalAddress
29
     */
30
    public function setAddressee($addressee)
31
    {
32
        $this->addressee = (string)$addressee;
33
        return $this;
34
    }
35
36
    /**
37
     * Name of the addressee
38
     * @return string
39
     */
40
    public function getAddressee()
41
    {
42
        return $this->addressee;
43
    }
44
45
    /**
46
     * Additional address information, preferably room or floor
47
     *
48
     * @param string $addressLine2
49
     *
50
     * @return InstructionalAddress
51
     */
52
    public function setAddressLine2($addressLine2)
53
    {
54
        $this->addressLine2 = (string)$addressLine2;
55
        return $this;
56
    }
57
58
    /**
59
     * Additional address information, preferably room or floor
60
     * @return string
61
     */
62
    public function getAddressLine2()
63
    {
64
        return $this->addressLine2;
65
    }
66
67
    /**
68
     * Additional address information, preferably department name
69
     *
70
     * @param string $addressLine3
71
     *
72
     * @return InstructionalAddress
73
     */
74
    public function setAddressLine3($addressLine3)
75
    {
76
        $this->addressLine3 = (string)$addressLine3;
77
        return $this;
78
    }
79
80
    /**
81
     * Additional address information, preferably department name
82
     * @return string
83
     */
84
    public function getAddressLine3()
85
    {
86
        return $this->addressLine3;
87
    }
88
89
    /**
90
     * Define if this address is residential
91
     *
92
     * @param $isResidential
93
     *
94
     * @return InstructionalAddress
95
     */
96
    public function setIsResidential($isResidential)
97
    {
98
        $this->isResidential = $isResidential;
99
        return $this;
100
    }
101
102
    /**
103
     * @internal
104
     * @return bool
105
     */
106
    public function isResidential()
107
    {
108
        return $this->isResidential;
109
    }
110
111
    /**
112
     * @internal
113
     *
114
     * @param \DomDocument $dom
115
     *
116
     * @return \DOMElement
117
     */
118
    public function toXml(\DomDocument $dom)
119
    {
120
        $address = $dom->createElement('Address');
121
122
        if ($this->getAddressee() != null) //no UPS api uses this
123
        {
124
            $address->appendChild($dom->createElement('Addressee', $this->getAddressee()));
125
        }
126
127
        if ($this->getStreet() != null) {
128
            $address->appendChild($dom->createElement('AddressLine1', $this->getStreet()));
129
        }
130
131
        if ($this->getAddressLine2() != null) {
132
            $address->appendChild($dom->createElement('AddressLine2', $this->getAddressLine2()));
133
        }
134
135
        if ($this->getAddressLine3() != null) {
136
            $address->appendChild($dom->createElement('AddressLine3', $this->getAddressLine3()));
137
        }
138
139
        if ($this->getCity() != null) {
140
            $address->appendChild($dom->createElement('City', $this->getCity()));
141
        }
142
143
        if ($this->getStateProvinceCode() != null) {
144
            $address->appendChild($dom->createElement('StateProvinceCode', $this->getStateProvinceCode()));
145
        }
146
147
        if ($this->getPostalCode() != null) {
148
            $address->appendChild($dom->createElement('PostalCode', $this->getPostalCode()));
149
        }
150
151
        if ($this->getCountryCode() != null) {
152
            $address->appendChild($dom->createElement('CountryCode', $this->getCountryCode()));
153
        }
154
155
        if ($this->isResidential()) {
156
            $address->appendChild($dom->createElement('ResidentialAddressIndicator'));
157
        }
158
159
        return $address;
160
    }
161
162
    /**
163
     * Create an address from XML.  SimpleXMLElement passed must have immediate children like AddressLine1, City, etc.
164
     * @internal
165
     *
166
     * @param \SimpleXMLElement $xml
167
     *
168
     * @return \SimpleUPS\InstructionalAddress
169
     */
170
    public static function fromXml(\SimpleXMLElement $xml)
171
    {
172
        $address = new InstructionalAddress();
173
        $address->setIsResponse();
174
175
        //@todo Consider alternatives for what to do with Consignee
176
177
        if (isset($xml->AddressLine1)) {
178
            $address->setStreet(trim($xml->AddressLine1));
179
        }
180
181
        if (isset($xml->AddressLine2)) {
182
            $address->setAddressLine2(trim($xml->AddressLine2));
183
        }
184
185
        if (isset($xml->AddressLine3)) {
186
            $address->setAddressLine3(trim($xml->AddressLine3));
187
        }
188
189
        if (isset($xml->City)) {
190
            $address->setCity($xml->City);
191
        }
192
193
        if (isset($xml->StateProvinceCode)) {
194
            $address->setStateProvinceCode((string)$xml->StateProvinceCode);
195
        }
196
197
        if (isset($xml->PostalCode)) {
198
            $address->setPostalCode((string)$xml->PostalCode);
199
        }
200
201
        if (isset($xml->CountryCode)) {
202
            $address->setCountryCode((string)$xml->CountryCode);
203
        }
204
205
        return $address;
206
    }
207
}