/**
  * @param ResourceEvent $e
  * @return CargoRoutingDto
  * @throws \PhlyRestfully\Exception\CreationException
  */
 public function onCreate(ResourceEvent $e)
 {
     $data = $e->getParam('data');
     $this->cargoFrom->setData((array) $data);
     if (!$this->cargoFrom->isValid()) {
         $creationException = new CreationException("Provided Cargo data is invalid.");
         $creationException->setAdditionalDetails(array('errors' => $this->cargoFrom->getMessages()));
         throw $creationException;
     }
     $trackingId = $this->bookingService->bookNewCargo($this->cargoFrom->get('origin')->getValue(), $this->cargoFrom->get('final_destination')->getValue());
     $cargoRouting = new CargoRoutingDto();
     $cargoRouting->setTrackingId($trackingId);
     $cargoRouting->setOrigin($this->cargoFrom->get('origin')->getValue());
     $cargoRouting->setFinalDestination($this->cargoFrom->get('final_destination')->getValue());
     return $cargoRouting;
 }
 /**
  * @param Cargo $aCargo
  * @return CargoRoutingDto
  */
 public function toDto(Cargo $aCargo)
 {
     $cargoRoutingDto = new CargoRoutingDto();
     $cargoRoutingDto->setTrackingId($aCargo->trackingId()->toString());
     $cargoRoutingDto->setOrigin($aCargo->origin());
     $cargoRoutingDto->setFinalDestination($aCargo->routeSpecification()->destination());
     foreach ($aCargo->itinerary()->legs() as $leg) {
         $legDto = new LegDto();
         $legDto->setLoadLocation($leg->loadLocation());
         $legDto->setUnloadLocation($leg->unloadLocation());
         $legDto->setLoadTime($leg->loadTime()->format(\DateTime::ISO8601));
         $legDto->setUnloadTime($leg->unloadTime()->format(\DateTime::ISO8601));
         $cargoRoutingDto->addLeg($legDto);
     }
     return $cargoRoutingDto;
 }