<?php
namespace App\Vendor;
use App\Interfaces\HelperInterface;
use App\Interfaces\IHotels;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use Symfony\Component\HttpFoundation\Request;
class Helper implements HelperInterface
{
private LoggerInterface $zstLogger;
public function __construct(LoggerInterface $zstLogger){
$this->zstLogger = $zstLogger;
}
public function logZst($start = false, $end = false, $params = []){
$message = "";
$time = date("Y-m-d H:i:s");
if(empty($params["error"])){
if(!empty($params["dumpType"])){
if(!empty($start)){
$message .= "Start processing hotel ".$params["dumpType"].". Time:".$time;
}
if(!empty($end)){
$message .= "End processing hotel ".$params["dumpType"].". Time:".$time;
}
} else{
$message = "";
}
$this->zstLogger->log(LogLevel::INFO, $message);
} else {
$this->zstLogger->log(LogLevel::ERROR, $params["error"]);
}
}
public static function uuid4($data = null) {
$data = $data ?? random_bytes(16);
assert(strlen($data) == 16);
$data[6] = chr(ord($data[6]) & 0x0f | 0x40);
$data[8] = chr(ord($data[8]) & 0x3f | 0x80);
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}
public static function gen_uuid() {
return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
// 32 bits for "time_low"
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
// 16 bits for "time_mid"
mt_rand( 0, 0xffff ),
// 16 bits for "time_hi_and_version",
// four most significant bits holds version number 4
mt_rand( 0, 0x0fff ) | 0x4000,
// 16 bits, 8 bits for "clk_seq_hi_res",
// 8 bits for "clk_seq_low",
// two most significant bits holds zero and one for variant DCE1.1
mt_rand( 0, 0x3fff ) | 0x8000,
// 48 bits for "node"
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
);
}
public static function dateDiffInDays($date1, $date2)
{
// Calculating the difference in timestamps
$diff = strtotime($date2) - strtotime($date1);
// 1 day = 24 hours
// 24 * 60 * 60 = 86400 seconds
return abs(round($diff / 86400));
}
public static function defaultParams(string $hotelId, array $params, IHotels $hotelService, Request $request){
if(empty($params["search"]["checkin"]) || empty($params["search"]["checkout"])){
$currentDay = date("Y-m-d");
$params["search"]["checkin"] = date("Y-m-d", strtotime($currentDay . ' +1 day'));
$params["search"]["checkout"] = date("Y-m-d", strtotime($currentDay . ' +2 day'));
}
$region = $hotelService->getByHotelId($hotelId, $request->getLocale());
if(!empty($region)){
$regionData["id"] = $region[0]["id"];
$regionData["name"] = $region[0]["name"];
}
$checkIn = empty($params["search"]["checkin"]) ? date("Y-m-d") : $params["search"]["checkin"];
$params["search"]["checkin"] =$checkIn;
$params["search"]["checkout"] = empty($params["search"]["checkout"]) ? date('Y-m-d', strtotime($checkIn . ' +1 day')) : $params["search"]["checkout"];
$hotelService->guestsRoomsDaysCount($params);
// $hotel = $hotelService->hotelPage($hotelId, $request);
return [];
}
public static function defaultSearchParams(){
$params = [
"search" => [
"location" => "2395",
"guests" => [
[
"adults" => "2",
"children" => ["NaN"]
]
],
"currency" => "RUB",
"language" => "ru"
]
];
$currentDay = date("Y-m-d");
$params["search"]["checkin"] = date("Y-m-d", strtotime($currentDay . ' +1 day'));
$params["search"]["checkout"] = date("Y-m-d", strtotime($currentDay . ' +2 day'));
return $params;
}
public static function clearSession(Request $request){
$session = $request->getSession();
if($session->has("guests_data")){
$session->remove("guests_data");
}
if($session->has("all_params")){
$session->remove("all_params");
}
if($session->has("bookingResult")){
$session->remove("bookingResult");
}
if($session->has("rate")){
$session->remove("rate");
}
// if($session->has('list')){
// $session->remove('list');
// }
// if($session->has('hotel')){
// $session->remove('hotel');
// }
if($session->has('back_url')){
$session->remove('back_url');
}
}
public static function interval($seconds){
$startDate = date('Y-m-d H:i:s');
$start = 0;
$k = true;
while ($k){
$endDate = date('Y-m-d H:i:s');
$diff = strtotime($endDate) - strtotime($startDate);
$l = abs(round($diff));
if(intval($l) == $seconds){
$k = false;
}
$start++;
}
}
}