| Total Complexity | 45 |
| Total Lines | 353 |
| Duplicated Lines | 0 % |
| Changes | 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); |
||
| 49 | class OrderConverter |
||
| 50 | { |
||
| 51 | public const CART_CONVERTED_TO_ORDER_EVENT = 'cart.convertedToOrder.event'; |
||
| 52 | |||
| 53 | public const CART_TYPE = 'recalculation'; |
||
| 54 | |||
| 55 | public const ORIGINAL_ID = 'originalId'; |
||
| 56 | |||
| 57 | public const ORIGINAL_ORDER_NUMBER = 'originalOrderNumber'; |
||
| 58 | |||
| 59 | public const ADMIN_EDIT_ORDER_PERMISSIONS = [ |
||
| 60 | ProductCartProcessor::ALLOW_PRODUCT_PRICE_OVERWRITES => true, |
||
| 61 | ProductCartProcessor::SKIP_PRODUCT_RECALCULATION => true, |
||
| 62 | DeliveryProcessor::SKIP_DELIVERY_PRICE_RECALCULATION => true, |
||
| 63 | DeliveryProcessor::SKIP_DELIVERY_TAX_RECALCULATION => true, |
||
| 64 | PromotionCollector::SKIP_PROMOTION => true, |
||
| 65 | ProductCartProcessor::SKIP_PRODUCT_STOCK_VALIDATION => true, |
||
| 66 | ProductCartProcessor::KEEP_INACTIVE_PRODUCT => true, |
||
| 67 | ]; |
||
| 68 | |||
| 69 | protected EntityRepository $customerRepository; |
||
| 70 | |||
| 71 | protected AbstractSalesChannelContextFactory $salesChannelContextFactory; |
||
| 72 | |||
| 73 | protected EventDispatcherInterface $eventDispatcher; |
||
| 74 | |||
| 75 | private NumberRangeValueGeneratorInterface $numberRangeValueGenerator; |
||
| 76 | |||
| 77 | private OrderDefinition $orderDefinition; |
||
| 78 | |||
| 79 | private EntityRepository $orderAddressRepository; |
||
| 80 | |||
| 81 | private InitialStateIdLoader $initialStateIdLoader; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @internal |
||
| 85 | */ |
||
| 86 | public function __construct( |
||
| 87 | EntityRepository $customerRepository, |
||
| 88 | AbstractSalesChannelContextFactory $salesChannelContextFactory, |
||
| 89 | EventDispatcherInterface $eventDispatcher, |
||
| 90 | NumberRangeValueGeneratorInterface $numberRangeValueGenerator, |
||
| 91 | OrderDefinition $orderDefinition, |
||
| 92 | EntityRepository $orderAddressRepository, |
||
| 93 | InitialStateIdLoader $initialStateIdLoader |
||
| 94 | ) { |
||
| 95 | $this->customerRepository = $customerRepository; |
||
| 96 | $this->salesChannelContextFactory = $salesChannelContextFactory; |
||
| 97 | $this->eventDispatcher = $eventDispatcher; |
||
| 98 | $this->numberRangeValueGenerator = $numberRangeValueGenerator; |
||
| 99 | $this->orderDefinition = $orderDefinition; |
||
| 100 | $this->orderAddressRepository = $orderAddressRepository; |
||
| 101 | $this->initialStateIdLoader = $initialStateIdLoader; |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @throws DeliveryWithoutAddressException |
||
| 106 | * |
||
| 107 | * @return array<string, mixed|float|string|array<int, array<string, string|int|bool|mixed>>|null> |
||
| 108 | */ |
||
| 109 | public function convertToOrder(Cart $cart, SalesChannelContext $context, OrderConversionContext $conversionContext): array |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @throws CartException |
||
| 208 | */ |
||
| 209 | public function convertToCart(OrderEntity $order, Context $context): Cart |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @param array<string, array<string, bool>|string> $overrideOptions |
||
| 250 | * |
||
| 251 | * @throws InconsistentCriteriaIdsException |
||
| 252 | */ |
||
| 253 | public function assembleSalesChannelContext(OrderEntity $order, Context $context, array $overrideOptions = []): SalesChannelContext |
||
| 254 | { |
||
| 255 | if ($order->getTransactions() === null) { |
||
| 256 | throw OrderException::missingAssociation('transactions'); |
||
| 257 | } |
||
| 258 | if ($order->getOrderCustomer() === null) { |
||
| 259 | throw OrderException::missingAssociation('orderCustomer'); |
||
| 260 | } |
||
| 261 | |||
| 262 | $customerId = $order->getOrderCustomer()->getCustomerId(); |
||
| 263 | $customerGroupId = null; |
||
| 264 | |||
| 265 | if ($customerId) { |
||
| 266 | /** @var CustomerEntity|null $customer */ |
||
| 267 | $customer = $this->customerRepository->search(new Criteria([$customerId]), $context)->get($customerId); |
||
| 268 | if ($customer !== null) { |
||
| 269 | $customerGroupId = $customer->getGroupId(); |
||
| 270 | } |
||
| 271 | } |
||
| 272 | |||
| 273 | $billingAddressId = $order->getBillingAddressId(); |
||
| 274 | /** @var OrderAddressEntity|null $billingAddress */ |
||
| 275 | $billingAddress = $this->orderAddressRepository->search(new Criteria([$billingAddressId]), $context)->get($billingAddressId); |
||
| 276 | if ($billingAddress === null) { |
||
| 277 | throw new AddressNotFoundException($billingAddressId); |
||
| 278 | } |
||
| 279 | |||
| 280 | $options = [ |
||
| 281 | SalesChannelContextService::CURRENCY_ID => $order->getCurrencyId(), |
||
| 282 | SalesChannelContextService::LANGUAGE_ID => $order->getLanguageId(), |
||
| 283 | SalesChannelContextService::CUSTOMER_ID => $customerId, |
||
| 284 | SalesChannelContextService::COUNTRY_STATE_ID => $billingAddress->getCountryStateId(), |
||
| 285 | SalesChannelContextService::CUSTOMER_GROUP_ID => $customerGroupId, |
||
| 286 | SalesChannelContextService::PERMISSIONS => self::ADMIN_EDIT_ORDER_PERMISSIONS, |
||
| 287 | SalesChannelContextService::VERSION_ID => $context->getVersionId(), |
||
| 288 | ]; |
||
| 289 | |||
| 290 | $delivery = $order->getDeliveries() !== null ? $order->getDeliveries()->first() : null; |
||
| 291 | if ($delivery !== null) { |
||
| 292 | $options[SalesChannelContextService::SHIPPING_METHOD_ID] = $delivery->getShippingMethodId(); |
||
| 293 | } |
||
| 294 | |||
| 295 | //get the first not paid transaction or, if all paid, the last transaction |
||
| 296 | if ($order->getTransactions() !== null) { |
||
| 297 | foreach ($order->getTransactions() as $transaction) { |
||
| 298 | $options[SalesChannelContextService::PAYMENT_METHOD_ID] = $transaction->getPaymentMethodId(); |
||
| 299 | if ( |
||
| 300 | $transaction->getStateMachineState() !== null |
||
| 301 | && $transaction->getStateMachineState()->getTechnicalName() !== OrderTransactionStates::STATE_PAID |
||
| 302 | ) { |
||
| 303 | break; |
||
| 304 | } |
||
| 305 | } |
||
| 306 | } |
||
| 307 | |||
| 308 | $options = array_merge($options, $overrideOptions); |
||
| 309 | |||
| 310 | $salesChannelContext = $this->salesChannelContextFactory->create(Uuid::randomHex(), $order->getSalesChannelId(), $options); |
||
| 311 | $salesChannelContext->getContext()->addExtensions($context->getExtensions()); |
||
| 312 | $salesChannelContext->addState(...$context->getStates()); |
||
| 313 | $salesChannelContext->setTaxState($order->getTaxStatus()); |
||
| 314 | |||
| 315 | if ($context->hasState(Context::SKIP_TRIGGER_FLOW)) { |
||
| 316 | $salesChannelContext->getContext()->addState(Context::SKIP_TRIGGER_FLOW); |
||
| 317 | } |
||
| 318 | |||
| 319 | if ($order->getItemRounding() !== null) { |
||
| 320 | $salesChannelContext->setItemRounding($order->getItemRounding()); |
||
| 321 | } |
||
| 322 | |||
| 323 | if ($order->getTotalRounding() !== null) { |
||
| 324 | $salesChannelContext->setTotalRounding($order->getTotalRounding()); |
||
| 325 | } |
||
| 326 | |||
| 327 | if ($order->getRuleIds() !== null) { |
||
| 328 | $salesChannelContext->setRuleIds($order->getRuleIds()); |
||
| 329 | } |
||
| 330 | |||
| 331 | return $salesChannelContext; |
||
| 332 | } |
||
| 333 | |||
| 334 | private function convertDeliveries(OrderDeliveryCollection $orderDeliveries, LineItemCollection $lineItems): DeliveryCollection |
||
| 402 | } |
||
| 403 | } |
||
| 404 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths