src/Service/SearchService.php line 118

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use App\Interfaces\HotelMongoInterface;
  4. use App\Interfaces\SearchInterface;
  5. use App\Vendor\Symfony\HAbstractService;
  6. use App\Vendor\Symfony\UserException;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Psr\Log\LoggerInterface;
  9. use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
  10. use Symfony\Component\HttpKernel\KernelInterface;
  11. use Symfony\Contracts\Translation\TranslatorInterface;
  12. class SearchService extends HAbstractService implements SearchInterface
  13. {
  14.     private array $guests = [];
  15.     private HotelMongoInterface $hotelMongo;
  16.     private LoggerInterface $zstLogger;
  17.     public function __construct(ContainerBagInterface $bagEntityManagerInterface $emKernelInterface $kernelHotelMongoInterface $hotelMongoTranslatorInterface $translatorLoggerInterface $zstLogger)
  18.     {
  19.         parent::__construct($bag$em$kernel,$translator);
  20.         $this->translator $translator;
  21.         $this->hotelMongo $hotelMongo;
  22.         $this->zstLogger $zstLogger;
  23.     }
  24.     /**
  25.      * @param int $regionId
  26.      * @param string $checkIn
  27.      * @param string $checkOut
  28.      * @param array $gusts
  29.      * @param string $language
  30.      * @param string $currency
  31.      * @param int $hotelsLimit
  32.      * @param string $residency
  33.      * @return array
  34.      */
  35.     public function regionSearch(int $regionIdstring $checkInstring $checkOutstring $languagestring $currency ""int $hotelsLimit 0string $residency ""): array
  36.     {
  37.         $params get_defined_vars();
  38.         $data $this->sendDataself::REGION_SEARCH,$params);
  39.         return $data;
  40.     }
  41.     /**
  42.      * @param float $latitude
  43.      * @param float $longitude
  44.      * @param int $radius
  45.      * @param string $checkIn
  46.      * @param string $checkOut
  47.      * @param array $gusts
  48.      * @param string $language
  49.      * @param string $currency
  50.      * @param string $residency
  51.      * @return array
  52.      */
  53.     public function geoSearch(float $latitudefloat $longitudeint $radiusstring $checkInstring $checkOutstring $languagestring $currency ""string $residency ""): array
  54.     {
  55.         $params get_defined_vars();
  56.         $data $this->generateSearchQueryself::GEO_SEARCH,$params);
  57.         return $data;
  58.     }
  59.     /**
  60.      * @param array $ids
  61.      * @param string $checkIn
  62.      * @param string $checkOut
  63.      * @param array $gusts
  64.      * @param string $language
  65.      * @param string $currency
  66.      * @param string $residency
  67.      * @return array
  68.      */
  69.     public function hotelsSearch(array $idsstring $checkInstring $checkOutstring $languagestring $currency ""string $residency ""): array
  70.     {
  71.         $params get_defined_vars();
  72.         $result $this->sendDataself::HOTELS_SEARCH,$params);
  73.         return $result;
  74.     }
  75.     /**
  76.      * @param string $search
  77.      * @param string $language
  78.      * @return array
  79.      */
  80.     public function hotelSearchAutocomplete(string $searchstring $language): array
  81.     {
  82.         return [];
  83.     }
  84.     /**
  85.      * @param string $id
  86.      * @param string $checkIn
  87.      * @param string $checkOut
  88.      * @param array $gusts
  89.      * @param string $language
  90.      * @param string $currency
  91.      * @param string $residency
  92.      * @return array
  93.      */
  94.     public function hotelPage(string $idstring $checkInstring $checkOutstring $languagestring $currency ""string $residency ""): array
  95.     {
  96.         return [];
  97.     }
  98.     /**
  99.      * @param array $rooms
  100.      * @throws UserException
  101.      * @throws \Psr\Container\ContainerExceptionInterface
  102.      * @throws \Psr\Container\NotFoundExceptionInterface
  103.      */
  104.     public function setRooms(array $rooms){
  105.         $children = [];
  106.         if(empty($rooms)){
  107.             throw new \Exception("rooms is required");
  108.         }
  109.         if(count($rooms) > (int)$this->bag->get('rooms_count')){
  110.             throw new UserException($this->translator->trans('roomsCount', ['{number}' => $this->bag->get('rooms_count')]));
  111.         }
  112.         foreach ($rooms as $room){
  113.             if(!empty($room["children"])){
  114.                 $children explode(','$room["children"][0]);
  115.                 foreach ($children as $key=>&$c){
  116.                     if($c == 'NaN'){
  117.                         unset($children[$key]);
  118.                         continue;
  119.                     }
  120.                     $children[$key] = intval($c);
  121.                 }
  122.             }
  123.             $roomChildrenCount count($children);
  124.             if($roomChildrenCount > (int)$this->bag->get('children_count')){
  125.                 throw new UserException($this->translator->trans('childrenCount', ['{number}' => $this->bag->get('children_count')]));
  126.             }
  127.             foreach ($children as $child){
  128.                 if((int)$child > (int)$this->bag->get('children_age')){
  129.                     throw new UserException($this->translator->trans('childrenAge', ['{year}' => $this->bag->get('children_age')]));
  130.                 }
  131.             }
  132.             $roomGuests = [
  133.                 "adults" => empty($room["adults"]) ? :(int)$room["adults"],
  134.                 "children" => $children
  135.             ];
  136.             $this->guests[] = $roomGuests;
  137.         }
  138.     }
  139.     public function generateSearchQuery($type$params = []){
  140.         if(!empty($params["checkIn"])){
  141.             $params["checkin"] = $params["checkIn"];
  142.         }
  143.         if(!empty($params["checkOut"])){
  144.             $params["checkout"] = $params["checkOut"];
  145.         }
  146.         $currentDay date("Y-m-d");
  147.         $checkIn = empty($params["checkin"]) ? date("Y-m-d"strtotime($currentDay ' +1 day')) : $params["checkin"];
  148.         $checkOut = empty($params["checkout"]) ? date('Y-m-d'strtotime($checkIn ' +1 day')) : $params["checkout"];
  149.         $language = empty($params["language"]) ? $this->request->getLocale() : $params["language"];
  150.         if(empty($this->guests)){
  151.             $this->guests = [
  152.                 [
  153.                     "adults" => empty($room["adults"]) ? :(int)$room["adults"],
  154.                     "children" => !empty($room["children"]) ? $room["children"] : []
  155.                 ]
  156.             ];
  157.         }
  158.         $query = [
  159.             "checkin" => $checkIn,
  160.             "checkout" => $checkOut,
  161.             "language" => $language,
  162.             "guests" => $this->guests,
  163.             "residency"=> $params["residency"],
  164.         ];
  165.         if($type == self::REGION_SEARCH){
  166.             $query["region_id"] = $params["regionId"];
  167.         }
  168.         if($type == self::GEO_SEARCH){
  169.             $query["latitude"] = $params["latitude"];
  170.             $query["longitude"] = $params["longitude"];
  171.             $query["radius"] = $params["radius"];
  172.         }
  173.         if($type == self::HOTELS_SEARCH){
  174.             $query["ids"] = $params["ids"];
  175.         }
  176.         if($type == self::HOTEL_PAGE){
  177.             $query['id'] = trim($params["hotelId"]);
  178.         }
  179.         $query["currency"] = empty($params["currency"]) ? "RUB" $params["currency"];
  180.         if(!empty($hotelsLimit)){
  181.             $query["hotels_limit"] = $hotelsLimit;
  182.         }
  183.         return json_encode($query);
  184.     }
  185.     public function searchAutocomplete($params){
  186.         $search = [
  187.             "query" => $params["search"],
  188.             "language" => $params["lang"],
  189.         ];
  190.         $postData json_encode($search);
  191.         $curl curl_init();
  192.         curl_setopt_array($curl, array(
  193.             CURLOPT_URL => 'https://api.worldota.net/api/b2b/v3/search/multicomplete/',
  194.             CURLOPT_USERPWD => $this->bag->get('zstd_user').":".$this->bag->get('zstd_pass'),
  195.             CURLOPT_RETURNTRANSFER => true,
  196.             CURLOPT_ENCODING => '',
  197.             CURLOPT_MAXREDIRS => 10,
  198.             CURLOPT_TIMEOUT => 0,
  199.             CURLOPT_FOLLOWLOCATION => true,
  200.             CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  201.             CURLOPT_CUSTOMREQUEST => 'POST',
  202.             CURLOPT_POSTFIELDS =>$postData,
  203.             CURLOPT_HTTPHEADER => array(
  204.                 'Content-Type: application/json'
  205.             ),
  206.         ));
  207.         $response curl_exec($curl);
  208.         $error curl_error($curl);
  209.         $data json_decode($responsetrue);
  210.         curl_close($curl);
  211.         if($data["status"] == "ok" && empty($data["error"])){
  212.             return $data["data"];
  213.         } else {
  214.             return $data["error"];
  215.         }
  216.     }
  217.     private function sendData($type$params){
  218.         $jsonParams $this->generateSearchQuery($type$params);
  219.         $curl curl_init();
  220.         curl_setopt_array($curl, array(
  221.             CURLOPT_URL => 'https://api.worldota.net/api/b2b/v3/search/serp/region/',
  222.             CURLOPT_USERPWD => $this->bag->get('zstd_user').":".$this->bag->get('zstd_pass'),
  223.             CURLOPT_RETURNTRANSFER => true,
  224.             CURLOPT_ENCODING => '',
  225.             CURLOPT_MAXREDIRS => 10,
  226.             CURLOPT_TIMEOUT => 0,
  227.             CURLOPT_FOLLOWLOCATION => true,
  228.             CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  229.             CURLOPT_CUSTOMREQUEST => 'POST',
  230.             CURLOPT_POSTFIELDS =>$jsonParams,
  231.             CURLOPT_HTTPHEADER => array(
  232.                 'Content-Type: application/json'
  233.             ),
  234.         ));
  235.         $response curl_exec($curl);
  236.         curl_close($curl);
  237.         $respArray json_decode($responsetrue);
  238.         if(empty($respArray["data"])){
  239.             return [];
  240.         }
  241.         foreach ($respArray["data"]["hotels"] as $ind=>$datum){
  242.             foreach ($datum["rates"] as $rIndex =>$rate){
  243.                 if($rate["allotment"] == 0){
  244.                     unset($datum["rates"][$rIndex]);
  245.                 }
  246.             }
  247.             if(count($datum["rates"]) < 1){
  248.                 unset($datum["hotels"][$ind]);
  249.             }
  250.         }
  251.         $resp json_encode($respArray["data"]["hotels"]);
  252.         $session $this->request->getSession();
  253.         if($session->has("search_result")){
  254.             $session->remove("search_result");
  255.         }
  256.         $session->set("search_result"$resp);
  257.         return $this->getIds();
  258.     }
  259.     private function getIds(){
  260.         $session $this->request->getSession();
  261.         $regions json_decode($session->get("search_result"), true);
  262.         $hotels = [];
  263.         $ids = [];
  264.         foreach ($regions as $region){
  265.             if(isset($region["rates"]) && !empty($region["rates"])){
  266.                 $ids[] = $region["id"];
  267.                 $hotels[$region["id"]] = $region;
  268.             }
  269.         }
  270.         return ["ids" => $ids"hotels" => $hotels];
  271.     }
  272. }