src/Delivery/Controller/ShipmentRoutesController.php line 60

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace App\Delivery\Controller;
  3. use App\Customer\Service\DeliveryAddressService;
  4. use App\Delivery\Service\HolidayService;
  5. use App\Delivery\Service\ShipmentService;
  6. use App\Delivery\Service\VehicleService;
  7. use App\Framework\Controller\APIController;
  8. use App\Framework\Serializer\APISerializerGroup;
  9. use DateTime;
  10. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Symfony\Component\Serializer\Exception\ExceptionInterface;
  13. #[Route(path'/api/shipment_routes')]
  14. class ShipmentRoutesController extends APIController
  15. {
  16.     /**
  17.      *
  18.      * @throws ExceptionInterface
  19.      * @return mixed[]
  20.      */
  21.     #[Route(path''methods: ['GET'])]
  22.     #[APISerializerGroup('vehicle_route:default''vehicle:default''order_shipping:default''streetaddress:default')]
  23.     #[Security("is_granted('ROLE_ADMIN') or is_granted('ROLE_CHOFER') or is_granted('ROLE_HUERTA') or is_granted('ROLE_CALLCENTER')")]
  24.     public function getDeliveryRoutes(ShipmentService $shipmentServiceVehicleService $vehicleService): array
  25.     {
  26.         $dateString $this->getRequestFilter('date'truenull);
  27.         $date = new DateTime($dateString);
  28.         $result $shipmentService->getRoutesByDate($date);
  29.         // Only return owned routes for drivers
  30.         if ($this->isGranted('ROLE_CHOFER')) {
  31.             // Reset our routes
  32.             $vehicle $vehicleService->getVehicleByOwner($this->getUser());
  33.             $routes = [];
  34.             if ($vehicle !== null) {
  35.                 foreach ($result['routes'] as $route) {
  36.                     if ($route->getVehicle() === $vehicle) {
  37.                         $routes[] = $route;
  38.                     }
  39.                 }
  40.             }
  41.             $result['routes'] = $routes;
  42.         }
  43.         return $result;
  44.     }
  45.     /**
  46.      * @throws ExceptionInterface
  47.      *
  48.      * @return array<string, bool>
  49.      */
  50.     #[Route(path'/availability'methods: ['GET'])]
  51.     public function getAvailabilityForDate(
  52.         DeliveryAddressService $deliveryAddressService,
  53.         ShipmentService $shipmentService,
  54.         VehicleService $vehicleService,
  55.         HolidayService $holidayService
  56.     ): array {
  57.         $addressId = (int) $this->getRequestFilter('address'truenull);
  58.         $dateString $this->getRequestFilter('date'truenull);
  59.         $startString $this->getRequestFilter('start'truenull);
  60.         $endString $this->getRequestFilter('end'truenull);
  61.         $featureIds $this->getRequestFilter('features'false, []);
  62.         $date = new DateTime($dateString);
  63.         // check if the date is a holiday
  64.         $isHoliday $holidayService->isHoliday($date);
  65.         if ($isHoliday) {
  66.             return [
  67.                 'available' => false,
  68.             ];
  69.         }
  70.         $address $deliveryAddressService->getAddressById($addressId);
  71.         $start = new DateTime('1970-01-01' $startString);
  72.         $end = new DateTime('1970-01-01' $endString);
  73.         $features array_map(fn ($id) => $vehicleService->getVehicleFeatureById($id), $featureIds);
  74.         $result $shipmentService->isTimeframeAvailable($address$date$start$end$features);
  75.         return [
  76.             'available' => $result,
  77.         ];
  78.     }
  79. }