Complex classes like Cart 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 Cart, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 68 | class Cart extends ExtendCart implements |
||
| 69 | ChannelAwareInterface, |
||
| 70 | FirstNameInterface, |
||
| 71 | LastNameInterface, |
||
| 72 | OriginAwareInterface, |
||
| 73 | IntegrationAwareInterface |
||
| 74 | { |
||
| 75 | use IntegrationEntityTrait, OriginTrait, NamesAwareTrait, ChannelEntityTrait; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var CartItem[]|Collection |
||
| 79 | * |
||
| 80 | * @ORM\OneToMany(targetEntity="OroCRM\Bundle\MagentoBundle\Entity\CartItem", |
||
| 81 | * mappedBy="cart", cascade={"all"}, orphanRemoval=true |
||
| 82 | * ) |
||
| 83 | * @ORM\OrderBy({"originId" = "DESC"}) |
||
| 84 | * @ConfigField( |
||
| 85 | * defaultValues={ |
||
| 86 | * "importexport"={ |
||
| 87 | * "full"=true |
||
| 88 | * } |
||
| 89 | * } |
||
| 90 | * ) |
||
| 91 | */ |
||
| 92 | protected $cartItems; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @ORM\ManyToOne(targetEntity="Customer", inversedBy="carts") |
||
| 96 | * @ORM\JoinColumn(name="customer_id", referencedColumnName="id", onDelete="CASCADE") |
||
| 97 | */ |
||
| 98 | protected $customer; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @var Store |
||
| 102 | * |
||
| 103 | * @ORM\ManyToOne(targetEntity="OroCRM\Bundle\MagentoBundle\Entity\Store") |
||
| 104 | * @ORM\JoinColumn(name="store_id", referencedColumnName="id", onDelete="SET NULL") |
||
| 105 | * @ConfigField( |
||
| 106 | * defaultValues={ |
||
| 107 | * "importexport"={ |
||
| 108 | * "full"=false |
||
| 109 | * } |
||
| 110 | * } |
||
| 111 | * ) |
||
| 112 | */ |
||
| 113 | protected $store; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Total items qty |
||
| 117 | * |
||
| 118 | * @var float |
||
| 119 | * |
||
| 120 | * @ORM\Column(name="items_qty", type="float") |
||
| 121 | */ |
||
| 122 | protected $itemsQty; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Items count |
||
| 126 | * |
||
| 127 | * @var integer |
||
| 128 | * |
||
| 129 | * @ORM\Column(name="items_count", type="integer", options={"unsigned"=true}) |
||
| 130 | */ |
||
| 131 | protected $itemsCount; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @var string |
||
| 135 | * |
||
| 136 | * @ORM\Column(name="base_currency_code", type="string", length=32, nullable=false) |
||
| 137 | */ |
||
| 138 | protected $baseCurrencyCode; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @var string |
||
| 142 | * |
||
| 143 | * @ORM\Column(name="store_currency_code", type="string", length=32, nullable=false) |
||
| 144 | */ |
||
| 145 | protected $storeCurrencyCode; |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @var string |
||
| 149 | * |
||
| 150 | * @ORM\Column(name="quote_currency_code", type="string", length=32, nullable=false) |
||
| 151 | */ |
||
| 152 | protected $quoteCurrencyCode; |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @var float |
||
| 156 | * |
||
| 157 | * @ORM\Column(name="store_to_base_rate", type="float", nullable=false) |
||
| 158 | */ |
||
| 159 | protected $storeToBaseRate; |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @var float |
||
| 163 | * |
||
| 164 | * @ORM\Column(name="store_to_quote_rate", type="float", nullable=true) |
||
| 165 | */ |
||
| 166 | protected $storeToQuoteRate; |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @var string |
||
| 170 | * |
||
| 171 | * @ORM\Column(name="email", type="string", length=255, nullable=true) |
||
| 172 | * @ConfigField( |
||
| 173 | * defaultValues={ |
||
| 174 | * "entity"={ |
||
| 175 | * "contact_information"="email" |
||
| 176 | * } |
||
| 177 | * } |
||
| 178 | * ) |
||
| 179 | */ |
||
| 180 | protected $email; |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @var string |
||
| 184 | * |
||
| 185 | * @ORM\Column(name="gift_message", type="string", length=255, nullable=true) |
||
| 186 | */ |
||
| 187 | protected $giftMessage; |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @var float |
||
| 191 | * |
||
| 192 | * @ORM\Column(name="is_guest", type="boolean") |
||
| 193 | */ |
||
| 194 | protected $isGuest; |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @var CartAddress $shippingAddress |
||
| 198 | * |
||
| 199 | * @ORM\ManyToOne(targetEntity="OroCRM\Bundle\MagentoBundle\Entity\CartAddress", cascade={"persist", "remove"}) |
||
| 200 | * @ORM\JoinColumn(name="shipping_address_id", referencedColumnName="id", onDelete="SET NULL") |
||
| 201 | * @ConfigField( |
||
| 202 | * defaultValues={ |
||
| 203 | * "importexport"={ |
||
| 204 | * "full"=true |
||
| 205 | * } |
||
| 206 | * } |
||
| 207 | * ) |
||
| 208 | */ |
||
| 209 | protected $shippingAddress; |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @var CartAddress $billingAddress |
||
| 213 | * |
||
| 214 | * @ORM\ManyToOne(targetEntity="OroCRM\Bundle\MagentoBundle\Entity\CartAddress", cascade={"persist", "remove"}) |
||
| 215 | * @ORM\JoinColumn(name="billing_address_id", referencedColumnName="id", onDelete="SET NULL") |
||
| 216 | * @ConfigField( |
||
| 217 | * defaultValues={ |
||
| 218 | * "importexport"={ |
||
| 219 | * "full"=true |
||
| 220 | * } |
||
| 221 | * } |
||
| 222 | * ) |
||
| 223 | */ |
||
| 224 | protected $billingAddress; |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @var string |
||
| 228 | * |
||
| 229 | * @ORM\Column(name="payment_details", type="string", length=255, nullable=true) |
||
| 230 | */ |
||
| 231 | protected $paymentDetails; |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @var CartStatus |
||
| 235 | * |
||
| 236 | * @ORM\ManyToOne(targetEntity="OroCRM\Bundle\MagentoBundle\Entity\CartStatus") |
||
| 237 | * @ORM\JoinColumn(name="status_name", referencedColumnName="name", onDelete="SET NULL") |
||
| 238 | * @ConfigField( |
||
| 239 | * defaultValues={ |
||
| 240 | * "importexport"={ |
||
| 241 | * "full"=false |
||
| 242 | * } |
||
| 243 | * } |
||
| 244 | * ) |
||
| 245 | */ |
||
| 246 | protected $status; |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @var Opportunity |
||
| 250 | * |
||
| 251 | * @ORM\ManyToOne(targetEntity="OroCRM\Bundle\SalesBundle\Entity\Opportunity") |
||
| 252 | * @ORM\JoinColumn(name="opportunity_id", referencedColumnName="id", onDelete="SET NULL") |
||
| 253 | */ |
||
| 254 | protected $opportunity; |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @var string |
||
| 258 | * |
||
| 259 | * @ORM\Column(name="notes", type="text", nullable=true) |
||
| 260 | */ |
||
| 261 | protected $notes; |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @var string |
||
| 265 | * |
||
| 266 | * @ORM\Column(name="status_message", type="string", length=255, nullable=true) |
||
| 267 | */ |
||
| 268 | protected $statusMessage; |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @var User |
||
| 272 | * @ORM\ManyToOne(targetEntity="Oro\Bundle\UserBundle\Entity\User") |
||
| 273 | * @ORM\JoinColumn(name="user_owner_id", referencedColumnName="id", onDelete="SET NULL") |
||
| 274 | */ |
||
| 275 | protected $owner; |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @var Organization |
||
| 279 | * |
||
| 280 | * @ORM\ManyToOne(targetEntity="Oro\Bundle\OrganizationBundle\Entity\Organization") |
||
| 281 | * @ORM\JoinColumn(name="organization_id", referencedColumnName="id", onDelete="SET NULL") |
||
| 282 | */ |
||
| 283 | protected $organization; |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @var \DateTime |
||
| 287 | * |
||
| 288 | * @ORM\Column(type="datetime", name="imported_at", nullable=true) |
||
| 289 | */ |
||
| 290 | protected $importedAt; |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @var \DateTime |
||
| 294 | * |
||
| 295 | * @ORM\Column(type="datetime", name="synced_at", nullable=true) |
||
| 296 | */ |
||
| 297 | protected $syncedAt; |
||
| 298 | |||
| 299 | public function __construct() |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @return CartItem[]|Collection |
||
| 309 | */ |
||
| 310 | public function getCartItems() |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @param CartItem[]|Collection $cartItems |
||
| 317 | */ |
||
| 318 | public function setCartItems(Collection $cartItems) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @param CartItem $cartItem |
||
| 325 | * |
||
| 326 | * @return $this |
||
| 327 | */ |
||
| 328 | public function addCartItem(CartItem $cartItem) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * @param CartItem $cartItem |
||
| 340 | * |
||
| 341 | * @return $this |
||
| 342 | */ |
||
| 343 | public function removeCartItem(CartItem $cartItem) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * @param Store $store |
||
| 354 | */ |
||
| 355 | public function setStore($store) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * @return Store |
||
| 362 | */ |
||
| 363 | public function getStore() |
||
| 367 | |||
| 368 | /** |
||
| 369 | * @return Customer |
||
| 370 | */ |
||
| 371 | public function getCustomer() |
||
| 375 | |||
| 376 | /** |
||
| 377 | * @param Customer|null $customer |
||
| 378 | * |
||
| 379 | * @return Cart |
||
| 380 | */ |
||
| 381 | public function setCustomer(Customer $customer = null) |
||
| 387 | |||
| 388 | /** |
||
| 389 | * @param CartAddress $shippingAddress |
||
| 390 | * |
||
| 391 | * @return Cart |
||
| 392 | */ |
||
| 393 | public function setShippingAddress(CartAddress $shippingAddress = null) |
||
| 399 | |||
| 400 | /** |
||
| 401 | * @param CartAddress $billingAddress |
||
| 402 | * |
||
| 403 | * @return Cart |
||
| 404 | */ |
||
| 405 | public function setBillingAddress(CartAddress $billingAddress = null) |
||
| 411 | |||
| 412 | /** |
||
| 413 | * @return CartAddress |
||
| 414 | */ |
||
| 415 | public function getBillingAddress() |
||
| 419 | |||
| 420 | /** |
||
| 421 | * @return CartAddress |
||
| 422 | */ |
||
| 423 | public function getShippingAddress() |
||
| 427 | |||
| 428 | /** |
||
| 429 | * @return string |
||
| 430 | */ |
||
| 431 | public function getEmail() |
||
| 435 | |||
| 436 | /** |
||
| 437 | * @param string $email |
||
| 438 | * |
||
| 439 | * @return Cart |
||
| 440 | */ |
||
| 441 | public function setEmail($email) |
||
| 447 | |||
| 448 | /** |
||
| 449 | * @return float |
||
| 450 | */ |
||
| 451 | public function getItemsQty() |
||
| 455 | |||
| 456 | /** |
||
| 457 | * @param float $itemsQty |
||
| 458 | * |
||
| 459 | * @return Cart |
||
| 460 | */ |
||
| 461 | public function setItemsQty($itemsQty) |
||
| 467 | |||
| 468 | /** |
||
| 469 | * @return float |
||
| 470 | */ |
||
| 471 | public function getSubTotal() |
||
| 475 | |||
| 476 | /** |
||
| 477 | * @return string |
||
| 478 | */ |
||
| 479 | public function getQuoteCurrencyCode() |
||
| 483 | |||
| 484 | /** |
||
| 485 | * @param string $quoteCurrencyCode |
||
| 486 | * |
||
| 487 | * @return Cart |
||
| 488 | */ |
||
| 489 | public function setQuoteCurrencyCode($quoteCurrencyCode) |
||
| 495 | |||
| 496 | /** |
||
| 497 | * @param string $paymentDetails |
||
| 498 | */ |
||
| 499 | public function setPaymentDetails($paymentDetails) |
||
| 503 | |||
| 504 | /** |
||
| 505 | * @return string |
||
| 506 | */ |
||
| 507 | public function getPaymentDetails() |
||
| 511 | |||
| 512 | /** |
||
| 513 | * @param CartStatus $status |
||
| 514 | * |
||
| 515 | * @return Cart |
||
| 516 | */ |
||
| 517 | public function setStatus($status) |
||
| 523 | |||
| 524 | /** |
||
| 525 | * @return CartStatus |
||
| 526 | */ |
||
| 527 | public function getStatus() |
||
| 531 | |||
| 532 | /** |
||
| 533 | * @param string $baseCurrencyCode |
||
| 534 | * |
||
| 535 | * @return Cart |
||
| 536 | */ |
||
| 537 | public function setBaseCurrencyCode($baseCurrencyCode) |
||
| 543 | |||
| 544 | /** |
||
| 545 | * @return string |
||
| 546 | */ |
||
| 547 | public function getBaseCurrencyCode() |
||
| 551 | |||
| 552 | /** |
||
| 553 | * @param string $giftMessage |
||
| 554 | * |
||
| 555 | * @return Cart |
||
| 556 | */ |
||
| 557 | public function setGiftMessage($giftMessage) |
||
| 563 | |||
| 564 | /** |
||
| 565 | * @return string |
||
| 566 | */ |
||
| 567 | public function getGiftMessage() |
||
| 571 | |||
| 572 | /** |
||
| 573 | * @param float $isGuest |
||
| 574 | * |
||
| 575 | * @return Cart |
||
| 576 | */ |
||
| 577 | public function setIsGuest($isGuest) |
||
| 583 | |||
| 584 | /** |
||
| 585 | * @return float |
||
| 586 | */ |
||
| 587 | public function getIsGuest() |
||
| 591 | |||
| 592 | /** |
||
| 593 | * @param int $itemsCount |
||
| 594 | * |
||
| 595 | * @return Cart |
||
| 596 | */ |
||
| 597 | public function setItemsCount($itemsCount) |
||
| 603 | |||
| 604 | /** |
||
| 605 | * @return int |
||
| 606 | */ |
||
| 607 | public function getItemsCount() |
||
| 611 | |||
| 612 | /** |
||
| 613 | * @param string $storeCurrencyCode |
||
| 614 | * |
||
| 615 | * @return Cart |
||
| 616 | */ |
||
| 617 | public function setStoreCurrencyCode($storeCurrencyCode) |
||
| 623 | |||
| 624 | /** |
||
| 625 | * @return string |
||
| 626 | */ |
||
| 627 | public function getStoreCurrencyCode() |
||
| 631 | |||
| 632 | /** |
||
| 633 | * @param float $storeToBaseRate |
||
| 634 | * |
||
| 635 | * @return Cart |
||
| 636 | */ |
||
| 637 | public function setStoreToBaseRate($storeToBaseRate) |
||
| 643 | |||
| 644 | /** |
||
| 645 | * @return float |
||
| 646 | */ |
||
| 647 | public function getStoreToBaseRate() |
||
| 651 | |||
| 652 | /** |
||
| 653 | * @param float $storeToQuoteRate |
||
| 654 | * |
||
| 655 | * @return Cart |
||
| 656 | */ |
||
| 657 | public function setStoreToQuoteRate($storeToQuoteRate) |
||
| 663 | |||
| 664 | /** |
||
| 665 | * @return float |
||
| 666 | */ |
||
| 667 | public function getStoreToQuoteRate() |
||
| 671 | |||
| 672 | /** |
||
| 673 | * @param Opportunity $opportunity |
||
| 674 | * |
||
| 675 | * @return Cart |
||
| 676 | */ |
||
| 677 | public function setOpportunity($opportunity) |
||
| 683 | |||
| 684 | /** |
||
| 685 | * @return Opportunity |
||
| 686 | */ |
||
| 687 | public function getOpportunity() |
||
| 691 | |||
| 692 | /** |
||
| 693 | * @param string $notes |
||
| 694 | * |
||
| 695 | * @return Cart |
||
| 696 | */ |
||
| 697 | public function setNotes($notes) |
||
| 702 | |||
| 703 | /** |
||
| 704 | * @return string |
||
| 705 | */ |
||
| 706 | public function getNotes() |
||
| 710 | |||
| 711 | /** |
||
| 712 | * Pre persist event listener |
||
| 713 | * |
||
| 714 | * @ORM\PrePersist |
||
| 715 | */ |
||
| 716 | public function beforeSave() |
||
| 720 | |||
| 721 | /** |
||
| 722 | * Pre update event handler |
||
| 723 | * |
||
| 724 | * @ORM\PreUpdate |
||
| 725 | */ |
||
| 726 | public function doPreUpdate() |
||
| 730 | |||
| 731 | /** |
||
| 732 | * @param string $statusMessage |
||
| 733 | */ |
||
| 734 | public function setStatusMessage($statusMessage) |
||
| 738 | |||
| 739 | /** |
||
| 740 | * @return string |
||
| 741 | */ |
||
| 742 | public function getStatusMessage() |
||
| 746 | |||
| 747 | /** |
||
| 748 | * @return User |
||
| 749 | */ |
||
| 750 | public function getOwner() |
||
| 754 | |||
| 755 | /** |
||
| 756 | * @param User $user |
||
| 757 | */ |
||
| 758 | public function setOwner(User $user) |
||
| 762 | |||
| 763 | /** |
||
| 764 | * Set organization |
||
| 765 | * |
||
| 766 | * @param Organization $organization |
||
| 767 | * @return Cart |
||
| 768 | */ |
||
| 769 | public function setOrganization(Organization $organization = null) |
||
| 775 | |||
| 776 | /** |
||
| 777 | * Get organization |
||
| 778 | * |
||
| 779 | * @return Organization |
||
| 780 | */ |
||
| 781 | public function getOrganization() |
||
| 785 | |||
| 786 | /** |
||
| 787 | * @return \DateTime |
||
| 788 | */ |
||
| 789 | public function getSyncedAt() |
||
| 793 | |||
| 794 | /** |
||
| 795 | * @param \DateTime|null $syncedAt |
||
| 796 | * @return Cart |
||
| 797 | */ |
||
| 798 | public function setSyncedAt(\DateTime $syncedAt = null) |
||
| 804 | |||
| 805 | /** |
||
| 806 | * @return \DateTime |
||
| 807 | */ |
||
| 808 | public function getImportedAt() |
||
| 812 | |||
| 813 | /** |
||
| 814 | * @param \DateTime|null $importedAt |
||
| 815 | * @return Cart |
||
| 816 | */ |
||
| 817 | public function setImportedAt(\DateTime $importedAt = null) |
||
| 823 | |||
| 824 | /** |
||
| 825 | * @return string |
||
| 826 | */ |
||
| 827 | public function __toString() |
||
| 831 | } |
||
| 832 |