1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Location\CardinalDirection; |
6
|
|
|
|
7
|
|
|
use Location\Bounds; |
8
|
|
|
use Location\Coordinate; |
9
|
|
|
use Location\Distance\DistanceInterface; |
10
|
|
|
|
11
|
|
|
class CardinalDirectionDistancesCalculator |
12
|
|
|
{ |
13
|
|
|
public function getCardinalDirectionDistances( |
14
|
|
|
Coordinate $point1, |
15
|
|
|
Coordinate $point2, |
16
|
|
|
DistanceInterface $distanceCalculator |
17
|
|
|
): CardinalDirectionDistances { |
18
|
|
|
$cardinalDirection = (new CardinalDirection())->getCardinalDirection($point1, $point2); |
19
|
|
|
$directDistance = $point1->getDistance($point2, $distanceCalculator); |
20
|
|
|
|
21
|
|
|
switch ($cardinalDirection) { |
22
|
|
|
case CardinalDirection::CARDINAL_DIRECTION_NONE: |
23
|
|
|
return CardinalDirectionDistances::create(); |
24
|
|
|
|
25
|
|
|
case CardinalDirection::CARDINAL_DIRECTION_NORTH: |
26
|
|
|
return CardinalDirectionDistances::create()->setSouth($directDistance); |
27
|
|
|
|
28
|
|
|
case CardinalDirection::CARDINAL_DIRECTION_EAST: |
29
|
|
|
return CardinalDirectionDistances::create()->setWest($directDistance); |
30
|
|
|
|
31
|
|
|
case CardinalDirection::CARDINAL_DIRECTION_SOUTH: |
32
|
|
|
return CardinalDirectionDistances::create()->setNorth($directDistance); |
33
|
|
|
|
34
|
|
|
case CardinalDirection::CARDINAL_DIRECTION_WEST: |
35
|
|
|
return CardinalDirectionDistances::create()->setEast($directDistance); |
36
|
|
|
|
37
|
|
|
case CardinalDirection::CARDINAL_DIRECTION_NORTHWEST: |
38
|
|
|
$bounds = new Bounds($point1, $point2); |
39
|
|
|
$point3 = new Coordinate($bounds->getNorth(), $bounds->getEast()); |
40
|
|
|
|
41
|
|
|
return CardinalDirectionDistances::create() |
42
|
|
|
->setEast($point1->getDistance($point3, $distanceCalculator)) |
43
|
|
|
->setSouth($point3->getDistance($point2, $distanceCalculator)); |
44
|
|
|
|
45
|
|
View Code Duplication |
case CardinalDirection::CARDINAL_DIRECTION_SOUTHWEST: |
|
|
|
|
46
|
|
|
$bounds = new Bounds( |
47
|
|
|
new Coordinate($point2->getLat(), $point1->getLng()), |
48
|
|
|
new Coordinate($point1->getLat(), $point2->getLng()) |
49
|
|
|
); |
50
|
|
|
$point3 = new Coordinate($bounds->getSouth(), $bounds->getEast()); |
51
|
|
|
|
52
|
|
|
return CardinalDirectionDistances::create() |
53
|
|
|
->setNorth($point3->getDistance($point2, $distanceCalculator)) |
54
|
|
|
->setEast($point1->getDistance($point3, $distanceCalculator)); |
55
|
|
|
|
56
|
|
View Code Duplication |
case CardinalDirection::CARDINAL_DIRECTION_NORTHEAST: |
|
|
|
|
57
|
|
|
$bounds = new Bounds( |
58
|
|
|
new Coordinate($point1->getLat(), $point2->getLng()), |
59
|
|
|
new Coordinate($point2->getLat(), $point1->getLng()) |
60
|
|
|
); |
61
|
|
|
$point3 = new Coordinate($bounds->getNorth(), $bounds->getWest()); |
62
|
|
|
|
63
|
|
|
return CardinalDirectionDistances::create() |
64
|
|
|
->setSouth($point3->getDistance($point2, $distanceCalculator)) |
65
|
|
|
->setWest($point1->getDistance($point3, $distanceCalculator)); |
66
|
|
|
|
67
|
|
|
case CardinalDirection::CARDINAL_DIRECTION_SOUTHEAST: |
68
|
|
|
$bounds = new Bounds($point2, $point1); |
69
|
|
|
$point3 = new Coordinate($bounds->getSouth(), $bounds->getWest()); |
70
|
|
|
|
71
|
|
|
return CardinalDirectionDistances::create() |
72
|
|
|
->setNorth($point3->getDistance($point2, $distanceCalculator)) |
73
|
|
|
->setWest($point1->getDistance($point3, $distanceCalculator)); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return CardinalDirectionDistances::create(); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.