1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the prooph/php-ddd-cargo-sample package. |
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
|
|
|
declare(strict_types = 1); |
10
|
|
|
|
11
|
|
|
namespace Codeliner\CargoBackend\Model\Cargo; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* A Cargo. This is the central class in the domain model. |
15
|
|
|
* |
16
|
|
|
* A cargo is identified by a unique tracking id, and it always has an origin |
17
|
|
|
* and a route specification. The life cycle of a cargo begins with the booking procedure, |
18
|
|
|
* when the tracking id is assigned. During a (short) period of time, between booking |
19
|
|
|
* and initial routing, the cargo has no itinerary. |
20
|
|
|
* |
21
|
|
|
* The booking clerk requests a list of possible routes, matching the route specification, |
22
|
|
|
* and assigns the cargo to one route. The route to which a cargo is assigned is described |
23
|
|
|
* by an itinerary. |
24
|
|
|
* |
25
|
|
|
* A cargo can be re-routed during transport, on demand of the customer, in which case |
26
|
|
|
* a new route is specified for the cargo and a new route is requested. The old itinerary, |
27
|
|
|
* being a value object, is discarded and a new one is attached. |
28
|
|
|
* |
29
|
|
|
* @author Alexander Miertsch <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
class Cargo |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* Unique Identifier |
35
|
|
|
* |
36
|
|
|
* @var TrackingId |
37
|
|
|
*/ |
38
|
|
|
private $trackingId; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
private $origin; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* |
47
|
|
|
* @var RouteSpecification |
48
|
|
|
*/ |
49
|
|
|
private $routeSpecification; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var Itinerary |
53
|
|
|
*/ |
54
|
|
|
private $itinerary; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Construct |
58
|
|
|
* |
59
|
|
|
* @param TrackingId $aTrackingId |
60
|
|
|
* @param RouteSpecification $aRouteSpecification |
61
|
|
|
*/ |
62
|
|
|
public function __construct(TrackingId $aTrackingId, RouteSpecification $aRouteSpecification) |
63
|
|
|
{ |
64
|
|
|
$this->trackingId = $aTrackingId; |
65
|
|
|
|
66
|
|
|
//Construct is only called when the Cargo is initially created. |
67
|
|
|
//Doctrine do not call __construct when it recreates a persisted entity. |
68
|
|
|
//Therefor we can assign the origin here. |
69
|
|
|
//It will be always the same for that specific Cargo even if the RouteSpecification changes. |
70
|
|
|
$this->origin = $aRouteSpecification->origin(); |
71
|
|
|
|
72
|
|
|
$this->routeSpecification = $aRouteSpecification; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return TrackingId Unique Identifier of this Cargo |
77
|
|
|
*/ |
78
|
|
|
public function trackingId(): TrackingId |
79
|
|
|
{ |
80
|
|
|
return $this->trackingId; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return string Origin of this Cargo |
85
|
|
|
*/ |
86
|
|
|
public function origin(): string |
87
|
|
|
{ |
88
|
|
|
return $this->origin; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return RouteSpecification |
93
|
|
|
*/ |
94
|
|
|
public function routeSpecification(): RouteSpecification |
95
|
|
|
{ |
96
|
|
|
return $this->routeSpecification; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Specifies a new route for this cargo. |
101
|
|
|
* |
102
|
|
|
* @param RouteSpecification $aRouteSpecification |
103
|
|
|
*/ |
104
|
|
|
public function specifyNewRoute(RouteSpecification $aRouteSpecification) |
105
|
|
|
{ |
106
|
|
|
$this->routeSpecification = $aRouteSpecification; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return Itinerary Never null |
111
|
|
|
*/ |
112
|
|
|
public function itinerary(): Itinerary |
113
|
|
|
{ |
114
|
|
|
if (is_null($this->itinerary)) { |
115
|
|
|
return new Itinerary(array()); |
116
|
|
|
} else { |
117
|
|
|
return $this->itinerary; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Attach a new itinerary to this cargo. |
123
|
|
|
* |
124
|
|
|
* @param Itinerary $anItinerary |
125
|
|
|
*/ |
126
|
|
|
public function assignToRoute(Itinerary $anItinerary) |
127
|
|
|
{ |
128
|
|
|
$this->itinerary = $anItinerary; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param Cargo $other |
133
|
|
|
* @return bool |
134
|
|
|
*/ |
135
|
|
|
public function sameIdentityAs(Cargo $other): bool |
136
|
|
|
{ |
137
|
|
|
return $this->trackingId()->sameValueAs($other->trackingId()); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|