Total Complexity | 49 |
Total Lines | 324 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like AtBpost often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AtBpost, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class AtBpost extends National |
||
27 | { |
||
28 | /** @var string */ |
||
29 | protected $product = Product::PRODUCT_NAME_BPACK_AT_BPOST; |
||
30 | |||
31 | /** @var string */ |
||
32 | private $pugoId; |
||
33 | |||
34 | /** @var string */ |
||
35 | private $pugoName; |
||
36 | |||
37 | /** @var \Bpost\BpostApiClient\Bpost\Order\PugoAddress */ |
||
38 | private $pugoAddress; |
||
39 | |||
40 | /** @var string */ |
||
41 | private $receiverName; |
||
42 | |||
43 | /** @var string */ |
||
44 | private $receiverCompany; |
||
45 | |||
46 | /** @var string */ |
||
47 | protected $requestedDeliveryDate; |
||
48 | |||
49 | /** @var ShopHandlingInstruction */ |
||
50 | private $shopHandlingInstruction; |
||
51 | |||
52 | /** |
||
53 | * @param string $product Possible values are: bpack@bpost |
||
54 | * |
||
55 | * @throws BpostInvalidValueException |
||
56 | */ |
||
57 | public function setProduct($product) |
||
58 | { |
||
59 | if (!in_array($product, self::getPossibleProductValues())) { |
||
60 | throw new BpostInvalidValueException('product', $product, self::getPossibleProductValues()); |
||
61 | } |
||
62 | |||
63 | parent::setProduct($product); |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * @return array |
||
68 | */ |
||
69 | public static function getPossibleProductValues() |
||
70 | { |
||
71 | return array( |
||
72 | Product::PRODUCT_NAME_BPACK_AT_BPOST, |
||
73 | ); |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * @param \Bpost\BpostApiClient\Bpost\Order\PugoAddress $pugoAddress |
||
78 | */ |
||
79 | public function setPugoAddress($pugoAddress) |
||
80 | { |
||
81 | $this->pugoAddress = $pugoAddress; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * @return \Bpost\BpostApiClient\Bpost\Order\PugoAddress |
||
86 | */ |
||
87 | public function getPugoAddress() |
||
88 | { |
||
89 | return $this->pugoAddress; |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * @param string $pugoId |
||
94 | */ |
||
95 | public function setPugoId($pugoId) |
||
96 | { |
||
97 | $this->pugoId = $pugoId; |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * @return string |
||
102 | */ |
||
103 | public function getPugoId() |
||
104 | { |
||
105 | return $this->pugoId; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * @param string $pugoName |
||
110 | */ |
||
111 | public function setPugoName($pugoName) |
||
112 | { |
||
113 | $this->pugoName = $pugoName; |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * @return string |
||
118 | */ |
||
119 | public function getPugoName() |
||
120 | { |
||
121 | return $this->pugoName; |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * @param string $receiverCompany |
||
126 | */ |
||
127 | public function setReceiverCompany($receiverCompany) |
||
128 | { |
||
129 | $this->receiverCompany = $receiverCompany; |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * @return string |
||
134 | */ |
||
135 | public function getReceiverCompany() |
||
136 | { |
||
137 | return $this->receiverCompany; |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * @param string $receiverName |
||
142 | */ |
||
143 | public function setReceiverName($receiverName) |
||
144 | { |
||
145 | $this->receiverName = $receiverName; |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * @return string |
||
150 | */ |
||
151 | public function getReceiverName() |
||
152 | { |
||
153 | return $this->receiverName; |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * @return string |
||
158 | */ |
||
159 | public function getRequestedDeliveryDate() |
||
160 | { |
||
161 | return $this->requestedDeliveryDate; |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * @param string $requestedDeliveryDate |
||
166 | */ |
||
167 | public function setRequestedDeliveryDate($requestedDeliveryDate) |
||
168 | { |
||
169 | $this->requestedDeliveryDate = $requestedDeliveryDate; |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * @return string |
||
174 | */ |
||
175 | public function getShopHandlingInstruction() |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * @param string $shopHandlingInstruction |
||
186 | */ |
||
187 | public function setShopHandlingInstruction($shopHandlingInstruction) |
||
188 | { |
||
189 | $this->shopHandlingInstruction = new ShopHandlingInstruction($shopHandlingInstruction); |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * Return the object as an array for usage in the XML |
||
194 | * |
||
195 | * @param DomDocument $document |
||
196 | * @param string $prefix |
||
197 | * @param string $type |
||
198 | * |
||
199 | * @return DomElement |
||
200 | */ |
||
201 | public function toXML(DOMDocument $document, $prefix = null, $type = null) |
||
202 | { |
||
203 | $nationalElement = $document->createElement(XmlHelper::getPrefixedTagName('nationalBox', $prefix)); |
||
204 | $boxElement = parent::toXML($document, null, 'atBpost'); |
||
205 | $nationalElement->appendChild($boxElement); |
||
206 | |||
207 | if ($this->getPugoId() !== null) { |
||
|
|||
208 | $boxElement->appendChild( |
||
209 | $document->createElement('pugoId', $this->getPugoId()) |
||
210 | ); |
||
211 | } |
||
212 | if ($this->getPugoName() !== null) { |
||
213 | $boxElement->appendChild( |
||
214 | $document->createElement('pugoName', $this->getPugoName()) |
||
215 | ); |
||
216 | } |
||
217 | if ($this->getPugoAddress() !== null) { |
||
218 | $boxElement->appendChild( |
||
219 | $this->getPugoAddress()->toXML($document, 'common') |
||
220 | ); |
||
221 | } |
||
222 | if ($this->getReceiverName() !== null) { |
||
223 | $boxElement->appendChild( |
||
224 | $document->createElement('receiverName', $this->getReceiverName()) |
||
225 | ); |
||
226 | } |
||
227 | if ($this->getReceiverCompany() !== null) { |
||
228 | $boxElement->appendChild( |
||
229 | $document->createElement('receiverCompany', $this->getReceiverCompany()) |
||
230 | ); |
||
231 | } |
||
232 | $this->addToXmlRequestedDeliveryDate($document, $boxElement, $prefix); |
||
233 | $this->addToXmlShopHandlingInstruction($document, $boxElement, $prefix); |
||
234 | |||
235 | return $nationalElement; |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * @param DOMDocument $document |
||
240 | * @param DOMElement $typeElement |
||
241 | * @param string $prefix |
||
242 | */ |
||
243 | protected function addToXmlRequestedDeliveryDate(DOMDocument $document, DOMElement $typeElement, $prefix) |
||
248 | ); |
||
249 | } |
||
250 | } |
||
251 | |||
252 | private function addToXmlShopHandlingInstruction(DOMDocument $document, DOMElement $typeElement, $prefix) |
||
257 | ); |
||
258 | } |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * @param SimpleXMLElement $xml |
||
263 | * @param National|null $self |
||
264 | * |
||
265 | * @return AtBpost |
||
266 | * |
||
267 | * @throws BpostInvalidValueException |
||
268 | * @throws BpostNotImplementedException |
||
269 | * @throws \Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidLengthException |
||
270 | * @throws \Bpost\BpostApiClient\Exception\XmlException\BpostXmlInvalidItemException |
||
271 | */ |
||
272 | public static function createFromXML(SimpleXMLElement $xml, National $self = null) |
||
350 | } |
||
351 | } |
||
352 |