src/Application/MTRoute.php line 72

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: boris
  5.  * Date: 05.01.2020
  6.  * Time: 17:51
  7.  */
  8. namespace App\Application;
  9. use App\Entity\RouteHashTable;
  10. use App\Application\MTHelper;
  11. use App\Application\MTSettings;
  12. class MTRoute
  13. {
  14.     private $params;
  15.     private $theRoute;
  16.     public function __construct($doctrine false$hash_basic '')
  17.     {
  18.         $this->params['from'] = '';
  19.         $this->params['to'] = '';
  20.         $this->params['points'] = [];
  21.         $this->params['speed_rr'] = [];
  22.         $this->params['bring_me_back'] = false;
  23.         $this->theRoute = [];
  24.         // fast initialisation of route object:
  25.         if($doctrine !== false && !empty($hash_basic))
  26.             self::getRoute($doctrine$hash_basic);
  27.     }
  28.     public function getRoute($doctrine$hash_basic '')
  29.     {
  30.         MTHelper::execEvent('GetRouteStart');
  31.         if(
  32.             empty($hash_basic) && ( empty($this->params['from']) || empty($this->params['to']) )
  33.         ){
  34.             //@todo: generate error message
  35.             return false;
  36.         }
  37.         if(empty($hash_basic))
  38.             $hash_basic self::getHashBasic();
  39.         //look up hash in DB
  40.         $repository $doctrine->getRepository(RouteHashTable::class);
  41.         $routeItem $repository->findOneBy([
  42.             'hash_basic' => $hash_basic,
  43.         ]);
  44.         if(!$routeItem){
  45.             MTHelper::execEvent('GetRouteFromApiStart');
  46.             //if there is one, get the route data from other table in DB
  47.             //if there is no route, get it from dispatcher and save response to DB
  48.            $route self::apiGetRouteAutodispatcher();
  49.             //@todo: check if response is allright
  50.             if(isset($route->status)) return $route;
  51.             $routeName $this->params['from']->getName() . " - ";
  52.             $routeName .= $this->params['to']->getName();
  53.             $routeItem = new RouteHashTable();
  54.             $routeItem->setHashBasic($hash_basic)
  55.                 ->setRouteName($routeName)
  56.                 ->setDistanceKm($route['kilometers'])
  57.                 ->setTimeMin($route['minutes'])
  58.                 ->setPolyline($route['polyline'])
  59.                 ->setSegments($route['segments'])
  60.                 ->setQueryUrl($route['query_url']);
  61.             $entityManager $doctrine->getManager();
  62.             $entityManager->persist($routeItem);
  63.             $entityManager->flush();
  64.             MTHelper::execEvent('GetRouteFromApiEnd');
  65.         }
  66.         $this->theRoute = [
  67.             'hash_basic'    => $routeItem->getHashBasic(),
  68.             'distance'      => $routeItem->getDistanceKm(),
  69.             'time'          => $routeItem->getTimeMin(),
  70.             'polyline'      => $routeItem->getPolyline(),
  71.             'segments'      => $routeItem->getSegments(),
  72.             'params_b'      => $this->params,
  73.         ];
  74.         MTHelper::execEvent('GetRouteEnd');
  75.         return $this->theRoute;
  76.     }
  77.     private function apiGetRouteAutodispatcher()
  78.     {
  79.         if(empty($this->params['from']) || empty($this->params['to'])) return false;
  80.         //@todo: make auth from settings, probably DB
  81.         //$my_auth = 'lakbor:sim33cat449';
  82.         $my_auth MTSettings::getSetting('autodispatcher_login') . ":" MTSettings::getSetting('autodispatcher_password');
  83.         //@todo: make normal url assembling, with coords (string methods of MTPlace will return urlencoded string)
  84.         $query_url "https://api.avtodispetcher.ru/v1/route?from=" $this->params['from'] . "&to=" $this->params['to'];
  85.         if(isset($this->params['points']) && !empty($this->params['points'])) $query_url .= "&v=" implode(';'$this->params['points']);
  86.         if(count($this->params['speed_rr']) > 0) {
  87.             $query_url .= self::getSpeedRange(falsetrue);
  88.         } else{
  89.             //@todo make default values pulled from DB and probably not from here but earlier
  90.             $query_url .= self::getSpeedRange(120true);
  91.         }
  92.         $ch curl_init();
  93.         curl_setopt($chCURLOPT_URL$query_url);
  94.         curl_setopt($chCURLOPT_RETURNTRANSFER1);
  95.         curl_setopt($chCURLOPT_HEADER0);
  96.         curl_setopt($chCURLOPT_HTTPAUTHCURLAUTH_BASIC);
  97.         curl_setopt($chCURLOPT_USERPWD$my_auth);
  98.         curl_setopt($chCURLOPT_SSL_VERIFYPEERfalse);
  99.         curl_setopt($chCURLOPT_HTTPHEADER, array(
  100.             "accept: application/json",
  101.         ));
  102.         $output json_decode(curl_exec($ch), true);
  103.         //$info = curl_getinfo($ch);
  104.         curl_close($ch);
  105.         $output['query_url'] = $query_url;
  106.         return $output;
  107.     }
  108.     private function getSpeedRange($speed$inline false)
  109.     {
  110.         //@todo: do smth with these speed blocks, they look too bad
  111.         if($speed == false && $inline == true){
  112.             $range $this->params["speed_rr"];
  113.             return "&rm=" $range["rm"] . "&rp=" $range["rp"] . "&rs=" $range["rs"] . "&ru=" $range["ru"];
  114.         }
  115.         if($speed <= 80){
  116.             $range = [
  117.                 "rm" => 80,
  118.                 "rp" => 70,
  119.                 "rs" => 60,
  120.                 "ru" => 40,
  121.             ];
  122.         } elseif ($speed <= 100){
  123.             $range = [
  124.                 "rm" => 100,
  125.                 "rp" => 80,
  126.                 "rs" => 60,
  127.                 "ru" => 50,
  128.             ];
  129.         } else {
  130.             $range = [
  131.                 "rm" => $speed,
  132.                 "rp" => 110,
  133.                 "rs" => 90,
  134.                 "ru" => 60,
  135.             ];
  136.         }
  137.         if($inline) return "&rm=" $range["rm"] . "&rp=" $range["rp"] . "&rs=" $range["rs"] . "&ru=" $range["ru"];
  138.         return $range;
  139.     }
  140.     public function setParam(string $param$value)
  141.     {
  142.         switch ($param) {
  143.             case 'from':
  144.             case 'to':
  145.                 $this->params[$param] = new MTPlace($value);
  146.                 break;
  147.             case 'point':
  148.                 $this->params['points'][] = new MTPlace($value);
  149.                 break;
  150.             //@todo: add other params
  151.         }
  152.     }
  153.     private function getHashBasic()
  154.     {
  155.         $value_to_hash print_r($this->paramstrue);
  156.         return md5($value_to_hash);
  157.     }
  158.     private function getHashFull()
  159.     {
  160.         //@todo: this full route should be outa here cos its route with separation and so on
  161.     }
  162.     public function getPolyline()
  163.     {
  164.         return $this->theRoute['polyline'];
  165.     }
  166.     public function getTheRoute()
  167.     {
  168.         return $this->theRoute;
  169.     }
  170.     public function getParam($param){
  171.         if(isset($this->params$param ]))
  172.             return $this->params$param ];
  173.         else return 0;
  174.     }
  175. }