<?php
namespace App\Controller\Api\Reservation;
use App\Enum\Parameter;
use App\Service\Reservation\Flight\FlightService;
use App\Service\Reservation\Flight\Paramerter\Save;
use App\Service\Reservation\Forwarding\Flight\Puzzle\PuzzleFlightAvailabilityModel;
use App\Service\Respy\RespyService;
use App\Service\Validator\ValidatorService;
use DateTime;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Class ReservationFlightController
* @package App\Controller\Admin
*/
class ReservationFlightController extends AbstractController
{
/**
* @Route("/api/flight/available", name="api_flight_available", methods={"POST","GET"})
*
* @param RespyService $respy
* @param FlightService $flightService
* @param Request $request
* @return JsonResponse
*/
public function available(RespyService $respy, FlightService $flightService, Request $request): JsonResponse
{
try {
$convertToDate = function ($dateList) {
$return = [];
if (empty($dateList)) {
return $return;
}
foreach (explode(',', $dateList) as $date) {
$return[] = new DateTime($date);
}
return $return;
};
$calculateAge = function ($birthDate) {
$now = new DateTime();
$age = $now->diff($birthDate)->y;
return $age;
};
$data = json_decode($request->getContent(), true);
$request = Request::createFromGlobals();
$marketCode = $request->headers->get("Marketcode", "SMK_001");
if (getenv('WEB_SITE') == Parameter::TD) {
$marketCode = "SMK_001";
}
if (getenv('WEB_SITE') == Parameter::DEROOMS) {
$marketCode = "SMK_93d7b8d3e86454";
}
if (getenv('WEB_BRANCH_SITE') == Parameter::PASSENGERBOOKING) {
$marketCode = "SMK_01449e75fe090a";
}
$adtList = $convertToDate($data["adt"]);
$chdList = $convertToDate($data["chd"]);
foreach ($chdList as $index => $child) {
if ($calculateAge($child) > 12) {
$adtList[] = $child;
unset($chdList[$index]);
}
}
$available = (new PuzzleFlightAvailabilityModel())
->setOrigin($data["origin"])
->setDestination($data["destination"])
->setDate(new DateTime($data['date']))
->setAdt($adtList)
->setChd($chdList)
->setMarket($marketCode);
if (!empty($data['returnDate'])) {
$available->setReturndate(new DateTime($data['returnDate']));
}
if (!empty($data["returnDestination"])) {
$available->setReturndestination($data["returnDestination"]);
}
if ($response = $flightService->available($available)) {
$respy->success();
$respy->setData($response);
}
} catch (\Exception $exception) {
$respy->setError($exception->getMessage());
$respy->setErrorCode($exception->getCode());
}
return new JsonResponse($respy->getResponse());
}
/**
* @Route("/api/reservation/flight/save", name="api_reservation_flight_save", methods={"POST"})
*
* @param RespyService $respy
* @param ValidatorService $validatorService
* @param FlightService $flightService
* @param Request $request
* @return JsonResponse
*/
public function save(RespyService $respy, ValidatorService $validatorService, FlightService $flightService, Request $request): JsonResponse
{
$constraints = new Assert\Collection([
'guid' => [
new Assert\Required()
],
'hashCode' => [
new Assert\NotBlank()
],
'priceCode' => [
new Assert\NotBlank()
],
'returnPriceCode' => [
new Assert\Required()
],
]);
try {
$input = $validatorService->validate($constraints);
$save = (new Save())
->setGuid($input["guid"])
->setHashCode($input["hashCode"])
->setPriceCode($input["priceCode"])
->setMarketCode($request->headers->get('marketCode'))
->setReturnPiceCode($input["returnPriceCode"]);
if ($response = $flightService->save($save)) {
$respy->success();
$respy->setData($response);
}
} catch (\Exception $exception) {
$respy->setError($exception->getMessage());
$respy->setErrorCode($exception->getCode());
}
return new JsonResponse($respy->getResponse());
}
}