/**
  * @param RouteCandidateDto $aRouteCandidate
  * @return Itinerary
  */
 public function toItinerary(RouteCandidateDto $aRouteCandidate)
 {
     $legs = array();
     foreach ($aRouteCandidate->getLegs() as $legDto) {
         $legs[] = new Leg($legDto->getLoadLocation(), $legDto->getUnloadLocation(), \DateTimeImmutable::createFromFormat(\DateTime::ATOM, $legDto->getLoadTime()), \DateTimeImmutable::createFromFormat(\DateTime::ATOM, $legDto->getUnloadTime()));
     }
     return new Itinerary($legs);
 }
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
 {
     if (null === ($trackingId = $request->getAttribute('trackingId'))) {
         return new EmptyResponse(404);
     }
     $cargoData = $request->getParsedBody();
     if (!isset($cargoData['legs']) || !is_array($cargoData['legs'])) {
         throw new \InvalidArgumentException("Missing legs for cargo");
     }
     $routeCandidate = new RouteCandidateDto();
     $routeCandidate->setLegs($this->toLegDtosFromData($cargoData['legs']));
     try {
         $this->bookingService->assignCargoToRoute($trackingId, $routeCandidate);
         $cargoRoutingDto = $this->bookingService->loadCargoForRouting($trackingId);
         return new JsonResponse($cargoRoutingDto->getArrayCopy());
     } catch (CargoNotFoundException $e) {
         return new EmptyResponse(404);
     }
 }