Total Complexity | 7 |
Total Lines | 56 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 0 |
1 | <?php |
||
8 | final class DijkstraWalker implements |
||
9 | GraphWalker |
||
10 | { |
||
11 | private $builder; |
||
12 | |||
13 | private $dijkstra; |
||
14 | |||
15 | private $path; |
||
16 | |||
17 | private $map; |
||
18 | |||
19 | 2 | public function __construct( |
|
20 | DataMapper $builder, |
||
21 | Dijkstra $dijkstra |
||
22 | ) { |
||
23 | 2 | $this->builder = $builder; |
|
24 | 2 | $this->dijkstra = $dijkstra; |
|
25 | |||
26 | 2 | $this->init(); |
|
27 | 2 | } |
|
28 | |||
29 | 1 | public function buildPathBetween($start, $end) : bool |
|
30 | { |
||
31 | 1 | $this->builder->rebuildRelationMap(); |
|
32 | |||
33 | 1 | $shortestPath = $this->dijkstra->shortestPaths($start, $end); |
|
34 | 1 | $prevRelations = $this->map[$start]['relations']; |
|
35 | |||
36 | 1 | $this->path = '_embedded'; |
|
37 | |||
38 | 1 | foreach ($shortestPath[0] as $meta) { |
|
39 | 1 | if ($relationName = array_search($meta, $prevRelations)) { |
|
40 | 1 | $this->path .= '.' . $relationName; |
|
41 | } |
||
42 | |||
43 | 1 | $prevRelations = $this->map[$meta]['relations']; |
|
44 | } |
||
45 | |||
46 | 1 | return true; |
|
47 | } |
||
48 | |||
49 | 2 | public function getPath() : string |
|
50 | { |
||
51 | 2 | if (!$this->path) { |
|
52 | 1 | throw new \RuntimeException( |
|
53 | 1 | 'Oops! path was never builded.' |
|
54 | ); |
||
55 | } |
||
56 | |||
57 | 1 | return $this->path; |
|
58 | } |
||
59 | |||
60 | 2 | public function init() |
|
64 | ); |
||
65 | 2 | } |
|
66 | } |
||
67 |