-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/pigeon-distance' into main
- Loading branch information
Showing
12 changed files
with
375 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
namespace App\Computers; | ||
|
||
use App\Exceptions\Computers\FlightDistanceComputer\MissingFromCoordinateException; | ||
use App\Exceptions\Computers\FlightDistanceComputer\MissingToCoordinateException; | ||
|
||
class FlightDistanceComputer | ||
{ | ||
protected const KM_IN_MILE = 1.609344; | ||
protected const METERS_IN_KM = 1_000; | ||
protected const MINUTES_IN_DEGREE = 60; | ||
protected const STATUTE_MILES_IN_NAUTICAL_MILE = 1.1515; | ||
|
||
protected ?float $fromLat = null; | ||
protected ?float $fromLng = null; | ||
|
||
protected ?float $toLat = null; | ||
protected ?float $toLng = null; | ||
|
||
public function __construct() | ||
{ | ||
} | ||
|
||
public function from(float $lat, float $lng): self | ||
{ | ||
$this->fromLat = $lat; | ||
$this->fromLng = $lng; | ||
|
||
return $this; | ||
} | ||
|
||
public function to(float $lat, float $lng): self | ||
{ | ||
$this->toLat = $lat; | ||
$this->toLng = $lng; | ||
|
||
return $this; | ||
} | ||
|
||
protected function checkMissingCoordinates(): void | ||
{ | ||
if ($this->fromLng === null || $this->fromLat === null) { | ||
throw new MissingFromCoordinateException(); | ||
} | ||
|
||
if ($this->toLng === null || $this->toLat === null) { | ||
throw new MissingToCoordinateException(); | ||
} | ||
} | ||
|
||
public function getDistanceInMiles(): float | ||
{ | ||
$this->checkMissingCoordinates(); | ||
|
||
if (($this->fromLat === $this->toLat) && ($this->fromLng === $this->toLng)) { | ||
return 0; | ||
} | ||
|
||
$theta = $this->fromLng - $this->toLng; | ||
$dist = sin(deg2rad($this->fromLat)) * sin(deg2rad($this->toLat)) + cos(deg2rad($this->fromLat)) * cos(deg2rad($this->toLat)) * cos(deg2rad($theta)); | ||
$dist = acos($dist); | ||
$dist = rad2deg($dist); | ||
|
||
return $dist * self::MINUTES_IN_DEGREE * self::STATUTE_MILES_IN_NAUTICAL_MILE; | ||
} | ||
|
||
public function getDistanceInKm(): float | ||
{ | ||
return $this->getDistanceInMiles() * self::KM_IN_MILE; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace App\Computers; | ||
|
||
use App\Exceptions\Computers\FlightDurationComputer\MissingDistanceException; | ||
|
||
class FlightDurationComputer | ||
{ | ||
protected const METERS_IN_KM = 1_000; | ||
protected const MINUTES_IN_HOUR = 60; | ||
|
||
protected const EARTH_KM_CIRCUMFERENCE = 40_070; | ||
|
||
protected const MAX_METER_DISTANCE_ON_EARTH = self::EARTH_KM_CIRCUMFERENCE * self::METERS_IN_KM / 2; | ||
protected const MIN_METER_DISTANCE_ON_EARTH = 0; | ||
|
||
protected const MIN_DURATION_IN_MINUTES = 10; | ||
protected const MAX_DURATION_IN_MINUTES = 4 * self::MINUTES_IN_HOUR; | ||
|
||
protected ?float $distanceInMeter = null; | ||
|
||
public function __construct() | ||
{ | ||
} | ||
|
||
public function forKmDistance($kmDistance): self | ||
{ | ||
$this->distanceInMeter = $kmDistance * self::METERS_IN_KM; | ||
|
||
return $this; | ||
} | ||
|
||
protected function checkMissingDistance(): void | ||
{ | ||
if ($this->distanceInMeter === null) { | ||
throw new MissingDistanceException(); | ||
} | ||
} | ||
|
||
public function getDurationInMinutes(): float | ||
{ | ||
$this->checkMissingDistance(); | ||
|
||
$b = self::MIN_DURATION_IN_MINUTES; | ||
$a = (self::MAX_DURATION_IN_MINUTES - self::MIN_DURATION_IN_MINUTES) / (self::MAX_METER_DISTANCE_ON_EARTH - self::MIN_METER_DISTANCE_ON_EARTH); | ||
$x = $this->distanceInMeter; | ||
|
||
return $a * $x + $b; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
app/Exceptions/Computers/FlightDistanceComputer/MissingFromCoordinateException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace App\Exceptions\Computers\FlightDistanceComputer; | ||
|
||
use Exception; | ||
use Throwable; | ||
|
||
class MissingFromCoordinateException extends Exception | ||
{ | ||
public function __construct(string $message = "", int $code = 0, ?Throwable $previous = null) | ||
{ | ||
parent::__construct("From coordinate are required to compute distance", $code, $previous); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
app/Exceptions/Computers/FlightDistanceComputer/MissingToCoordinateException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace App\Exceptions\Computers\FlightDistanceComputer; | ||
|
||
use Exception; | ||
use Throwable; | ||
|
||
class MissingToCoordinateException extends Exception | ||
{ | ||
public function __construct(string $message = "", int $code = 0, ?Throwable $previous = null) | ||
{ | ||
parent::__construct("To coordinate are required to compute distance", $code, $previous); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
app/Exceptions/Computers/FlightDurationComputer/MissingDistanceException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace App\Exceptions\Computers\FlightDurationComputer; | ||
|
||
use Exception; | ||
use Throwable; | ||
|
||
class MissingDistanceException extends Exception | ||
{ | ||
public function __construct(string $message = "", int $code = 0, ?Throwable $previous = null) | ||
{ | ||
parent::__construct("Distance is required to compute duration", $code, $previous); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.