Complex classes like Orders 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Orders, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class Orders extends MoipResource |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * @const string |
||
| 12 | */ |
||
| 13 | const PATH = 'orders'; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Defines what kind of payee as pripmary. |
||
| 17 | * |
||
| 18 | * @const string |
||
| 19 | */ |
||
| 20 | const RECEIVER_TYPE_PRIMARY = 'PRIMARY'; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Defines what kind of payee as secundary. |
||
| 24 | * |
||
| 25 | * @const string |
||
| 26 | */ |
||
| 27 | const RECEIVER_TYPE_SECONDARY = 'SECONDARY'; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Currency used in the application. |
||
| 31 | * |
||
| 32 | * @const string |
||
| 33 | */ |
||
| 34 | const AMOUNT_CURRENCY = 'BRL'; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var \Moip\Resource\Orders |
||
| 38 | **/ |
||
| 39 | private $orders; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Adds a new item to order. |
||
| 43 | * |
||
| 44 | * @param string $product Name of the product. |
||
| 45 | * @param int $quantity Product Quantity. |
||
| 46 | * @param string $detail Additional product description. |
||
| 47 | * @param int $price Initial value of the item. |
||
| 48 | * |
||
| 49 | * @return $this |
||
| 50 | */ |
||
| 51 | public function addItem($product, $quantity, $detail, $price) |
||
| 52 | { |
||
| 53 | $item = new stdClass(); |
||
| 54 | $item->product = $product; |
||
| 55 | $item->quantity = $quantity; |
||
| 56 | $item->detail = $detail; |
||
| 57 | $item->price = $price; |
||
| 58 | |||
| 59 | $this->data->items[] = $item; |
||
| 60 | |||
| 61 | return $this; |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Adds a new receiver to order. |
||
| 66 | * |
||
| 67 | * @param string $moipAccount Id MoIP MoIP account that will receive payment values. |
||
| 68 | * @param string $type Define qual o tipo de recebedor do pagamento, valores possíveis: PRIMARY, SECONDARY. |
||
| 69 | * |
||
| 70 | * @return $this |
||
| 71 | */ |
||
| 72 | public function addReceiver($moipAccount, $type = self::RECEIVER_TYPE_PRIMARY) |
||
| 73 | { |
||
| 74 | $receiver = new stdClass(); |
||
| 75 | $receiver->moipAccount = new stdClass(); |
||
| 76 | $receiver->moipAccount->id = $moipAccount; |
||
| 77 | $receiver->type = $type; |
||
| 78 | |||
| 79 | $this->data->receivers[] = $receiver; |
||
| 80 | |||
| 81 | return $this; |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Initialize necessary used in some functions. |
||
| 86 | */ |
||
| 87 | protected function initialize() |
||
| 88 | { |
||
| 89 | $this->data = new stdClass(); |
||
| 90 | $this->data->ownId = null; |
||
| 91 | $this->data->amount = new stdClass(); |
||
| 92 | $this->data->amount->currency = self::AMOUNT_CURRENCY; |
||
| 93 | $this->data->amount->subtotals = new stdClass(); |
||
| 94 | $this->data->items = []; |
||
| 95 | $this->data->receivers = []; |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Initialize necessary used in some functions. |
||
| 100 | */ |
||
| 101 | private function initializeSubtotals() |
||
| 102 | { |
||
| 103 | if (!isset($this->data->subtotals)) { |
||
| 104 | $this->data->subtotals = new stdClass(); |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Mount the structure of order. |
||
| 110 | * |
||
| 111 | * @param \stdClass $response |
||
| 112 | * |
||
| 113 | * @return Orders Response order. |
||
| 114 | */ |
||
| 115 | protected function populate(stdClass $response) |
||
| 116 | { |
||
| 117 | $this->orders = clone $this; |
||
| 118 | $this->orders->data->id = $response->id; |
||
| 119 | $this->orders->data->amount->total = $response->amount->total; |
||
| 120 | $this->orders->data->amount->fees = $response->amount->fees; |
||
| 121 | $this->orders->data->amount->refunds = $response->amount->refunds; |
||
| 122 | $this->orders->data->amount->liquid = $response->amount->liquid; |
||
| 123 | $this->orders->data->amount->otherReceivers = $response->amount->otherReceivers; |
||
| 124 | $this->orders->data->amount->subtotals = $response->amount->subtotals; |
||
| 125 | |||
| 126 | $customer = new Customer($this->moip); |
||
| 127 | $customer->populate($response->customer); |
||
| 128 | |||
| 129 | $this->orders->data->payments = $this->structure($response, Payment::PATH, Payment::class); |
||
| 130 | $this->orders->data->refunds = $this->structure($response, Refund::PATH, Refund::class); |
||
| 131 | $this->orders->data->entries = $this->structure($response, Entry::PATH, Entry::class); |
||
| 132 | $this->orders->data->events = $this->structure($response, Event::PATH, Event::class); |
||
| 133 | |||
| 134 | $this->orders->data->items = $response->items; |
||
| 135 | $this->orders->data->receivers = $response->receivers; |
||
| 136 | $this->orders->data->createdAt = $response->createdAt; |
||
| 137 | $this->orders->data->status = $response->status; |
||
| 138 | $this->orders->data->_links = $response->_links; |
||
| 139 | |||
| 140 | return $this->orders; |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Structure resource. |
||
| 145 | * |
||
| 146 | * @param stdClass $response |
||
| 147 | * @param string $resource |
||
| 148 | * @param \Moip\Resource\Payment|\Moip\Resource\Refund|\Moip\Resource\Entry|\Moip\Resource\Event $class |
||
| 149 | * |
||
| 150 | * @return array |
||
| 151 | */ |
||
| 152 | private function structure(stdClass $response, $resource, $class) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Create a new order in MoIP. |
||
| 168 | * |
||
| 169 | * @return Orders |
||
| 170 | */ |
||
| 171 | public function create() |
||
| 172 | { |
||
| 173 | return $this->createResource(sprintf('/%s/%s', MoipResource::VERSION, self::PATH)); |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Get an order in MoIP. |
||
| 178 | * |
||
| 179 | * @param string $id_moip Id MoIP order id |
||
| 180 | * |
||
| 181 | * @return stdClass |
||
| 182 | */ |
||
| 183 | public function get($id_moip) |
||
| 184 | { |
||
| 185 | return $this->getByPath(sprintf('/%s/%s/%s', MoipResource::VERSION, self::PATH, $id_moip)); |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Get MoIP order id. |
||
| 190 | * |
||
| 191 | * @return string |
||
| 192 | */ |
||
| 193 | public function getId() |
||
| 194 | { |
||
| 195 | return $this->getIfSet('id'); |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Get own request id. external reference. |
||
| 200 | * |
||
| 201 | * @return string |
||
| 202 | */ |
||
| 203 | public function getOwnId() |
||
| 204 | { |
||
| 205 | return $this->getIfSet('ownId'); |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Get total value of order. |
||
| 210 | * |
||
| 211 | * @return int|float |
||
| 212 | */ |
||
| 213 | public function getAmountTotal() |
||
| 214 | { |
||
| 215 | return $this->getIfSet('total', $this->data->amount); |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Get total value of MoIP rate. |
||
| 220 | * |
||
| 221 | * @return int|float |
||
| 222 | */ |
||
| 223 | public function getAmountFees() |
||
| 224 | { |
||
| 225 | return $this->getIfSet('feed', $this->data->amount); |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Get total amount of refunds. |
||
| 230 | * |
||
| 231 | * @return int|float |
||
| 232 | */ |
||
| 233 | public function getAmountRefunds() |
||
| 234 | { |
||
| 235 | return $this->getIfSet('refunds', $this->data->amount); |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Get net total value. |
||
| 240 | * |
||
| 241 | * @return int|float |
||
| 242 | */ |
||
| 243 | public function getAmountLiquid() |
||
| 244 | { |
||
| 245 | return $this->getIfSet('liquid', $this->data->amount); |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Get sum of amounts received by other recipients. Used in Marketplaces. |
||
| 250 | * |
||
| 251 | * @return int|float |
||
| 252 | */ |
||
| 253 | public function getAmountOtherReceivers() |
||
| 254 | { |
||
| 255 | return $this->getIfSet('otherReceivers', $this->data->amount); |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Get currency used in the application. Possible values: BRL. |
||
| 260 | * |
||
| 261 | * @return string |
||
| 262 | */ |
||
| 263 | public function getCurrenty() |
||
| 264 | { |
||
| 265 | return $this->getIfSet('currency', $this->data->amount); |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Get greight value of the item will be added to the value of the items. |
||
| 270 | * |
||
| 271 | * @return int|float |
||
| 272 | */ |
||
| 273 | public function getSubtotalShipping() |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Get Additional value to the item will be added to the value of the items. |
||
| 282 | * |
||
| 283 | * @return int|float |
||
| 284 | */ |
||
| 285 | public function getSubtotalAddition() |
||
| 286 | { |
||
| 287 | $this->initializeSubtotals(); |
||
| 288 | |||
| 291 | |||
| 292 | /** |
||
| 293 | * Get discounted value of the item will be subtracted from the total value of the items. |
||
| 294 | * |
||
| 295 | * @return int|float |
||
| 296 | */ |
||
| 297 | public function getSubtotalDiscount() |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Get summing the values of all items. |
||
| 306 | * |
||
| 307 | * @return int|float |
||
| 308 | */ |
||
| 309 | public function getSubtotalItems() |
||
| 310 | { |
||
| 311 | $this->initializeSubtotals(); |
||
| 312 | |||
| 313 | return $this->getIfSet('items', $this->data->amount->subtotals); |
||
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Ger structure item information request. |
||
| 318 | * |
||
| 319 | * @return \ArrayIterator |
||
| 320 | */ |
||
| 321 | public function getItemIterator() |
||
| 322 | { |
||
| 323 | return new ArrayIterator($this->data->items); |
||
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Get Customer associated with the request. |
||
| 328 | * |
||
| 329 | * @return \Moip\Resource\Customer |
||
| 330 | */ |
||
| 331 | public function getCustomer() |
||
| 332 | { |
||
| 333 | return $this->data->customer; |
||
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Get payments associated with the request. |
||
| 338 | * |
||
| 339 | * @return ArrayIterator |
||
| 340 | */ |
||
| 341 | public function getPaymentIterator() |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Get recipient structure of payments. |
||
| 348 | * |
||
| 349 | * @return ArrayIterator |
||
| 350 | */ |
||
| 351 | public function getReceiverIterator() |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Get releases associated with the request. |
||
| 358 | * |
||
| 359 | * @return ArrayIterator |
||
| 360 | */ |
||
| 361 | public function getEventIterator() |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Get repayments associated with the request. |
||
| 368 | * |
||
| 369 | * @return ArrayIterator |
||
| 370 | */ |
||
| 371 | public function getRefundIterator() |
||
| 372 | { |
||
| 373 | return new ArrayIterator($this->data->refunds); |
||
| 374 | } |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Get order status. |
||
| 378 | * Possible values: CREATED, WAITING, PAID, NOT_PAID, REVERTED. |
||
| 379 | * |
||
| 380 | * @return string |
||
| 381 | */ |
||
| 382 | public function getStatus() |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Get date of resource creation. |
||
| 389 | * |
||
| 390 | * @return \DateTime |
||
| 391 | */ |
||
| 392 | public function getCreatedAt() |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Get updated resource. |
||
| 399 | * |
||
| 400 | * @return \DateTime |
||
| 401 | */ |
||
| 402 | public function getUpdatedAt() |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Structure of payment. |
||
| 409 | * |
||
| 410 | * @return \Moip\Resource\Payment |
||
| 411 | */ |
||
| 412 | public function payments() |
||
| 413 | { |
||
| 414 | $payment = new Payment($this->moip); |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Structure of refund. |
||
| 422 | * |
||
| 423 | * @return \Moip\Resource\Refund |
||
| 424 | */ |
||
| 425 | public function refunds() |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Set additional value to the item will be added to the value of the items. |
||
| 435 | * |
||
| 436 | * @param int|float $value additional value to the item. |
||
| 437 | * |
||
| 438 | * @return $this |
||
| 439 | */ |
||
| 440 | public function setAddition($value) |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Set customer associated with the order. |
||
| 452 | * |
||
| 453 | * @param \Moip\Resource\Customer $customer customer associated with the request. |
||
| 454 | * |
||
| 455 | * @return $this |
||
| 456 | */ |
||
| 457 | public function setCustomer(Customer $customer) |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Set discounted value of the item will be subtracted from the total value of the items. |
||
| 466 | * |
||
| 467 | * @param int|float $value discounted value. |
||
| 468 | * |
||
| 469 | * @return $this |
||
| 470 | */ |
||
| 471 | public function setDiscount($value) |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Set discounted value of the item will be subtracted from the total value of the items. |
||
| 480 | * |
||
| 481 | * @deprecated |
||
| 482 | * |
||
| 483 | * @param int|float $value discounted value. |
||
| 484 | * |
||
| 485 | * @return $this |
||
| 486 | */ |
||
| 487 | public function setDiscont($value) |
||
| 493 | |||
| 494 | /** |
||
| 495 | * Set own request id. external reference. |
||
| 496 | * |
||
| 497 | * @param string $ownId external reference. |
||
| 498 | * |
||
| 499 | * @return $this |
||
| 500 | */ |
||
| 501 | public function setOwnId($ownId) |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Set shipping Amount. |
||
| 510 | * |
||
| 511 | * @param float $value shipping Amount. |
||
| 512 | * |
||
| 513 | * @return $this |
||
| 514 | */ |
||
| 515 | public function setShippingAmount($value) |
||
| 521 | } |
||
| 522 |