| Total Complexity | 53 |
| Total Lines | 393 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like OrderConverter 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.
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 OrderConverter, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 54 | #[Package('checkout')] |
||
| 55 | class OrderConverter |
||
| 56 | { |
||
| 57 | public const CART_CONVERTED_TO_ORDER_EVENT = 'cart.convertedToOrder.event'; |
||
| 58 | |||
| 59 | public const CART_TYPE = 'recalculation'; |
||
| 60 | |||
| 61 | public const ORIGINAL_ID = 'originalId'; |
||
| 62 | |||
| 63 | public const ORIGINAL_ORDER_NUMBER = 'originalOrderNumber'; |
||
| 64 | |||
| 65 | public const ORIGINAL_DOWNLOADS = 'originalDownloads'; |
||
| 66 | |||
| 67 | public const ADMIN_EDIT_ORDER_PERMISSIONS = [ |
||
| 68 | ProductCartProcessor::ALLOW_PRODUCT_PRICE_OVERWRITES => true, |
||
| 69 | ProductCartProcessor::SKIP_PRODUCT_RECALCULATION => true, |
||
| 70 | DeliveryProcessor::SKIP_DELIVERY_PRICE_RECALCULATION => true, |
||
| 71 | DeliveryProcessor::SKIP_DELIVERY_TAX_RECALCULATION => true, |
||
| 72 | PromotionCollector::SKIP_PROMOTION => true, |
||
| 73 | ProductCartProcessor::SKIP_PRODUCT_STOCK_VALIDATION => true, |
||
| 74 | ProductCartProcessor::KEEP_INACTIVE_PRODUCT => true, |
||
| 75 | ]; |
||
| 76 | |||
| 77 | protected EntityRepositoryInterface $customerRepository; |
||
| 78 | |||
| 79 | protected AbstractSalesChannelContextFactory $salesChannelContextFactory; |
||
| 80 | |||
| 81 | protected EventDispatcherInterface $eventDispatcher; |
||
| 82 | |||
| 83 | private NumberRangeValueGeneratorInterface $numberRangeValueGenerator; |
||
| 84 | |||
| 85 | private OrderDefinition $orderDefinition; |
||
| 86 | |||
| 87 | private EntityRepositoryInterface $orderAddressRepository; |
||
| 88 | |||
| 89 | private InitialStateIdLoader $initialStateIdLoader; |
||
| 90 | |||
| 91 | private LineItemDownloadLoader $downloadLoader; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @internal |
||
| 95 | */ |
||
| 96 | public function __construct( |
||
| 97 | EntityRepositoryInterface $customerRepository, |
||
| 98 | AbstractSalesChannelContextFactory $salesChannelContextFactory, |
||
| 99 | EventDispatcherInterface $eventDispatcher, |
||
| 100 | NumberRangeValueGeneratorInterface $numberRangeValueGenerator, |
||
| 101 | OrderDefinition $orderDefinition, |
||
| 102 | EntityRepositoryInterface $orderAddressRepository, |
||
| 103 | InitialStateIdLoader $initialStateIdLoader, |
||
| 104 | LineItemDownloadLoader $downloadLoader |
||
| 105 | ) { |
||
| 106 | $this->customerRepository = $customerRepository; |
||
| 107 | $this->salesChannelContextFactory = $salesChannelContextFactory; |
||
| 108 | $this->eventDispatcher = $eventDispatcher; |
||
| 109 | $this->numberRangeValueGenerator = $numberRangeValueGenerator; |
||
| 110 | $this->orderDefinition = $orderDefinition; |
||
| 111 | $this->orderAddressRepository = $orderAddressRepository; |
||
| 112 | $this->initialStateIdLoader = $initialStateIdLoader; |
||
| 113 | $this->downloadLoader = $downloadLoader; |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @throws DeliveryWithoutAddressException |
||
| 118 | * |
||
| 119 | * @return array<string, mixed|float|string|array<int, array<string, string|int|bool|mixed>>|null> |
||
| 120 | */ |
||
| 121 | public function convertToOrder(Cart $cart, SalesChannelContext $context, OrderConversionContext $conversionContext): array |
||
| 122 | { |
||
| 123 | foreach ($cart->getDeliveries() as $delivery) { |
||
| 124 | if ($delivery->getLocation()->getAddress() !== null || $delivery->hasExtensionOfType(self::ORIGINAL_ID, IdStruct::class)) { |
||
| 125 | continue; |
||
| 126 | } |
||
| 127 | |||
| 128 | throw new DeliveryWithoutAddressException(); |
||
| 129 | } |
||
| 130 | $data = CartTransformer::transform( |
||
| 131 | $cart, |
||
| 132 | $context, |
||
| 133 | $this->initialStateIdLoader->get(OrderStates::STATE_MACHINE), |
||
| 134 | $conversionContext->shouldIncludeOrderDate() |
||
| 135 | ); |
||
| 136 | |||
| 137 | if ($conversionContext->shouldIncludeCustomer()) { |
||
| 138 | $customer = $context->getCustomer(); |
||
| 139 | if ($customer === null) { |
||
| 140 | throw CartException::customerNotLoggedIn(); |
||
| 141 | } |
||
| 142 | |||
| 143 | $data['orderCustomer'] = CustomerTransformer::transform($customer); |
||
| 144 | } |
||
| 145 | |||
| 146 | $data['languageId'] = $context->getLanguageId(); |
||
| 147 | |||
| 148 | $convertedLineItems = LineItemTransformer::transformCollection($cart->getLineItems()); |
||
| 149 | $shippingAddresses = []; |
||
| 150 | |||
| 151 | if ($conversionContext->shouldIncludeDeliveries()) { |
||
| 152 | $shippingAddresses = AddressTransformer::transformCollection($cart->getDeliveries()->getAddresses(), true); |
||
| 153 | $data['deliveries'] = DeliveryTransformer::transformCollection( |
||
| 154 | $cart->getDeliveries(), |
||
| 155 | $convertedLineItems, |
||
| 156 | $this->initialStateIdLoader->get(OrderDeliveryStates::STATE_MACHINE), |
||
| 157 | $context->getContext(), |
||
| 158 | $shippingAddresses |
||
| 159 | ); |
||
| 160 | } |
||
| 161 | |||
| 162 | if ($conversionContext->shouldIncludeBillingAddress()) { |
||
| 163 | $customer = $context->getCustomer(); |
||
| 164 | if ($customer === null) { |
||
| 165 | throw CartException::customerNotLoggedIn(); |
||
| 166 | } |
||
| 167 | |||
| 168 | $activeBillingAddress = $customer->getActiveBillingAddress(); |
||
| 169 | if ($activeBillingAddress === null) { |
||
| 170 | throw new AddressNotFoundException(''); |
||
| 171 | } |
||
| 172 | $customerAddressId = $activeBillingAddress->getId(); |
||
| 173 | |||
| 174 | if (\array_key_exists($customerAddressId, $shippingAddresses)) { |
||
| 175 | $billingAddressId = $shippingAddresses[$customerAddressId]['id']; |
||
| 176 | } else { |
||
| 177 | $billingAddress = AddressTransformer::transform($activeBillingAddress); |
||
| 178 | $data['addresses'] = [$billingAddress]; |
||
| 179 | $billingAddressId = $billingAddress['id']; |
||
| 180 | } |
||
| 181 | $data['billingAddressId'] = $billingAddressId; |
||
| 182 | } |
||
| 183 | |||
| 184 | if ($conversionContext->shouldIncludeTransactions()) { |
||
| 185 | $data['transactions'] = TransactionTransformer::transformCollection( |
||
| 186 | $cart->getTransactions(), |
||
| 187 | $this->initialStateIdLoader->get(OrderTransactionStates::STATE_MACHINE), |
||
| 188 | $context->getContext() |
||
| 189 | ); |
||
| 190 | } |
||
| 191 | |||
| 192 | $data['lineItems'] = array_values($convertedLineItems); |
||
| 193 | |||
| 194 | foreach ($this->downloadLoader->load($data['lineItems'], $context->getContext()) as $key => $downloads) { |
||
| 195 | if (!\array_key_exists($key, $data['lineItems'])) { |
||
| 196 | continue; |
||
| 197 | } |
||
| 198 | |||
| 199 | $data['lineItems'][$key]['downloads'] = $downloads; |
||
| 200 | } |
||
| 201 | |||
| 202 | /** @var IdStruct|null $idStruct */ |
||
| 203 | $idStruct = $cart->getExtensionOfType(self::ORIGINAL_ID, IdStruct::class); |
||
| 204 | $data['id'] = $idStruct ? $idStruct->getId() : Uuid::randomHex(); |
||
|
|
|||
| 205 | |||
| 206 | /** @var IdStruct|null $orderNumberStruct */ |
||
| 207 | $orderNumberStruct = $cart->getExtensionOfType(self::ORIGINAL_ORDER_NUMBER, IdStruct::class); |
||
| 208 | if ($orderNumberStruct !== null) { |
||
| 209 | $data['orderNumber'] = $orderNumberStruct->getId(); |
||
| 210 | } else { |
||
| 211 | $data['orderNumber'] = $this->numberRangeValueGenerator->getValue( |
||
| 212 | $this->orderDefinition->getEntityName(), |
||
| 213 | $context->getContext(), |
||
| 214 | $context->getSalesChannel()->getId() |
||
| 215 | ); |
||
| 216 | } |
||
| 217 | |||
| 218 | $data['ruleIds'] = $context->getRuleIds(); |
||
| 219 | |||
| 220 | $event = new CartConvertedEvent($cart, $data, $context, $conversionContext); |
||
| 221 | $this->eventDispatcher->dispatch($event); |
||
| 222 | |||
| 223 | return $event->getConvertedCart(); |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @throws InvalidPayloadException |
||
| 228 | * @throws InvalidQuantityException |
||
| 229 | * @throws LineItemNotStackableException |
||
| 230 | * @throws MixedLineItemTypeException |
||
| 231 | * @throws MissingOrderRelationException |
||
| 232 | */ |
||
| 233 | public function convertToCart(OrderEntity $order, Context $context): Cart |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @param array<string, array<string, bool>|string> $overrideOptions |
||
| 286 | * |
||
| 287 | * @throws InconsistentCriteriaIdsException |
||
| 288 | */ |
||
| 289 | public function assembleSalesChannelContext(OrderEntity $order, Context $context, array $overrideOptions = []): SalesChannelContext |
||
| 290 | { |
||
| 291 | if ($order->getTransactions() === null) { |
||
| 292 | if (Feature::isActive('v6.5.0.0')) { |
||
| 293 | throw OrderException::missingAssociation('transactions'); |
||
| 294 | } |
||
| 295 | |||
| 296 | throw new MissingOrderRelationException('transactions'); |
||
| 297 | } |
||
| 298 | if ($order->getOrderCustomer() === null) { |
||
| 299 | if (Feature::isActive('v6.5.0.0')) { |
||
| 300 | throw OrderException::missingAssociation('orderCustomer'); |
||
| 301 | } |
||
| 302 | |||
| 303 | throw new MissingOrderRelationException('orderCustomer'); |
||
| 304 | } |
||
| 305 | |||
| 306 | $customerId = $order->getOrderCustomer()->getCustomerId(); |
||
| 307 | $customerGroupId = null; |
||
| 308 | |||
| 309 | if ($customerId) { |
||
| 310 | /** @var CustomerEntity|null $customer */ |
||
| 311 | $customer = $this->customerRepository->search(new Criteria([$customerId]), $context)->get($customerId); |
||
| 312 | if ($customer !== null) { |
||
| 313 | $customerGroupId = $customer->getGroupId(); |
||
| 314 | } |
||
| 315 | } |
||
| 316 | |||
| 317 | $billingAddressId = $order->getBillingAddressId(); |
||
| 318 | /** @var OrderAddressEntity|null $billingAddress */ |
||
| 319 | $billingAddress = $this->orderAddressRepository->search(new Criteria([$billingAddressId]), $context)->get($billingAddressId); |
||
| 320 | if ($billingAddress === null) { |
||
| 321 | throw new AddressNotFoundException($billingAddressId); |
||
| 322 | } |
||
| 323 | |||
| 324 | $options = [ |
||
| 325 | SalesChannelContextService::CURRENCY_ID => $order->getCurrencyId(), |
||
| 326 | SalesChannelContextService::LANGUAGE_ID => $order->getLanguageId(), |
||
| 327 | SalesChannelContextService::CUSTOMER_ID => $customerId, |
||
| 328 | SalesChannelContextService::COUNTRY_STATE_ID => $billingAddress->getCountryStateId(), |
||
| 329 | SalesChannelContextService::CUSTOMER_GROUP_ID => $customerGroupId, |
||
| 330 | SalesChannelContextService::PERMISSIONS => self::ADMIN_EDIT_ORDER_PERMISSIONS, |
||
| 331 | SalesChannelContextService::VERSION_ID => $context->getVersionId(), |
||
| 332 | ]; |
||
| 333 | |||
| 334 | $delivery = $order->getDeliveries() !== null ? $order->getDeliveries()->first() : null; |
||
| 335 | if ($delivery !== null) { |
||
| 336 | $options[SalesChannelContextService::SHIPPING_METHOD_ID] = $delivery->getShippingMethodId(); |
||
| 337 | } |
||
| 338 | |||
| 339 | //get the first not paid transaction and not cancelled or, if all paid or cancelled, the last transaction |
||
| 340 | if ($order->getTransactions() !== null) { |
||
| 341 | foreach ($order->getTransactions() as $transaction) { |
||
| 342 | $options[SalesChannelContextService::PAYMENT_METHOD_ID] = $transaction->getPaymentMethodId(); |
||
| 343 | if ( |
||
| 344 | $transaction->getStateMachineState() !== null |
||
| 345 | && $transaction->getStateMachineState()->getTechnicalName() !== OrderTransactionStates::STATE_PAID |
||
| 346 | && $transaction->getStateMachineState()->getTechnicalName() !== OrderTransactionStates::STATE_CANCELLED |
||
| 347 | ) { |
||
| 348 | break; |
||
| 349 | } |
||
| 350 | } |
||
| 351 | } |
||
| 352 | |||
| 353 | $options = array_merge($options, $overrideOptions); |
||
| 354 | |||
| 355 | $salesChannelContext = $this->salesChannelContextFactory->create(Uuid::randomHex(), $order->getSalesChannelId(), $options); |
||
| 356 | $salesChannelContext->getContext()->addExtensions($context->getExtensions()); |
||
| 357 | $salesChannelContext->addState(...$context->getStates()); |
||
| 358 | $salesChannelContext->setTaxState($order->getTaxStatus()); |
||
| 359 | |||
| 360 | if ($context->hasState(Context::SKIP_TRIGGER_FLOW)) { |
||
| 361 | $salesChannelContext->getContext()->addState(Context::SKIP_TRIGGER_FLOW); |
||
| 362 | } |
||
| 363 | |||
| 364 | if ($order->getItemRounding() !== null) { |
||
| 365 | $salesChannelContext->setItemRounding($order->getItemRounding()); |
||
| 366 | } |
||
| 367 | |||
| 368 | if ($order->getTotalRounding() !== null) { |
||
| 369 | $salesChannelContext->setTotalRounding($order->getTotalRounding()); |
||
| 370 | } |
||
| 371 | |||
| 372 | if ($order->getRuleIds() !== null) { |
||
| 373 | $salesChannelContext->setRuleIds($order->getRuleIds()); |
||
| 374 | } |
||
| 375 | |||
| 376 | return $salesChannelContext; |
||
| 377 | } |
||
| 378 | |||
| 379 | private function convertDeliveries(OrderDeliveryCollection $orderDeliveries, LineItemCollection $lineItems): DeliveryCollection |
||
| 447 | } |
||
| 448 | } |
||
| 449 |