Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
36 | class LoadMagentoChannel extends AbstractFixture implements ContainerAwareInterface |
||
37 | { |
||
38 | const CHANNEL_NAME = 'Magento channel'; |
||
39 | const CHANNEL_TYPE = 'magento'; |
||
40 | |||
41 | /** @var ObjectManager */ |
||
42 | protected $em; |
||
43 | |||
44 | /** @var integration */ |
||
45 | protected $integration; |
||
46 | |||
47 | /** @var MagentoSoapTransport */ |
||
48 | protected $transport; |
||
49 | |||
50 | /** @var array */ |
||
51 | protected $countries; |
||
52 | |||
53 | /** @var array */ |
||
54 | protected $regions; |
||
55 | |||
56 | /** @var Website */ |
||
57 | protected $website; |
||
58 | |||
59 | /** @var Store */ |
||
60 | protected $store; |
||
61 | |||
62 | /** @var CustomerGroup */ |
||
63 | protected $customerGroup; |
||
64 | |||
65 | /** @var Channel */ |
||
66 | protected $channel; |
||
67 | |||
68 | /** @var BuilderFactory */ |
||
69 | protected $factory; |
||
70 | |||
71 | /** |
||
72 | * @var Organization |
||
73 | */ |
||
74 | protected $organization; |
||
75 | |||
76 | /** |
||
77 | * {@inheritDoc} |
||
78 | */ |
||
79 | public function setContainer(ContainerInterface $container = null) |
||
83 | |||
84 | /** |
||
85 | * {@inheritDoc} |
||
86 | */ |
||
87 | public function load(ObjectManager $manager) |
||
88 | { |
||
89 | $this->em = $manager; |
||
90 | $this->countries = $this->loadStructure('OroAddressBundle:Country', 'getIso2Code'); |
||
91 | $this->regions = $this->loadStructure('OroAddressBundle:Region', 'getCombinedCode'); |
||
92 | $this->organization = $manager->getRepository('OroOrganizationBundle:Organization')->getFirst(); |
||
93 | |||
94 | $this |
||
95 | ->createTransport() |
||
96 | ->createIntegration() |
||
97 | ->createChannel() |
||
98 | ->createWebSite() |
||
99 | ->createCustomerGroup() |
||
100 | ->createGuestCustomerGroup() |
||
101 | ->createStore(); |
||
102 | |||
103 | $magentoAddress = $this->createMagentoAddress($this->regions['US-AZ'], $this->countries['US']); |
||
104 | $account = $this->createAccount(); |
||
105 | $this->setReference('account', $account); |
||
106 | |||
107 | $customer = $this->createCustomer(1, $account, $magentoAddress); |
||
108 | $cartAddress1 = $this->createCartAddress($this->regions['US-AZ'], $this->countries['US'], 1); |
||
109 | $cartAddress2 = $this->createCartAddress($this->regions['US-AZ'], $this->countries['US'], 2); |
||
110 | $cartItem = $this->createCartItem(); |
||
111 | $status = $this->getStatus(); |
||
112 | $items = new ArrayCollection(); |
||
113 | $items->add($cartItem); |
||
114 | |||
115 | $cart = $this->createCart($cartAddress1, $cartAddress2, $customer, $items, $status); |
||
116 | $this->updateCartItem($cartItem, $cart); |
||
117 | |||
118 | $order = $this->createOrder($cart, $customer); |
||
119 | |||
120 | $this->setReference('customer', $customer); |
||
121 | $this->setReference('integration', $this->integration); |
||
122 | $this->setReference('cart', $cart); |
||
123 | $this->setReference('order', $order); |
||
124 | |||
125 | $baseOrderItem = $this->createBaseOrderItem($order); |
||
126 | $order->setItems([$baseOrderItem]); |
||
127 | $this->em->persist($order); |
||
128 | |||
129 | $cartAddress3 = $this->createGuestCartAddress($this->regions['US-AZ'], $this->countries['US'], null); |
||
130 | $cartAddress4 = $this->createGuestCartAddress($this->regions['US-AZ'], $this->countries['US'], null); |
||
131 | |||
132 | $cartItem = $this->createCartItem(); |
||
133 | $status = $this->getStatus(); |
||
134 | $items = new ArrayCollection(); |
||
135 | $items->add($cartItem); |
||
136 | $guestCart = $this->createGuestCart($cartAddress3, $cartAddress4, $items, $status); |
||
137 | $this->updateCartItem($cartItem, $guestCart); |
||
138 | $guestOrder = $this->createGuestOrder($guestCart); |
||
139 | |||
140 | $this->setReference('guestCart', $guestCart); |
||
141 | $this->setReference('guestOrder', $guestOrder); |
||
142 | |||
143 | $baseOrderItem = $this->createBaseOrderItem($guestOrder); |
||
144 | $order->setItems([$baseOrderItem]); |
||
145 | $this->em->persist($guestOrder); |
||
146 | |||
147 | $this->em->flush(); |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * @param $billing |
||
152 | * @param $shipping |
||
153 | * @param Customer $customer |
||
154 | * @param ArrayCollection $item |
||
155 | * @param CartStatus $status |
||
156 | * |
||
157 | * @return Cart |
||
158 | */ |
||
159 | View Code Duplication | protected function createCart($billing, $shipping, Customer $customer, ArrayCollection $item, $status) |
|
190 | |||
191 | /** |
||
192 | * @param $billing |
||
193 | * @param $shipping |
||
194 | * @param ArrayCollection $item |
||
195 | * @param CartStatus $status |
||
196 | * |
||
197 | * @return Cart |
||
198 | */ |
||
199 | View Code Duplication | protected function createGuestCart($billing, $shipping, ArrayCollection $item, $status) |
|
200 | { |
||
201 | $cart = new Cart(); |
||
202 | $cart->setOriginId(101); |
||
203 | $cart->setChannel($this->integration); |
||
204 | $cart->setDataChannel($this->channel); |
||
205 | $cart->setBillingAddress($billing); |
||
206 | $cart->setShippingAddress($shipping); |
||
207 | $cart->setCustomer(null); |
||
208 | $cart->setEmail('[email protected]'); |
||
209 | $cart->setCreatedAt(new \DateTime('now')); |
||
210 | $cart->setUpdatedAt(new \DateTime('now')); |
||
211 | $cart->setCartItems($item); |
||
212 | $cart->setStatus($status); |
||
213 | $cart->setItemsQty(0); |
||
214 | $cart->setItemsCount(1); |
||
215 | $cart->setBaseCurrencyCode('code'); |
||
216 | $cart->setStoreCurrencyCode('code'); |
||
217 | $cart->setQuoteCurrencyCode('usd'); |
||
218 | $cart->setStoreToBaseRate(12); |
||
219 | $cart->setStoreToQuoteRate(12); |
||
220 | $cart->setGrandTotal(2.54); |
||
221 | $cart->setIsGuest(1); |
||
222 | $cart->setStore($this->store); |
||
223 | $cart->setOwner($this->getUser()); |
||
224 | $cart->setOrganization($this->organization); |
||
225 | |||
226 | $this->em->persist($cart); |
||
227 | |||
228 | return $cart; |
||
229 | } |
||
230 | |||
231 | /** |
||
232 | * @param $table |
||
233 | * @param $method |
||
234 | * |
||
235 | * @return array |
||
236 | */ |
||
237 | protected function loadStructure($table, $method) |
||
247 | |||
248 | /** |
||
249 | * @return $this |
||
250 | */ |
||
251 | protected function createIntegration() |
||
268 | |||
269 | /** |
||
270 | * @return $this |
||
271 | */ |
||
272 | protected function createTransport() |
||
291 | |||
292 | /** |
||
293 | * @param $region |
||
294 | * @param $country |
||
295 | * @param $originId |
||
296 | * |
||
297 | * @return CartAddress |
||
298 | */ |
||
299 | View Code Duplication | protected function createCartAddress($region, $country, $originId) |
|
316 | |||
317 | /** |
||
318 | * @param $region |
||
319 | * @param $country |
||
320 | * @param $originId |
||
321 | * |
||
322 | * @return CartAddress |
||
323 | */ |
||
324 | View Code Duplication | protected function createGuestCartAddress($region, $country, $originId) |
|
341 | |||
342 | /** |
||
343 | * @param $region |
||
344 | * @param $country |
||
345 | * |
||
346 | * @return MagentoAddress |
||
347 | */ |
||
348 | protected function createMagentoAddress($region, $country) |
||
369 | |||
370 | /** |
||
371 | * @param $region |
||
372 | * @param $country |
||
373 | * |
||
374 | * @return Address |
||
375 | */ |
||
376 | View Code Duplication | protected function createAddress($region, $country) |
|
392 | |||
393 | /** |
||
394 | * @param $oid |
||
395 | * @param Account $account |
||
396 | * @param MagentoAddress $address |
||
397 | * |
||
398 | * @return Customer |
||
399 | */ |
||
400 | protected function createCustomer($oid, Account $account, MagentoAddress $address) |
||
428 | |||
429 | /** |
||
430 | * @return $this |
||
431 | */ |
||
432 | View Code Duplication | protected function createWebSite() |
|
446 | |||
447 | /** |
||
448 | * @return $this |
||
449 | */ |
||
450 | View Code Duplication | protected function createStore() |
|
465 | |||
466 | /** |
||
467 | * @return Account |
||
468 | */ |
||
469 | View Code Duplication | protected function createAccount() |
|
480 | |||
481 | /** |
||
482 | * @return $this |
||
483 | */ |
||
484 | protected function createCustomerGroup() |
||
497 | |||
498 | /** |
||
499 | * @return $this |
||
500 | */ |
||
501 | protected function createGuestCustomerGroup() |
||
502 | { |
||
503 | $customerGroup = new CustomerGroup; |
||
504 | $customerGroup->setName('NOT LOGGED IN'); |
||
505 | $customerGroup->setChannel($this->integration); |
||
506 | $customerGroup->setOriginId(0); |
||
507 | |||
508 | $this->em->persist($customerGroup); |
||
509 | return $this; |
||
510 | } |
||
511 | |||
512 | /** |
||
513 | * @return CartItem |
||
514 | */ |
||
515 | protected function createCartItem() |
||
539 | |||
540 | /** |
||
541 | * @return CartStatus |
||
542 | */ |
||
543 | protected function getStatus() |
||
549 | |||
550 | /** |
||
551 | * @param CartItem $cartItem |
||
552 | * @param Cart $cart |
||
553 | * |
||
554 | * @return $this |
||
555 | */ |
||
556 | protected function updateCartItem(CartItem $cartItem, Cart $cart) |
||
563 | |||
564 | /** |
||
565 | * @param Cart $cart |
||
566 | * @param Customer $customer |
||
567 | * |
||
568 | * @return Order |
||
569 | */ |
||
570 | View Code Duplication | protected function createOrder(Cart $cart, Customer $customer) |
|
600 | |||
601 | /** |
||
602 | * @param Cart $cart |
||
603 | * |
||
604 | * @return Order |
||
605 | */ |
||
606 | View Code Duplication | protected function createGuestOrder(Cart $cart) |
|
607 | { |
||
608 | $order = new Order(); |
||
609 | $order->setChannel($this->integration); |
||
610 | $order->setDataChannel($this->channel); |
||
611 | $order->setStatus('open'); |
||
612 | $order->setIncrementId('100000308'); |
||
613 | $order->setCreatedAt(new \DateTime('now')); |
||
614 | $order->setUpdatedAt(new \DateTime('now')); |
||
615 | $order->setCart($cart); |
||
616 | $order->setStore($this->store); |
||
617 | $order->setCustomer(null); |
||
618 | $order->setIsGuest(1); |
||
619 | $order->setCustomerEmail('[email protected]'); |
||
620 | $order->setDiscountAmount(4.40); |
||
621 | $order->setTaxAmount(12.47); |
||
622 | $order->setShippingAmount(5); |
||
623 | $order->setTotalPaidAmount(17.85); |
||
624 | $order->setTotalInvoicedAmount(11); |
||
625 | $order->setTotalRefundedAmount(4); |
||
626 | $order->setTotalCanceledAmount(0); |
||
627 | $order->setShippingMethod('some unique shipping method'); |
||
628 | $order->setRemoteIp('127.0.0.1'); |
||
629 | $order->setGiftMessage('some very unique gift message'); |
||
630 | $order->setOwner($this->getUser()); |
||
631 | $order->setOrganization($this->organization); |
||
632 | |||
633 | $this->em->persist($order); |
||
634 | |||
635 | return $order; |
||
636 | } |
||
637 | |||
638 | /** |
||
639 | * @param Order $order |
||
640 | * @return OrderItem |
||
641 | */ |
||
642 | protected function createBaseOrderItem(Order $order) |
||
664 | |||
665 | /** |
||
666 | * @return User |
||
667 | */ |
||
668 | protected function getUser() |
||
674 | |||
675 | /** |
||
676 | * @return LoadMagentoChannel |
||
677 | */ |
||
678 | protected function createChannel() |
||
700 | } |
||
701 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: