| Conditions | 6 |
| Paths | 6 |
| Total Lines | 72 |
| Code Lines | 47 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php declare(strict_types=1); |
||
| 90 | public function switchContext(RequestDataBag $data, SalesChannelContext $context): ContextTokenResponse |
||
| 91 | { |
||
| 92 | $definition = new DataValidationDefinition('context_switch'); |
||
| 93 | |||
| 94 | $parameters = $data->only( |
||
| 95 | self::SHIPPING_METHOD_ID, |
||
| 96 | self::PAYMENT_METHOD_ID, |
||
| 97 | self::BILLING_ADDRESS_ID, |
||
| 98 | self::SHIPPING_ADDRESS_ID, |
||
| 99 | self::COUNTRY_ID, |
||
| 100 | self::STATE_ID, |
||
| 101 | self::CURRENCY_ID, |
||
| 102 | self::LANGUAGE_ID |
||
| 103 | ); |
||
| 104 | |||
| 105 | $addressCriteria = new Criteria(); |
||
| 106 | if ($context->getCustomer()) { |
||
| 107 | $addressCriteria->addFilter(new EqualsFilter('customer_address.customerId', $context->getCustomer()->getId())); |
||
| 108 | } else { |
||
| 109 | // do not allow to set address ids if the customer is not logged in |
||
| 110 | if (isset($parameters[self::SHIPPING_ADDRESS_ID])) { |
||
| 111 | throw new CustomerNotLoggedInException(); |
||
| 112 | } |
||
| 113 | |||
| 114 | if (isset($parameters[self::BILLING_ADDRESS_ID])) { |
||
| 115 | throw new CustomerNotLoggedInException(); |
||
| 116 | } |
||
| 117 | } |
||
| 118 | |||
| 119 | $currencyCriteria = new Criteria(); |
||
| 120 | $currencyCriteria->addFilter( |
||
| 121 | new EqualsFilter('currency.salesChannels.id', $context->getSalesChannel()->getId()) |
||
| 122 | ); |
||
| 123 | |||
| 124 | $languageCriteria = new Criteria(); |
||
| 125 | $languageCriteria->addFilter( |
||
| 126 | new EqualsFilter('language.salesChannels.id', $context->getSalesChannel()->getId()) |
||
| 127 | ); |
||
| 128 | |||
| 129 | $paymentMethodCriteria = new Criteria(); |
||
| 130 | $paymentMethodCriteria->addFilter( |
||
| 131 | new EqualsFilter('payment_method.salesChannels.id', $context->getSalesChannel()->getId()) |
||
| 132 | ); |
||
| 133 | |||
| 134 | $shippingMethodCriteria = new Criteria(); |
||
| 135 | $shippingMethodCriteria->addFilter( |
||
| 136 | new EqualsFilter('shipping_method.salesChannels.id', $context->getSalesChannel()->getId()) |
||
| 137 | ); |
||
| 138 | |||
| 139 | $definition |
||
| 140 | ->add(self::LANGUAGE_ID, new EntityExists(['entity' => 'language', 'context' => $context->getContext(), 'criteria' => $languageCriteria])) |
||
| 141 | ->add(self::CURRENCY_ID, new EntityExists(['entity' => 'currency', 'context' => $context->getContext(), 'criteria' => $currencyCriteria])) |
||
| 142 | ->add(self::SHIPPING_METHOD_ID, new EntityExists(['entity' => 'shipping_method', 'context' => $context->getContext(), 'criteria' => $shippingMethodCriteria])) |
||
| 143 | ->add(self::PAYMENT_METHOD_ID, new EntityExists(['entity' => 'payment_method', 'context' => $context->getContext(), 'criteria' => $paymentMethodCriteria])) |
||
| 144 | ->add(self::BILLING_ADDRESS_ID, new EntityExists(['entity' => 'customer_address', 'context' => $context->getContext(), 'criteria' => $addressCriteria])) |
||
| 145 | ->add(self::SHIPPING_ADDRESS_ID, new EntityExists(['entity' => 'customer_address', 'context' => $context->getContext(), 'criteria' => $addressCriteria])) |
||
| 146 | ->add(self::COUNTRY_ID, new EntityExists(['entity' => 'country', 'context' => $context->getContext()])) |
||
| 147 | ->add(self::STATE_ID, new EntityExists(['entity' => 'country_state', 'context' => $context->getContext()])) |
||
| 148 | ; |
||
| 149 | |||
| 150 | $this->validator->validate($parameters, $definition); |
||
| 151 | |||
| 152 | if (Feature::isActive('FEATURE_NEXT_10058') && $customer = $context->getCustomer()) { |
||
| 153 | $this->contextPersister->save($context->getToken(), $parameters, $customer->getId()); |
||
| 154 | } else { |
||
| 155 | $this->contextPersister->save($context->getToken(), $parameters); |
||
| 156 | } |
||
| 157 | |||
| 158 | $event = new SalesChannelContextSwitchEvent($context, $data); |
||
| 159 | $this->eventDispatcher->dispatch($event); |
||
| 160 | |||
| 161 | return new ContextTokenResponse($context->getToken()); |
||
| 162 | } |
||
| 164 |