| Conditions | 3 |
| Paths | 3 |
| Total Lines | 52 |
| Code Lines | 27 |
| 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 |
||
| 66 | public function estimateShippingCostAction(Request $request) |
||
| 67 | { |
||
| 68 | /** @var OrderRepositoryInterface $cartRepository */ |
||
| 69 | $cartRepository = $this->get('sylius.repository.order'); |
||
| 70 | /** @var ViewHandlerInterface $viewHandler */ |
||
| 71 | $viewHandler = $this->get('fos_rest.view_handler'); |
||
| 72 | /** @var ShippingMethodsResolverInterface $shippingMethodResolver */ |
||
| 73 | $shippingMethodResolver = $this->get('sylius.shipping_methods_resolver'); |
||
| 74 | /** @var AddressFactoryInterface $addressFactory */ |
||
| 75 | $addressFactory = $this->get('sylius.factory.address'); |
||
| 76 | /** @var FactoryInterface $shipmentFactory */ |
||
| 77 | $shipmentFactory = $this->get('sylius.factory.shipment'); |
||
| 78 | /** @var ServiceRegistryInterface $calculators */ |
||
| 79 | $calculators = $this->get('sylius.registry.shipping_calculator'); |
||
| 80 | /** @var PriceViewFactoryInterface $priceViewFactory */ |
||
| 81 | $priceViewFactory = $this->get('sylius.shop_api_plugin.factory.price_view_factory'); |
||
| 82 | |||
| 83 | /** @var OrderInterface $cart */ |
||
| 84 | $cart = $cartRepository->findOneBy(['tokenValue' => $request->attributes->get('token')]); |
||
| 85 | |||
| 86 | if (null === $cart) { |
||
| 87 | throw new NotFoundHttpException('Cart with given id does not exists'); |
||
| 88 | } |
||
| 89 | |||
| 90 | /** @var AddressInterface $address */ |
||
| 91 | $address = $addressFactory->createNew(); |
||
| 92 | $address->setCountryCode($request->query->get('countryCode')); |
||
| 93 | $address->setProvinceCode($request->query->get('provinceCode')); |
||
| 94 | $cart->setShippingAddress($address); |
||
| 95 | |||
| 96 | /** @var ShipmentInterface $shipment */ |
||
| 97 | $shipment = $shipmentFactory->createNew(); |
||
| 98 | $shipment->setOrder($cart); |
||
| 99 | |||
| 100 | $shippingMethods = $shippingMethodResolver->getSupportedMethods($shipment); |
||
| 101 | |||
| 102 | if (empty($shippingMethods)) { |
||
| 103 | throw new UnresolvedDefaultShippingMethodException(); |
||
| 104 | } |
||
| 105 | |||
| 106 | $shippingMethod = $shippingMethods[0]; |
||
| 107 | |||
| 108 | $estimatedShippingCostView = new EstimatedShippingCostView(); |
||
| 109 | $calculator = $calculators->get($shippingMethod->getCalculator()); |
||
| 110 | |||
| 111 | $estimatedShippingCostView->price = $priceViewFactory->create( |
||
| 112 | $calculator->calculate($shipment, $shippingMethod->getConfiguration()), |
||
| 113 | $cart->getCurrencyCode() |
||
| 114 | ); |
||
| 115 | |||
| 116 | return $viewHandler->handle(View::create($estimatedShippingCostView, Response::HTTP_OK)); |
||
| 117 | } |
||
| 118 | } |
||
| 119 |