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 |
||
42 | class CoreContext extends DefaultContext |
||
43 | { |
||
44 | /** |
||
45 | * Created orders. |
||
46 | * |
||
47 | * @var OrderInterface[] |
||
48 | */ |
||
49 | protected $orders = array(); |
||
50 | |||
51 | /** |
||
52 | * @Given store has default configuration |
||
53 | */ |
||
54 | public function storeHasDefaultConfiguration() |
||
55 | { |
||
56 | $manager = $this->getEntityManager(); |
||
57 | |||
58 | /** @var CurrencyInterface $currency */ |
||
59 | $currency = $this->getFactory('currency')->createNew(); |
||
60 | $currency->setCode('EUR'); |
||
61 | $currency->setExchangeRate(1); |
||
62 | $manager->persist($currency); |
||
63 | |||
64 | /** @var LocaleInterface $locale */ |
||
65 | $locale = $this->getFactory('locale')->createNew(); |
||
66 | $locale->setCode('en_US'); |
||
67 | $manager->persist($locale); |
||
68 | |||
69 | /* @var ChannelInterface $channel */ |
||
70 | $channel = $this->getFactory('channel')->createNew(); |
||
71 | $channel->setCode('DEFAULT-WEB'); |
||
72 | $channel->setName('Default'); |
||
73 | $channel->setUrl('http://example.com'); |
||
74 | $channel->addCurrency($currency); |
||
75 | $channel->setDefaultCurrency($currency); |
||
76 | $channel->addLocale($locale); |
||
77 | $channel->setDefaultLocale($locale); |
||
78 | $manager->persist($channel); |
||
79 | |||
80 | $manager->flush(); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @Given I am logged in as :role |
||
85 | */ |
||
86 | public function iAmLoggedInAsAuthorizationRole($role) |
||
90 | |||
91 | /** |
||
92 | * @Given /^I am logged in user$/ |
||
93 | * @Given /^I am logged in as user "([^""]*)"$/ |
||
94 | */ |
||
95 | public function iAmLoggedInUser($email = '[email protected]') |
||
99 | |||
100 | /** |
||
101 | * @Given /^I am not logged in$/ |
||
102 | */ |
||
103 | public function iAmNotLoggedIn() |
||
107 | |||
108 | /** |
||
109 | * @Given /^there are following orders:$/ |
||
110 | * @Given /^the following orders exist:$/ |
||
111 | * @Given /^there are orders:$/ |
||
112 | * @Given /^the following orders were placed:$/ |
||
113 | */ |
||
114 | public function thereAreOrders(TableNode $table) |
||
167 | |||
168 | /** |
||
169 | * @Given /^order #(\d+) has following items:$/ |
||
170 | */ |
||
171 | public function orderHasFollowingItems($number, TableNode $items) |
||
172 | { |
||
173 | $manager = $this->getEntityManager(); |
||
174 | $orderItemFactory = $this->getFactory('order_item'); |
||
175 | $orderItemQuantityModifier = $this->getService('sylius.order_item_quantity_modifier'); |
||
176 | |||
177 | $order = $this->orders[$number]; |
||
178 | |||
179 | foreach ($items->getHash() as $data) { |
||
180 | $product = $this->findOneByName('product', trim($data['product'])); |
||
181 | |||
182 | /* @var $item OrderItemInterface */ |
||
183 | $item = $orderItemFactory->createNew(); |
||
184 | $item->setVariant($product->getMasterVariant()); |
||
185 | $item->setUnitPrice($product->getMasterVariant()->getPrice()); |
||
186 | |||
187 | $orderItemQuantityModifier->modify($item, $data['quantity']); |
||
188 | |||
189 | $order->addItem($item); |
||
190 | } |
||
191 | |||
192 | $order->complete(); |
||
193 | |||
194 | $this->getService('sylius.order_processing.payment_processor')->createPayment($order); |
||
195 | $this->getService('event_dispatcher')->dispatch(SyliusCartEvents::CART_CHANGE, new GenericEvent($order)); |
||
196 | |||
197 | $order->setPaymentState(PaymentInterface::STATE_COMPLETED); |
||
198 | |||
199 | $manager->persist($order); |
||
200 | $manager->flush(); |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * @Given /^there are following users:$/ |
||
205 | */ |
||
206 | public function thereAreFollowingUsers(TableNode $table) |
||
224 | |||
225 | /** |
||
226 | * @Given /^there are following customers:$/ |
||
227 | * @Given /^the following customers exist:$/ |
||
228 | */ |
||
229 | public function thereAreFollowingCustomers(TableNode $table) |
||
243 | |||
244 | /** |
||
245 | * @Given /^there are groups:$/ |
||
246 | * @Given /^there are following groups:$/ |
||
247 | * @Given /^the following groups exist:$/ |
||
248 | */ |
||
249 | public function thereAreGroups(TableNode $table) |
||
263 | |||
264 | /** |
||
265 | * @Given /^the following addresses exist:$/ |
||
266 | */ |
||
267 | public function theFollowingAddressesExist(TableNode $table) |
||
283 | |||
284 | public function thereIsUser($email, $password, $role = null, $enabled = 'yes', $address = null, $groups = array(), $flush = true, array $authorizationRoles = array(), $createdAt = null) |
||
300 | |||
301 | protected function thereIsCustomer($email, $address = null, $groups = array(), $flush = true, $createdAt = null) |
||
317 | |||
318 | /** |
||
319 | * @Given /^product "([^""]*)" has the following volume based pricing:$/ |
||
320 | */ |
||
321 | public function productHasTheFollowingVolumeBasedPricing($productName, TableNode $table) |
||
352 | |||
353 | /** |
||
354 | * @Given /^product "([^""]*)" has the following group based pricing:$/ |
||
355 | */ |
||
356 | public function productHasTheFollowingGroupBasedPricing($productName, TableNode $table) |
||
376 | |||
377 | /** |
||
378 | * @Given /^there are following tax rates:$/ |
||
379 | * @Given /^the following tax rates exist:$/ |
||
380 | */ |
||
381 | public function thereAreTaxRates(TableNode $table) |
||
389 | |||
390 | /** |
||
391 | * @Given /^there is (\d+)% tax "([^""]*)" with code "([^""]*)" for category "([^""]*)" with zone "([^""]*)"$/ |
||
392 | * @Given /^I created (\d+)% tax "([^""]*)" with code "([^""]*)" for category "([^""]*)" with zone "([^""]*)"$/ |
||
393 | */ |
||
394 | public function thereIsTaxRate($amount, $name, $code, $category, $zone, $includedInPrice = false, $flush = true) |
||
414 | |||
415 | /** |
||
416 | * @Given /^the following shipping methods are configured:$/ |
||
417 | * @Given /^the following shipping methods exist:$/ |
||
418 | * @Given /^there are shipping methods:$/ |
||
419 | */ |
||
420 | public function thereAreShippingMethods(TableNode $table) |
||
435 | |||
436 | /** |
||
437 | * @Given /^I created shipping method "([^""]*)" with code "([^""]*)" and zone "([^""]*)"$/ |
||
438 | * @Given /^There is shipping method "([^""]*)" with code "([^""]*)" and zone "([^""]*)"$/ |
||
439 | * @Given /^there is an enabled shipping method "([^""]*)" with code "([^""]*)" and zone "([^""]*)"$/ |
||
440 | */ |
||
441 | public function thereIsShippingMethod($name, $code, $zoneName, $calculator = DefaultCalculators::PER_ITEM_RATE, array $configuration = null, $enabled = true, $flush = true) |
||
466 | |||
467 | /** |
||
468 | * @Given /^there is a disabled shipping method "([^""]*)" with code "([^""]*)" and zone "([^""]*)"$/ |
||
469 | */ |
||
470 | public function thereIsDisabledShippingMethod($name, $code, $zoneName) |
||
474 | |||
475 | /** |
||
476 | * @Given /^the following locales are defined:$/ |
||
477 | * @Given /^there are following locales configured:$/ |
||
478 | */ |
||
479 | public function thereAreLocales(TableNode $table) |
||
513 | |||
514 | /** |
||
515 | * @Given /^there are following locales configured and assigned to the default channel:$/ |
||
516 | */ |
||
517 | public function thereAreLocalesAssignedToDefaultChannel(TableNode $table) |
||
532 | |||
533 | /** |
||
534 | * @Given /^product "([^""]*)" is available in all variations$/ |
||
535 | */ |
||
536 | public function productIsAvailableInAllVariations($productName) |
||
545 | |||
546 | /** |
||
547 | * @Given all products are available in all variations |
||
548 | */ |
||
549 | public function allProductsAreAvailableInAllVariations() |
||
561 | |||
562 | /** |
||
563 | * @Then the customer with email :email should have username :username |
||
564 | */ |
||
565 | public function theCustomerWithEmailShouldHaveUsername($email, $username) |
||
573 | |||
574 | /** |
||
575 | * @Then the customer with email :email should be enabled |
||
576 | */ |
||
577 | public function theCustomerWithEmailShouldBeEnabled($email) |
||
585 | |||
586 | /** |
||
587 | * Create an address instance from string. |
||
588 | * |
||
589 | * @param string $string |
||
590 | * |
||
591 | * @return AddressInterface |
||
592 | */ |
||
593 | private function createAddress($string) |
||
609 | |||
610 | /** |
||
611 | * @param string $address |
||
612 | * |
||
613 | * @return array |
||
614 | */ |
||
615 | protected function processAddress($address) |
||
622 | |||
623 | /** |
||
624 | * Create an payment instance. |
||
625 | * |
||
626 | * @param OrderInterface $order |
||
627 | * @param PaymentMethodInterface $method |
||
628 | */ |
||
629 | private function createPayment(OrderInterface $order, PaymentMethodInterface $method) |
||
641 | |||
642 | /** |
||
643 | * Create an shipment instance from string. |
||
644 | * |
||
645 | * @param string $string |
||
646 | * |
||
647 | * @return ShipmentInterface |
||
648 | */ |
||
649 | private function createShipment($string) |
||
669 | |||
670 | /** |
||
671 | * Create user and login with given role. |
||
672 | * |
||
673 | * @param string $role |
||
674 | * @param string $email |
||
675 | * @param array $authorizationRoles |
||
676 | */ |
||
677 | private function iAmLoggedInAsRole($role, $email = '[email protected]', array $authorizationRoles = array()) |
||
692 | |||
693 | /** |
||
694 | * @param GroupableInterface $groupableObject |
||
695 | * @param array $groups |
||
696 | */ |
||
697 | protected function assignGroups(GroupableInterface $groupableObject, array $groups) |
||
705 | |||
706 | /** |
||
707 | * @param array $authorizationRoles |
||
708 | * @param UserInterface $user |
||
709 | */ |
||
710 | protected function assignAuthorizationRoles(UserInterface $user, array $authorizationRoles = array()) |
||
723 | |||
724 | /** |
||
725 | * @param $email |
||
726 | * @param $address |
||
727 | * @param $groups |
||
728 | * @param $createdAt |
||
729 | * |
||
730 | * @return CustomerInterface |
||
731 | */ |
||
732 | protected function createCustomer($email, $address = null, $groups = array(), $createdAt = null) |
||
749 | |||
750 | /** |
||
751 | * @param $email |
||
752 | * @param $password |
||
753 | * @param $role |
||
754 | * @param $enabled |
||
755 | * @param $address |
||
756 | * @param $groups |
||
757 | * @param array $authorizationRoles |
||
758 | * @param $createdAt |
||
759 | * |
||
760 | * @return UserInterface |
||
761 | */ |
||
762 | protected function createUser($email, $password, $role = null, $enabled = 'yes', $address = null, array $groups = array(), array $authorizationRoles = array(), $createdAt = null) |
||
783 | |||
784 | /** |
||
785 | * @param string $role |
||
786 | * |
||
787 | * @return RoleInterface |
||
788 | */ |
||
789 | protected function createAuthorizationRole($role) |
||
798 | |||
799 | /** |
||
800 | * @param ProductInterface $product |
||
801 | */ |
||
802 | private function generateProductVariations($product) |
||
812 | |||
813 | private function prepareSessionIfNeeded() |
||
825 | |||
826 | /** |
||
827 | * @param $email |
||
828 | * |
||
829 | * @return \Sylius\Component\User\Model\UserInterface |
||
830 | * @throws \Exception |
||
831 | */ |
||
832 | private function getUserWithEmail($email) |
||
846 | } |
||
847 |