Service/ShippingMethodHandler.php
public function calculateForOrder(BaseOrder $order = null)
    {
        if (!$order) {
            $order = $this->orderSessionHandler->getOrderFromSession();
        }

        if (!$order->getShipments()) {
            throw new ShipmentsNotCalculatedException();
        }

        $shipmentMethods = [];
        if (!$order->getShippingAddress()) {
            throw new \InvalidArgumentException('shipmentMethod.error.no_shipping_address');
        }

        $country = $order->getShippingAddress()->getCity()->getCountry();

        foreach ($order->getShipments() as $shipment) {
            $this->getAvailableShipmentMethods($shipment, $country);

            $shipmentMethods = [];

            if (count($this->shipmentMethods) < 1) {
                throw new \Exception('shipmentMethod.error.no_method_found');
            } elseif (count($this->shipmentMethods) === 1) {
                if (array_key_exists($this->shipmentMethods[0]->getId(), $shipmentMethods)) {
                    $shipmentMethods[$shipmentMethods[0]->getId()]['total'] += $shipmentMethods[0]->getPrice();
                } else {
                    $shipmentMethods[$this->shipmentMethods[0]->getId()] = [
                        'shipment' => $this->shipmentMethods[0],
                        'total'    => $this->shipmentMethods[0]->getPrice()
                    ];
                }
            } else {
                foreach ($this->shipmentMethods as $shipmentMethod) {
                    if (array_key_exists($shipmentMethod->getId(), $shipmentMethods)) {
                        $shipmentMethods[$shipmentMethod->getId()]['total'] += $shipmentMethod->getPrice();
                    } else {
                        $shipmentMethods[$shipmentMethod->getId()] = [
                            'shipment' => $shipmentMethod,
                            'total'    => $shipmentMethod->getPrice()
                        ];
                    }
                }
            }
        }

        $toDisplay = [];
        $shipments = count($order->getShipments());

        foreach ($shipmentMethods as $shipmentMethod) {
            if ($shipments === 1) {
                $toDisplay[$shipmentMethod['shipment']->getNameWithPrice()] = $shipmentMethod['shipment']->getId();
            } else {
                $toDisplay[$shipmentMethod['shipment']->getNameWithPrice() . ' par envoi'] = $shipmentMethod['shipment']->getId();
            }
        }

        return $toDisplay;
    }
public function calculateForOrder(BaseOrder $order = null)
    {
        if (!$order) {
            $order = $this->orderSessionHandler->getOrderFromSession();
        }

        if (!$order->getShipments()) {
            throw new ShipmentsNotCalculatedException();
        }

        $shipmentMethods = [];
        if (!$order->getShippingAddress()) {
            throw new \InvalidArgumentException('shipmentMethod.error.no_shipping_address');
        }

        $country = $order->getShippingAddress()->getCity()->getCountry();

        foreach ($order->getShipments() as $shipment) {
            $this->getAvailableShipmentMethods($shipment, $country);

            $shipmentMethods = [];

            if (count($this->shipmentMethods) < 1) {
                throw new \Exception('shipmentMethod.error.no_method_found');
            } elseif (count($this->shipmentMethods) === 1) {
                if (array_key_exists($this->shipmentMethods[0]->getId(), $shipmentMethods)) {
                    $shipmentMethods[$shipmentMethods[0]->getId()]['total'] += $shipmentMethods[0]->getPrice();
                } else {
                    $shipmentMethods[$this->shipmentMethods[0]->getId()] = [
                        'shipment' => $this->shipmentMethods[0],
                        'total'    => $this->shipmentMethods[0]->getPrice()
                    ];
                }
            } else {
                foreach ($this->shipmentMethods as $shipmentMethod) {
                    if (array_key_exists($shipmentMethod->getId(), $shipmentMethods)) {
                        $shipmentMethods[$shipmentMethod->getId()]['total'] += $shipmentMethod->getPrice();
                    } else {
                        $shipmentMethods[$shipmentMethod->getId()] = [
                            'shipment' => $shipmentMethod,
                            'total'    => $shipmentMethod->getPrice()
                        ];
                    }
                }
            }
        }

        $toDisplay = [];
        $shipments = count($order->getShipments());

        foreach ($shipmentMethods as $shipmentMethod) {
            if ($shipments === 1) {
                $toDisplay[$shipmentMethod['shipment']->getNameWithPrice()] = $shipmentMethod['shipment']->getId();
            } else {
                $toDisplay[$shipmentMethod['shipment']->getNameWithPrice() . ' par envoi'] = $shipmentMethod['shipment']->getId();
            }
        }

        return $toDisplay;
    }
@param BaseOrder|null $order

@return array
@throws \Exception