Complex classes like CoreContext 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 CoreContext, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
43 | class CoreContext extends DefaultContext |
||
44 | { |
||
45 | /** |
||
46 | * Created orders. |
||
47 | * |
||
48 | * @var OrderInterface[] |
||
49 | */ |
||
50 | protected $orders = array(); |
||
51 | |||
52 | /** |
||
53 | * @Given store has default configuration |
||
54 | */ |
||
55 | public function storeHasDefaultConfiguration() |
||
56 | { |
||
57 | $manager = $this->getEntityManager(); |
||
58 | |||
59 | /** @var CurrencyInterface $currency */ |
||
60 | $currency = $this->getFactory('currency')->createNew(); |
||
61 | $currency->setCode('EUR'); |
||
62 | $currency->setExchangeRate(1); |
||
63 | $manager->persist($currency); |
||
64 | |||
65 | /** @var LocaleInterface $locale */ |
||
66 | $locale = $this->getFactory('locale')->createNew(); |
||
67 | $locale->setCode('en_US'); |
||
68 | $manager->persist($locale); |
||
69 | |||
70 | /* @var ChannelInterface $channel */ |
||
71 | $channel = $this->getFactory('channel')->createNew(); |
||
72 | $channel->setCode('DEFAULT-WEB'); |
||
73 | $channel->setName('Default'); |
||
74 | $channel->setUrl('http://example.com'); |
||
75 | $channel->addCurrency($currency); |
||
76 | $channel->setDefaultCurrency($currency); |
||
77 | $channel->addLocale($locale); |
||
78 | $channel->setDefaultLocale($locale); |
||
79 | $manager->persist($channel); |
||
80 | |||
81 | $manager->flush(); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * @Given I am logged in as :role |
||
86 | */ |
||
87 | public function iAmLoggedInAsAuthorizationRole($role) |
||
88 | { |
||
89 | $this->iAmLoggedInAsRole('ROLE_ADMINISTRATION_ACCESS', '[email protected]', array($role)); |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * @Given /^I am logged in user$/ |
||
94 | * @Given /^I am logged in as user "([^""]*)"$/ |
||
95 | */ |
||
96 | public function iAmLoggedInUser($email = '[email protected]') |
||
97 | { |
||
98 | $this->iAmLoggedInAsRole('ROLE_USER', $email); |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * @Given /^I am not logged in$/ |
||
103 | */ |
||
104 | public function iAmNotLoggedIn() |
||
105 | { |
||
106 | $this->getSession()->restart(); |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * @Given /^there are following orders:$/ |
||
111 | * @Given /^the following orders exist:$/ |
||
112 | * @Given /^there are orders:$/ |
||
113 | * @Given /^the following orders were placed:$/ |
||
114 | */ |
||
115 | public function thereAreOrders(TableNode $table) |
||
116 | { |
||
117 | $manager = $this->getEntityManager(); |
||
118 | $finite = $this->getService('sm.factory'); |
||
119 | $orderFactory = $this->getFactory('order'); |
||
120 | $shipmentProcessor = $this->getService('sylius.processor.shipment_processor'); |
||
121 | |||
122 | /** @var $paymentMethod PaymentMethodInterface */ |
||
123 | $paymentMethod = $this->getFactory('payment_method')->createNew(); |
||
124 | $paymentMethod->setName('Stripe'); |
||
125 | $paymentMethod->setGateway('stripe'); |
||
126 | $paymentMethod->setCode('PM100'); |
||
127 | $manager->persist($paymentMethod); |
||
128 | |||
129 | $currentOrderNumber = 1; |
||
130 | foreach ($table->getHash() as $data) { |
||
131 | $address = $this->createAddress($data['address']); |
||
132 | |||
133 | /* @var $order OrderInterface */ |
||
134 | $order = $orderFactory->createNew(); |
||
135 | $order->setShippingAddress($address); |
||
136 | $order->setBillingAddress($address); |
||
137 | |||
138 | $customer = $this->thereIsCustomer($data['customer']); |
||
139 | $customer->addAddress($address); |
||
140 | $order->setCustomer($customer); |
||
141 | |||
142 | if (isset($data['shipment']) && '' !== trim($data['shipment'])) { |
||
143 | $order->addShipment($this->createShipment($data['shipment'])); |
||
144 | } |
||
145 | |||
146 | $order->setNumber(str_pad($currentOrderNumber, 9, 0, STR_PAD_LEFT)); |
||
147 | |||
148 | $finite->get($order, OrderTransitions::GRAPH)->apply(OrderTransitions::SYLIUS_CREATE); |
||
149 | |||
150 | $this->createPayment($order, $paymentMethod); |
||
151 | |||
152 | $order->setCurrency('EUR'); |
||
153 | $order->setPaymentState(PaymentInterface::STATE_COMPLETED); |
||
154 | |||
155 | $order->complete(); |
||
156 | |||
157 | $shipmentProcessor->updateShipmentStates($order->getShipments(), ShipmentTransitions::SYLIUS_PREPARE); |
||
158 | |||
159 | $manager->persist($order); |
||
160 | |||
161 | $this->orders[$order->getNumber()] = $order; |
||
162 | |||
163 | ++$currentOrderNumber; |
||
164 | } |
||
165 | |||
166 | $manager->flush(); |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * @Given /^order #(\d+) has following items:$/ |
||
171 | */ |
||
172 | public function orderHasFollowingItems($number, TableNode $items) |
||
173 | { |
||
174 | $manager = $this->getEntityManager(); |
||
175 | $orderItemFactory = $this->getFactory('order_item'); |
||
176 | $orderItemQuantityModifier = $this->getService('sylius.order_item_quantity_modifier'); |
||
177 | |||
178 | $order = $this->orders[$number]; |
||
179 | |||
180 | foreach ($items->getHash() as $data) { |
||
181 | $product = $this->findOneByName('product', trim($data['product'])); |
||
182 | |||
183 | /* @var $item OrderItemInterface */ |
||
184 | $item = $orderItemFactory->createNew(); |
||
185 | $item->setVariant($product->getMasterVariant()); |
||
186 | $item->setUnitPrice($product->getMasterVariant()->getPrice()); |
||
187 | |||
188 | $orderItemQuantityModifier->modify($item, $data['quantity']); |
||
189 | |||
190 | $order->addItem($item); |
||
191 | } |
||
192 | |||
193 | $order->complete(); |
||
194 | |||
195 | $this->getService('sylius.order_processing.payment_processor')->createPayment($order); |
||
196 | $this->getService('event_dispatcher')->dispatch(SyliusCartEvents::CART_CHANGE, new GenericEvent($order)); |
||
197 | |||
198 | $order->setPaymentState(PaymentInterface::STATE_COMPLETED); |
||
199 | |||
200 | $manager->persist($order); |
||
201 | $manager->flush(); |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * @Given /^there are following users:$/ |
||
206 | */ |
||
207 | public function thereAreFollowingUsers(TableNode $table) |
||
225 | |||
226 | /** |
||
227 | * @Given /^there are following customers:$/ |
||
228 | * @Given /^the following customers exist:$/ |
||
229 | */ |
||
230 | public function thereAreFollowingCustomers(TableNode $table) |
||
244 | |||
245 | /** |
||
246 | * @Given /^there are groups:$/ |
||
247 | * @Given /^there are following groups:$/ |
||
248 | * @Given /^the following groups exist:$/ |
||
249 | */ |
||
250 | public function thereAreGroups(TableNode $table) |
||
264 | |||
265 | /** |
||
266 | * @Given /^the following addresses exist:$/ |
||
267 | */ |
||
268 | public function theFollowingAddressesExist(TableNode $table) |
||
284 | |||
285 | public function thereIsUser($email, $password, $role = null, $enabled = 'yes', $address = null, $groups = array(), $flush = true, array $authorizationRoles = array(), $createdAt = null) |
||
301 | |||
302 | protected function thereIsCustomer($email, $address = null, $groups = array(), $flush = true, $createdAt = null) |
||
318 | |||
319 | /** |
||
320 | * @Given /^product "([^""]*)" has the following volume based pricing:$/ |
||
321 | */ |
||
322 | public function productHasTheFollowingVolumeBasedPricing($productName, TableNode $table) |
||
353 | |||
354 | /** |
||
355 | * @Given /^product "([^""]*)" has the following group based pricing:$/ |
||
356 | */ |
||
357 | public function productHasTheFollowingGroupBasedPricing($productName, TableNode $table) |
||
377 | |||
378 | /** |
||
379 | * @Given /^there are following tax rates:$/ |
||
380 | * @Given /^the following tax rates exist:$/ |
||
381 | */ |
||
382 | public function thereAreTaxRates(TableNode $table) |
||
390 | |||
391 | /** |
||
392 | * @Given /^there is (\d+)% tax "([^""]*)" with code "([^""]*)" for category "([^""]*)" with zone "([^""]*)"$/ |
||
393 | * @Given /^I created (\d+)% tax "([^""]*)" with code "([^""]*)" for category "([^""]*)" with zone "([^""]*)"$/ |
||
394 | */ |
||
395 | public function thereIsTaxRate($amount, $name, $code, $category, $zone, $includedInPrice = false, $flush = true) |
||
415 | |||
416 | /** |
||
417 | * @Given /^the following shipping methods are configured:$/ |
||
418 | * @Given /^the following shipping methods exist:$/ |
||
419 | * @Given /^there are shipping methods:$/ |
||
420 | */ |
||
421 | public function thereAreShippingMethods(TableNode $table) |
||
437 | |||
438 | /** |
||
439 | * @Given /^I created shipping method "([^""]*)" with code "([^""]*)" and zone "([^""]*)"$/ |
||
440 | * @Given /^There is shipping method "([^""]*)" with code "([^""]*)" and zone "([^""]*)"$/ |
||
441 | * @Given /^there is an enabled shipping method "([^""]*)" with code "([^""]*)" and zone "([^""]*)"$/ |
||
442 | */ |
||
443 | public function thereIsShippingMethod($name, $code, $zoneName, $calculator = DefaultCalculators::PER_ITEM_RATE, TaxCategoryInterface $taxCategory = null, array $configuration = null, $enabled = true, $flush = true) |
||
469 | |||
470 | /** |
||
471 | * @Given /^there is a disabled shipping method "([^""]*)" with code "([^""]*)" and zone "([^""]*)"$/ |
||
472 | */ |
||
473 | public function thereIsDisabledShippingMethod($name, $code, $zoneName) |
||
477 | |||
478 | /** |
||
479 | * @Given /^the following locales are defined:$/ |
||
480 | * @Given /^there are following locales configured:$/ |
||
481 | */ |
||
482 | public function thereAreLocales(TableNode $table) |
||
516 | |||
517 | /** |
||
518 | * @Given /^there are following locales configured and assigned to the default channel:$/ |
||
519 | */ |
||
520 | public function thereAreLocalesAssignedToDefaultChannel(TableNode $table) |
||
535 | |||
536 | /** |
||
537 | * @Given /^product "([^""]*)" is available in all variations$/ |
||
538 | */ |
||
539 | public function productIsAvailableInAllVariations($productName) |
||
548 | |||
549 | /** |
||
550 | * @Given all products are available in all variations |
||
551 | */ |
||
552 | public function allProductsAreAvailableInAllVariations() |
||
564 | |||
565 | /** |
||
566 | * Create an address instance from string. |
||
567 | * |
||
568 | * @param string $string |
||
569 | * |
||
570 | * @return AddressInterface |
||
571 | */ |
||
572 | private function createAddress($string) |
||
588 | |||
589 | /** |
||
590 | * @param string $address |
||
591 | * |
||
592 | * @return array |
||
593 | */ |
||
594 | protected function processAddress($address) |
||
601 | |||
602 | /** |
||
603 | * Create an payment instance. |
||
604 | * |
||
605 | * @param OrderInterface $order |
||
606 | * @param PaymentMethodInterface $method |
||
607 | */ |
||
608 | private function createPayment(OrderInterface $order, PaymentMethodInterface $method) |
||
620 | |||
621 | /** |
||
622 | * Create an shipment instance from string. |
||
623 | * |
||
624 | * @param string $string |
||
625 | * |
||
626 | * @return ShipmentInterface |
||
627 | */ |
||
628 | private function createShipment($string) |
||
648 | |||
649 | /** |
||
650 | * Create user and login with given role. |
||
651 | * |
||
652 | * @param string $role |
||
653 | * @param string $email |
||
654 | * @param array $authorizationRoles |
||
655 | */ |
||
656 | private function iAmLoggedInAsRole($role, $email = '[email protected]', array $authorizationRoles = array()) |
||
671 | |||
672 | /** |
||
673 | * @param GroupableInterface $groupableObject |
||
674 | * @param array $groups |
||
675 | */ |
||
676 | protected function assignGroups(GroupableInterface $groupableObject, array $groups) |
||
684 | |||
685 | /** |
||
686 | * @param array $authorizationRoles |
||
687 | * @param UserInterface $user |
||
688 | */ |
||
689 | protected function assignAuthorizationRoles(UserInterface $user, array $authorizationRoles = array()) |
||
702 | |||
703 | /** |
||
704 | * @param $email |
||
705 | * @param $address |
||
706 | * @param $groups |
||
707 | * @param $createdAt |
||
708 | * |
||
709 | * @return CustomerInterface |
||
710 | */ |
||
711 | protected function createCustomer($email, $address = null, $groups = array(), $createdAt = null) |
||
728 | |||
729 | /** |
||
730 | * @param $email |
||
731 | * @param $password |
||
732 | * @param $role |
||
733 | * @param $enabled |
||
734 | * @param $address |
||
735 | * @param $groups |
||
736 | * @param array $authorizationRoles |
||
737 | * @param $createdAt |
||
738 | * |
||
739 | * @return UserInterface |
||
740 | */ |
||
741 | protected function createUser($email, $password, $role = null, $enabled = 'yes', $address = null, array $groups = array(), array $authorizationRoles = array(), $createdAt = null) |
||
762 | |||
763 | /** |
||
764 | * @param string $role |
||
765 | * |
||
766 | * @return RoleInterface |
||
767 | */ |
||
768 | protected function createAuthorizationRole($role) |
||
777 | |||
778 | /** |
||
779 | * @param ProductInterface $product |
||
780 | */ |
||
781 | private function generateProductVariations($product) |
||
791 | |||
792 | private function prepareSessionIfNeeded() |
||
804 | } |
||
805 |