<?php
/**
* Created by PhpStorm.
* User: boris
* Date: 05.01.2020
* Time: 17:51
*/
namespace App\Application;
use App\Entity\RouteHashTable;
use App\Application\MTHelper;
use App\Application\MTSettings;
class MTRoute
{
private $params;
private $theRoute;
public function __construct($doctrine = false, $hash_basic = '')
{
$this->params['from'] = '';
$this->params['to'] = '';
$this->params['points'] = [];
$this->params['speed_rr'] = [];
$this->params['bring_me_back'] = false;
$this->theRoute = [];
// fast initialisation of route object:
if($doctrine !== false && !empty($hash_basic))
self::getRoute($doctrine, $hash_basic);
}
public function getRoute($doctrine, $hash_basic = '')
{
MTHelper::execEvent('GetRouteStart');
if(
empty($hash_basic) && ( empty($this->params['from']) || empty($this->params['to']) )
){
//@todo: generate error message
return false;
}
if(empty($hash_basic))
$hash_basic = self::getHashBasic();
//look up hash in DB
$repository = $doctrine->getRepository(RouteHashTable::class);
$routeItem = $repository->findOneBy([
'hash_basic' => $hash_basic,
]);
if(!$routeItem){
MTHelper::execEvent('GetRouteFromApiStart');
//if there is one, get the route data from other table in DB
//if there is no route, get it from dispatcher and save response to DB
$route = self::apiGetRouteAutodispatcher();
//@todo: check if response is allright
if(isset($route->status)) return $route;
$routeName = $this->params['from']->getName() . " - ";
$routeName .= $this->params['to']->getName();
$routeItem = new RouteHashTable();
$routeItem->setHashBasic($hash_basic)
->setRouteName($routeName)
->setDistanceKm($route['kilometers'])
->setTimeMin($route['minutes'])
->setPolyline($route['polyline'])
->setSegments($route['segments'])
->setQueryUrl($route['query_url']);
$entityManager = $doctrine->getManager();
$entityManager->persist($routeItem);
$entityManager->flush();
MTHelper::execEvent('GetRouteFromApiEnd');
}
$this->theRoute = [
'hash_basic' => $routeItem->getHashBasic(),
'distance' => $routeItem->getDistanceKm(),
'time' => $routeItem->getTimeMin(),
'polyline' => $routeItem->getPolyline(),
'segments' => $routeItem->getSegments(),
'params_b' => $this->params,
];
MTHelper::execEvent('GetRouteEnd');
return $this->theRoute;
}
private function apiGetRouteAutodispatcher()
{
if(empty($this->params['from']) || empty($this->params['to'])) return false;
//@todo: make auth from settings, probably DB
//$my_auth = 'lakbor:sim33cat449';
$my_auth = MTSettings::getSetting('autodispatcher_login') . ":" . MTSettings::getSetting('autodispatcher_password');
//@todo: make normal url assembling, with coords (string methods of MTPlace will return urlencoded string)
$query_url = "https://api.avtodispetcher.ru/v1/route?from=" . $this->params['from'] . "&to=" . $this->params['to'];
if(isset($this->params['points']) && !empty($this->params['points'])) $query_url .= "&v=" . implode(';', $this->params['points']);
if(count($this->params['speed_rr']) > 0) {
$query_url .= self::getSpeedRange(false, true);
} else{
//@todo make default values pulled from DB and probably not from here but earlier
$query_url .= self::getSpeedRange(120, true);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $query_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $my_auth);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"accept: application/json",
));
$output = json_decode(curl_exec($ch), true);
//$info = curl_getinfo($ch);
curl_close($ch);
$output['query_url'] = $query_url;
return $output;
}
private function getSpeedRange($speed, $inline = false)
{
//@todo: do smth with these speed blocks, they look too bad
if($speed == false && $inline == true){
$range = $this->params["speed_rr"];
return "&rm=" . $range["rm"] . "&rp=" . $range["rp"] . "&rs=" . $range["rs"] . "&ru=" . $range["ru"];
}
if($speed <= 80){
$range = [
"rm" => 80,
"rp" => 70,
"rs" => 60,
"ru" => 40,
];
} elseif ($speed <= 100){
$range = [
"rm" => 100,
"rp" => 80,
"rs" => 60,
"ru" => 50,
];
} else {
$range = [
"rm" => $speed,
"rp" => 110,
"rs" => 90,
"ru" => 60,
];
}
if($inline) return "&rm=" . $range["rm"] . "&rp=" . $range["rp"] . "&rs=" . $range["rs"] . "&ru=" . $range["ru"];
return $range;
}
public function setParam(string $param, $value)
{
switch ($param) {
case 'from':
case 'to':
$this->params[$param] = new MTPlace($value);
break;
case 'point':
$this->params['points'][] = new MTPlace($value);
break;
//@todo: add other params
}
}
private function getHashBasic()
{
$value_to_hash = print_r($this->params, true);
return md5($value_to_hash);
}
private function getHashFull()
{
//@todo: this full route should be outa here cos its route with separation and so on
}
public function getPolyline()
{
return $this->theRoute['polyline'];
}
public function getTheRoute()
{
return $this->theRoute;
}
public function getParam($param){
if(isset($this->params[ $param ]))
return $this->params[ $param ];
else return 0;
}
}