1
|
|
|
<?php |
2
|
|
|
namespace ItinerarySorter\Application; |
3
|
|
|
|
4
|
|
|
use ItinerarySorter\Exception\EmptyItineraryException; |
5
|
|
|
use ItinerarySorter\Exception\ItineraryNotFoundException; |
6
|
|
|
use ItinerarySorter\Model\BoardingCardCollection; |
7
|
|
|
use ItinerarySorter\Model\BoardingCard; |
8
|
|
|
|
9
|
|
|
class ItinerarySorter |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var array |
13
|
|
|
*/ |
14
|
|
|
private $destinationHashmap = []; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
private $originHashmap = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private $pointersHashmap = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var null|BoardingCard |
28
|
|
|
*/ |
29
|
|
|
private $backwardPointer; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var null|BoardingCard |
33
|
|
|
*/ |
34
|
|
|
private $forwardPointer; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var BoardingCardCollection |
38
|
|
|
*/ |
39
|
|
|
private $sortedBoardingCards; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Given a unsorted collection of boarding cards |
43
|
|
|
* Returns a sorted collection of boarding cards |
44
|
|
|
* |
45
|
|
|
* @param BoardingCardCollection $boardingCards |
46
|
|
|
* @return BoardingCardCollection |
47
|
|
|
* @throws ItineraryNotFoundException |
48
|
|
|
* @throws EmptyItineraryException |
49
|
|
|
*/ |
50
|
|
|
public function sort(BoardingCardCollection $boardingCards) |
51
|
|
|
{ |
52
|
|
|
$this->generateHashmaps($boardingCards); |
53
|
|
|
|
54
|
|
|
$initialPointer = $this->initializePointers($boardingCards); |
55
|
|
|
|
56
|
|
|
$this->sortedBoardingCards = new BoardingCardCollection([$initialPointer]); |
57
|
|
|
|
58
|
|
|
while (!empty($this->forwardPointer)) { |
59
|
|
|
$this->moveForward(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
while (!empty($this->backwardPointer)) { |
63
|
|
|
$this->moveBackward(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if (!empty($this->pointersHashmap)) { |
67
|
|
|
throw new ItineraryNotFoundException(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$this->clearHashmaps(); |
71
|
|
|
|
72
|
|
|
return $this->sortedBoardingCards; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param BoardingCardCollection $boardingCards |
77
|
|
|
*/ |
78
|
|
|
private function generateHashmaps(BoardingCardCollection $boardingCards) |
79
|
|
|
{ |
80
|
|
|
foreach ($boardingCards->getBoardingCards() as $pointer) { |
81
|
|
|
/** @var $pointer BoardingCard */ |
82
|
|
|
$this->pointersHashmap[$pointer->getUuid()] = $pointer; |
83
|
|
|
$this->destinationHashmap[$pointer->getDestination()][] = $pointer->getUuid(); |
84
|
|
|
$this->originHashmap[$pointer->getOrigin()][] = $pointer->getUuid(); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
private function clearHashmaps() |
89
|
|
|
{ |
90
|
|
|
unset($this->pointersHashmap); |
91
|
|
|
unset($this->destinationHashmap); |
92
|
|
|
unset($this->originHashmap); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
private function moveBackward() |
96
|
|
|
{ |
97
|
|
|
$origin = $this->backwardPointer->getOrigin(); |
98
|
|
|
|
99
|
|
|
if ($this->forwardPointer != $this->backwardPointer) { |
100
|
|
|
unset($this->pointersHashmap[$this->backwardPointer->getUuid()]); |
101
|
|
|
$this->backwardPointer = null; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
View Code Duplication |
if ($this->isThereNextLeg($this->destinationHashmap, $origin)) { |
|
|
|
|
105
|
|
|
$backwardLeg = array_shift($this->destinationHashmap[$origin]); |
106
|
|
|
$pointer = $this->pointersHashmap[$backwardLeg]; |
107
|
|
|
|
108
|
|
|
if ($pointer) { |
109
|
|
|
$this->sortedBoardingCards->unshift($pointer); |
110
|
|
|
$this->backwardPointer = $pointer; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
private function moveForward() |
116
|
|
|
{ |
117
|
|
|
$target = $this->forwardPointer->getDestination(); |
118
|
|
|
|
119
|
|
|
unset($this->pointersHashmap[$this->forwardPointer->getUuid()]); |
120
|
|
|
$this->forwardPointer = null; |
121
|
|
|
|
122
|
|
View Code Duplication |
if ($this->isThereNextLeg($this->originHashmap, $target)) { |
|
|
|
|
123
|
|
|
$forwardLeg = array_shift($this->originHashmap[$target]); |
124
|
|
|
$pointer = $this->pointersHashmap[$forwardLeg]; |
125
|
|
|
|
126
|
|
|
if ($pointer) { |
127
|
|
|
$this->sortedBoardingCards->push($pointer); |
128
|
|
|
$this->forwardPointer = $pointer; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param BoardingCardCollection $boardingCards |
135
|
|
|
* @return \ItinerarySorter\Model\BoardingCard |
136
|
|
|
* @throws EmptyItineraryException |
137
|
|
|
*/ |
138
|
|
|
public function initializePointers(BoardingCardCollection $boardingCards) |
139
|
|
|
{ |
140
|
|
|
$initialPointer = $boardingCards->getInitialBoardingCard(); |
141
|
|
|
|
142
|
|
|
$this->forwardPointer = $initialPointer; |
143
|
|
|
$this->backwardPointer = $initialPointer; |
144
|
|
|
|
145
|
|
|
return $initialPointer; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param array $hashmap |
150
|
|
|
* @param string $location |
151
|
|
|
* @return bool |
152
|
|
|
*/ |
153
|
|
|
private function isThereNextLeg($hashmap, $location) |
154
|
|
|
{ |
155
|
|
|
return isset($hashmap[$location]) && !empty($hashmap[$location]); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
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.