| Conditions | 8 |
| Paths | 96 |
| Total Lines | 74 |
| Code Lines | 45 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 61 | public function create(OrderInterface $order): OrderView |
||
| 62 | { |
||
| 63 | $channel = $order->getChannel(); |
||
| 64 | Assert::notNull($channel); |
||
| 65 | |||
| 66 | $locale = $channel->getDefaultLocale(); |
||
|
|
|||
| 67 | Assert::notNull($locale); |
||
| 68 | |||
| 69 | $localeCode = $locale->getCode(); |
||
| 70 | Assert::notNull($localeCode); |
||
| 71 | |||
| 72 | $checkoutCompletedAt = $order->getCheckoutCompletedAt(); |
||
| 73 | Assert::notNull($checkoutCompletedAt); |
||
| 74 | |||
| 75 | /** @var CustomerInterface|null $customer */ |
||
| 76 | $customer = $order->getCustomer(); |
||
| 77 | Assert::notNull($customer); |
||
| 78 | |||
| 79 | /** @var OrderView $orderView */ |
||
| 80 | $orderView = new $this->orderViewClass(); |
||
| 81 | $orderView->id = $order->getId(); |
||
| 82 | $orderView->number = $order->getNumber(); |
||
| 83 | $orderView->channel = $channel->getCode(); |
||
| 84 | $orderView->currencyCode = $order->getCurrencyCode(); |
||
| 85 | $orderView->localeCode = $localeCode; |
||
| 86 | $orderView->state = $order->getState(); |
||
| 87 | $orderView->checkoutState = $order->getCheckoutState(); |
||
| 88 | $orderView->checkoutCompletedAt = $checkoutCompletedAt->format('c'); |
||
| 89 | $orderView->shippingState = $order->getShippingState(); |
||
| 90 | $orderView->paymentState = $order->getPaymentState(); |
||
| 91 | |||
| 92 | /** @var OrderItemInterface $item */ |
||
| 93 | foreach ($order->getItems() as $item) { |
||
| 94 | $orderView->items[] = $this->itemFactory->create( |
||
| 95 | $item, |
||
| 96 | $localeCode |
||
| 97 | ); |
||
| 98 | } |
||
| 99 | |||
| 100 | /** @var ShipmentInterface $shipment */ |
||
| 101 | foreach ($order->getShipments() as $shipment) { |
||
| 102 | $orderView->shipments[] = $this->shipmentViewFactory->create($shipment, $localeCode); |
||
| 103 | } |
||
| 104 | |||
| 105 | /** @var PaymentInterface $payment */ |
||
| 106 | foreach ($order->getPayments() as $payment) { |
||
| 107 | $orderView->payments[] = $this->paymentViewFactory->create($payment, $localeCode); |
||
| 108 | } |
||
| 109 | |||
| 110 | if (null !== $order->getShippingAddress()) { |
||
| 111 | $orderView->shippingAddress = $this->addressViewFactory->create($order->getShippingAddress()); |
||
| 112 | } |
||
| 113 | |||
| 114 | if (null !== $order->getBillingAddress()) { |
||
| 115 | $orderView->billingAddress = $this->addressViewFactory->create($order->getBillingAddress()); |
||
| 116 | } |
||
| 117 | |||
| 118 | $adjustments = []; |
||
| 119 | /** @var AdjustmentInterface $adjustment */ |
||
| 120 | foreach ($order->getAdjustmentsRecursively() as $adjustment) { |
||
| 121 | $originCode = $adjustment->getOriginCode(); |
||
| 122 | $additionalAmount = isset($adjustments[$originCode]) ? $adjustments[$originCode]->amount : 0; |
||
| 123 | |||
| 124 | $adjustments[$originCode] = $this->adjustmentViewFactory->create( |
||
| 125 | $adjustment, |
||
| 126 | $additionalAmount |
||
| 127 | ); |
||
| 128 | } |
||
| 129 | $orderView->adjustments = $adjustments; |
||
| 130 | $orderView->adjustmentsTotal = $order->getAdjustmentsTotalRecursively(); |
||
| 131 | $orderView->total = $order->getTotal(); |
||
| 132 | $orderView->customer = $this->customerViewFactory->create($customer); |
||
| 133 | |||
| 134 | return $orderView; |
||
| 135 | } |
||
| 137 |