Advertisement
Guest User

Untitled

a guest
Jul 13th, 2020
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.53 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Helpers\Map;
  4.  
  5. use App\Exceptions\MapPointException;
  6. use App\Models\Car\CarPoint;
  7. use App\Models\Car\CarRouteSection;
  8. use App\Models\Car\PointsSection;
  9. use App\Services\Map\Section\CreateSection;
  10. use Carbon\CarbonInterval;
  11.  
  12. class SectionGenerator
  13. {
  14.     private CarPoint        $startPoint;
  15.     private CarPoint        $endPoint;
  16.     private CarRouteSection $section;
  17.  
  18.     public function generate(CarPoint $startPoint, CarPoint $endPoint): ?CarRouteSection
  19.     {
  20.         $this->startPoint = $startPoint;
  21.         $this->endPoint = $endPoint;
  22.  
  23.         if ($this->isStartPointIDGreaterEndPointID()) {
  24.             $this->startPoint = $endPoint;
  25.             $this->endPoint = $startPoint;
  26. //            throw new MapPointException(
  27. //                'Starting point cannot be greater than ending point',
  28. //                'Starting point cannot be greater than ending point');
  29.         }
  30.  
  31.         if ($this->isPointsHasOneRoute() && $this->isPointsHasOneCar()) {
  32.             $interval = $endPoint->created_at->diffAsCarbonInterval($startPoint->created_at);
  33.  
  34.             $this->section = $this->createSection($interval);
  35.  
  36.             $this->createRelationPointSection();
  37.  
  38.             return $this->section;
  39.         }
  40.         return null;
  41.     }
  42.  
  43.     private function createSection(CarbonInterval $interval): CarRouteSection
  44.     {
  45.         return app(CreateSection::class)->execute([
  46.             'route_id'       => $this->endPoint->route_id,
  47.             'moving_time_ru' => $interval->locale('ru')->forHumans(),
  48.             'moving_time_en' => $interval->locale('en')->forHumans(),
  49.         ]);
  50.     }
  51.  
  52.     private function createRelationPointSection(): void
  53.     {
  54.         PointsSection::create([
  55.             'section_id' => $this->section->id,
  56.             'point_id'   => $this->startPoint->id,
  57.         ]);
  58.         $this->startPoint->update(['section_id' => $this->section->id]);
  59.  
  60.         PointsSection::create([
  61.             'section_id' => $this->section->id,
  62.             'point_id'   => $this->endPoint->id,
  63.         ]);
  64.         $this->endPoint->update(['section_id' => $this->section->id]);
  65.     }
  66.  
  67.     private function isPointsHasOneRoute(): bool
  68.     {
  69.         return $this->startPoint->route_id === $this->endPoint->route_id;
  70.     }
  71.  
  72.     private function isPointsHasOneCar(): bool
  73.     {
  74.         return $this->startPoint->car_id === $this->endPoint->car_id;
  75.     }
  76.  
  77.     private function isStartPointIDGreaterEndPointID(): bool
  78.     {
  79.         return $this->startPoint->id > $this->endPoint->id;
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement