src/Controller/Api/Reservation/ReservationFlightController.php line 32

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Api\Reservation;
  3. use App\Enum\Parameter;
  4. use App\Service\Reservation\Flight\FlightService;
  5. use App\Service\Reservation\Flight\Paramerter\Save;
  6. use App\Service\Reservation\Forwarding\Flight\Puzzle\PuzzleFlightAvailabilityModel;
  7. use App\Service\Respy\RespyService;
  8. use App\Service\Validator\ValidatorService;
  9. use DateTime;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\JsonResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use Symfony\Component\Validator\Constraints as Assert;
  15. /**
  16.  * Class ReservationFlightController
  17.  * @package App\Controller\Admin
  18.  */
  19. class ReservationFlightController extends AbstractController
  20. {
  21.     /**
  22.      * @Route("/api/flight/available", name="api_flight_available", methods={"POST","GET"})
  23.      *
  24.      * @param RespyService $respy
  25.      * @param FlightService $flightService
  26.      * @param Request $request
  27.      * @return JsonResponse
  28.      */
  29.     public function available(RespyService $respyFlightService $flightServiceRequest $request): JsonResponse
  30.     {
  31.         try {
  32.             $convertToDate = function ($dateList) {
  33.                 $return = [];
  34.                 if (empty($dateList)) {
  35.                     return $return;
  36.                 }
  37.                 foreach (explode(','$dateList) as $date) {
  38.                     $return[] = new DateTime($date);
  39.                 }
  40.                 return $return;
  41.             };
  42.             $calculateAge = function ($birthDate) {
  43.                 $now = new DateTime();
  44.                 $age $now->diff($birthDate)->y;
  45.                 return $age;
  46.             };
  47.             $data json_decode($request->getContent(), true);
  48.             $request Request::createFromGlobals();
  49.             $marketCode $request->headers->get("Marketcode""SMK_001");
  50.             if (getenv('WEB_SITE') == Parameter::TD) {
  51.                 $marketCode "SMK_001";
  52.             }
  53.             if (getenv('WEB_SITE') == Parameter::DEROOMS) {
  54.                 $marketCode "SMK_93d7b8d3e86454";
  55.             }
  56.             if (getenv('WEB_BRANCH_SITE') == Parameter::PASSENGERBOOKING) {
  57.                 $marketCode "SMK_01449e75fe090a";
  58.             }
  59.             $adtList $convertToDate($data["adt"]);
  60.             $chdList $convertToDate($data["chd"]);
  61.             foreach ($chdList as $index => $child) {
  62.                 if ($calculateAge($child) > 12) {
  63.                     $adtList[] = $child;
  64.                     unset($chdList[$index]);
  65.                 }
  66.             }
  67.             $available = (new PuzzleFlightAvailabilityModel())
  68.                 ->setOrigin($data["origin"])
  69.                 ->setDestination($data["destination"])
  70.                 ->setDate(new DateTime($data['date']))
  71.                 ->setAdt($adtList)
  72.                 ->setChd($chdList)
  73.                 ->setMarket($marketCode);
  74.             if (!empty($data['returnDate'])) {
  75.                 $available->setReturndate(new DateTime($data['returnDate']));
  76.             }
  77.             if (!empty($data["returnDestination"])) {
  78.                 $available->setReturndestination($data["returnDestination"]);
  79.             }
  80.             if ($response $flightService->available($available)) {
  81.                 $respy->success();
  82.                 $respy->setData($response);
  83.             }
  84.         } catch (\Exception $exception) {
  85.             $respy->setError($exception->getMessage());
  86.             $respy->setErrorCode($exception->getCode());
  87.         }
  88.         return new JsonResponse($respy->getResponse());
  89.     }
  90.     /**
  91.      * @Route("/api/reservation/flight/save", name="api_reservation_flight_save", methods={"POST"})
  92.      *
  93.      * @param RespyService $respy
  94.      * @param ValidatorService $validatorService
  95.      * @param FlightService $flightService
  96.      * @param Request $request
  97.      * @return JsonResponse
  98.      */
  99.     public function save(RespyService $respyValidatorService $validatorServiceFlightService $flightServiceRequest $request): JsonResponse
  100.     {
  101.         $constraints = new Assert\Collection([
  102.             'guid' => [
  103.                 new Assert\Required()
  104.             ],
  105.             'hashCode' => [
  106.                 new Assert\NotBlank()
  107.             ],
  108.             'priceCode' => [
  109.                 new Assert\NotBlank()
  110.             ],
  111.             'returnPriceCode' => [
  112.                 new Assert\Required()
  113.             ],
  114.         ]);
  115.         try {
  116.             $input $validatorService->validate($constraints);
  117.             $save = (new Save())
  118.                 ->setGuid($input["guid"])
  119.                 ->setHashCode($input["hashCode"])
  120.                 ->setPriceCode($input["priceCode"])
  121.                 ->setMarketCode($request->headers->get('marketCode'))
  122.                 ->setReturnPiceCode($input["returnPriceCode"]);
  123.             if ($response $flightService->save($save)) {
  124.                 $respy->success();
  125.                 $respy->setData($response);
  126.             }
  127.         } catch (\Exception $exception) {
  128.             $respy->setError($exception->getMessage());
  129.             $respy->setErrorCode($exception->getCode());
  130.         }
  131.         return new JsonResponse($respy->getResponse());
  132.     }
  133. }