Guest User

Untitled

a guest
Jul 13th, 2020
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.07 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Helpers\Map;
  4.  
  5. use App\Helpers\CarHelper;
  6. use App\Models\Car\Car;
  7. use App\Models\Car\CarRoute;
  8. use App\Services\Map\Points\CreatePoint;
  9. use App\Services\Map\Routes\CreateRoute;
  10. use Illuminate\Http\Request;
  11.  
  12. class RouteGenerator
  13. {
  14.     protected array  $data = [];
  15.     protected object $routeActions;
  16.  
  17.     private CarRoute $route;
  18.  
  19.     public function __construct(Request $request)
  20.     {
  21.         [$apiCode, $carID] = explode('_', $request->carInfo);
  22.  
  23.         $this->data = [
  24.             'car_id'    => $carID,
  25.             'api_code'  => $apiCode,
  26.             'latitude'  => $request->latitude,
  27.             'longitude' => $request->longitude
  28.         ];
  29.  
  30.         $this->routeActions = (object)[
  31.             'startRoute' => $request->start_route ? true : false,
  32.             'endRoute'   => $request->end_route ? true : false
  33.         ];
  34.     }
  35.  
  36.     public function generate(): void
  37.     {
  38.         $car = Car::whereId($this->data['car_id'])->first();
  39.  
  40.         if ($this->isStartRoute()) {
  41.             $this->route = app(CreateRoute::class)->execute([
  42.                 'name'       => 'test',
  43.                 'car_id'     => $car->id,
  44.                 'start_time' => now()
  45.             ]);
  46.         }
  47.  
  48.         app(CreatePoint::class)->execute($this->data);
  49.  
  50.         $points = CarHelper::getLatestCarPoints($car);
  51.         $startPoint = $points[0];
  52.  
  53.         if ($points->count() > 1) {
  54.             $endPoint = $points[1];
  55.  
  56.  
  57.  
  58.             app(SectionGenerator::class)->generate($startPoint, $endPoint);
  59.         }
  60.  
  61.  
  62.         if ($this->isEndRoute()) {
  63.             $this->route = CarHelper::getLastRoute($car);
  64.             $this->endRoute();
  65.         }
  66.     }
  67.  
  68.     private function endRoute(): void
  69.     {
  70.         $this->route->update(['end_time' => now()]);
  71.     }
  72.  
  73.     private function isStartRoute(): bool
  74.     {
  75.         return $this->routeActions->startRoute && !$this->routeActions->endRoute;
  76.     }
  77.  
  78.     private function isEndRoute(): bool
  79.     {
  80.         return !$this->routeActions->startRoute && $this->routeActions->endRoute;
  81.     }
  82. }
Add Comment
Please, Sign In to add comment