|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the prooph/php-ddd-cargo-sample. |
|
4
|
|
|
* (c) Alexander Miertsch <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
* |
|
9
|
|
|
* Date: 29.03.14 - 18:48 |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Codeliner\CargoBackend\Infrastructure\Routing; |
|
13
|
|
|
|
|
14
|
|
|
use Codeliner\CargoBackend\Model\Cargo\Itinerary; |
|
15
|
|
|
use Codeliner\CargoBackend\Model\Cargo\Leg; |
|
16
|
|
|
use Codeliner\CargoBackend\Model\Cargo\RouteSpecification; |
|
17
|
|
|
use Codeliner\CargoBackend\Model\Routing\RoutingServiceInterface; |
|
18
|
|
|
use Codeliner\GraphTraversalBackend\Dto\EdgeDto; |
|
19
|
|
|
use Codeliner\GraphTraversalBackend\Dto\TransitPathDto; |
|
20
|
|
|
use Codeliner\GraphTraversalBackend\GraphTraversalServiceInterface; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class ExternalRoutingService |
|
24
|
|
|
* |
|
25
|
|
|
* Our end of the routing service. This is basically a data model |
|
26
|
|
|
* translation layer between our domain model and the API put forward |
|
27
|
|
|
* by the routing team, which operates in a different context from us. |
|
28
|
|
|
* |
|
29
|
|
|
* @package CargoBackend\Infrastructure\Routing |
|
30
|
|
|
* @author Alexander Miertsch <[email protected]> |
|
31
|
|
|
*/ |
|
32
|
|
|
class ExternalRoutingService implements RoutingServiceInterface |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* @var GraphTraversalServiceInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
private $graphTraversalService; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param GraphTraversalServiceInterface $aGraphTraversalService |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct(GraphTraversalServiceInterface $aGraphTraversalService) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->graphTraversalService = $aGraphTraversalService; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param RouteSpecification $aRouteSpecification |
|
49
|
|
|
* @return Itinerary[] A list of itineraries that satisfy the specification. May be an empty list if no route is found. |
|
50
|
|
|
*/ |
|
51
|
|
|
public function fetchRoutesForSpecification(RouteSpecification $aRouteSpecification): array |
|
52
|
|
|
{ |
|
53
|
|
|
$transitPaths = $this->graphTraversalService->findShortestPath( |
|
54
|
|
|
$aRouteSpecification->origin(), |
|
55
|
|
|
$aRouteSpecification->destination() |
|
56
|
|
|
); |
|
57
|
|
|
|
|
58
|
|
|
$itineraries = array(); |
|
59
|
|
|
|
|
60
|
|
|
foreach ($transitPaths as $transitPath) { |
|
61
|
|
|
$itineraries[] = $this->toItinerary($transitPath); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return $itineraries; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param TransitPathDto $aTransitPath |
|
69
|
|
|
* @return Itinerary |
|
70
|
|
|
*/ |
|
71
|
|
|
private function toItinerary(TransitPathDto $aTransitPath): Itinerary |
|
72
|
|
|
{ |
|
73
|
|
|
$legs = array(); |
|
74
|
|
|
|
|
75
|
|
|
foreach ($aTransitPath->getEdges() as $edge) { |
|
76
|
|
|
$legs[] = $this->toLeg($edge); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return new Itinerary($legs); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param EdgeDto $anEdgeDto |
|
84
|
|
|
* @return Leg |
|
85
|
|
|
*/ |
|
86
|
|
|
private function toLeg(EdgeDto $anEdgeDto): Leg |
|
87
|
|
|
{ |
|
88
|
|
|
return new Leg( |
|
89
|
|
|
$anEdgeDto->getFromUnLocode(), |
|
90
|
|
|
$anEdgeDto->getToUnLocode(), |
|
91
|
|
|
\DateTimeImmutable::createFromFormat(\DateTime::ATOM, $anEdgeDto->getFromDate()), |
|
92
|
|
|
\DateTimeImmutable::createFromFormat(\DateTime::ATOM, $anEdgeDto->getToDate()) |
|
93
|
|
|
); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|