|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ReliqArts\Logistiq\Tracking\Services; |
|
6
|
|
|
|
|
7
|
|
|
use ReliqArts\Logistiq\Tracking\Contracts\Status; |
|
8
|
|
|
use ReliqArts\Logistiq\Tracking\Contracts\Trackable; |
|
9
|
|
|
use ReliqArts\Logistiq\Tracking\Contracts\Tracker as TrackerContract; |
|
10
|
|
|
use ReliqArts\Logistiq\Tracking\Contracts\TrackingUpdate; |
|
11
|
|
|
use ReliqArts\Logistiq\Tracking\Events\StatusChanged; |
|
12
|
|
|
use ReliqArts\Logistiq\Tracking\Exceptions\StatusChangeFailed; |
|
13
|
|
|
use ReliqArts\Logistiq\Utility\Contracts\EventDispatcher; |
|
14
|
|
|
|
|
15
|
|
|
final class Tracker implements TrackerContract |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var EventDispatcher |
|
19
|
|
|
*/ |
|
20
|
|
|
private $eventDispatcher; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var TrackingUpdate |
|
24
|
|
|
*/ |
|
25
|
|
|
private $trackingUpdate; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Tracker constructor. |
|
29
|
|
|
* |
|
30
|
|
|
* @param EventDispatcher $eventDispatcher |
|
31
|
|
|
* @param TrackingUpdate $trackingUpdate |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct(EventDispatcher $eventDispatcher, TrackingUpdate $trackingUpdate) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->eventDispatcher = $eventDispatcher; |
|
36
|
|
|
$this->trackingUpdate = $trackingUpdate; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param Trackable $trackable |
|
41
|
|
|
* |
|
42
|
|
|
* @return Status |
|
43
|
|
|
*/ |
|
44
|
|
|
public function getTrackableStatus(Trackable $trackable): Status |
|
45
|
|
|
{ |
|
46
|
|
|
return $trackable->getStatus(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param Trackable $trackable |
|
51
|
|
|
* @param Status $status |
|
52
|
|
|
* |
|
53
|
|
|
* @throws StatusChangeFailed |
|
54
|
|
|
*/ |
|
55
|
|
|
public function setTrackableStatus(Trackable $trackable, Status $status): void |
|
56
|
|
|
{ |
|
57
|
|
|
$oldStatus = $trackable->getStatus(); |
|
58
|
|
|
|
|
59
|
|
|
if (!$trackable->setStatus($status)) { |
|
60
|
|
|
throw StatusChangeFailed::forTrackable($trackable, $status); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$this->registerStatusChange($trackable, $oldStatus, $status); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param Trackable $trackable |
|
68
|
|
|
* @param Status $oldStatus |
|
69
|
|
|
* @param Status $status |
|
70
|
|
|
*/ |
|
71
|
|
|
private function registerStatusChange(Trackable $trackable, Status $oldStatus, Status $status): void |
|
72
|
|
|
{ |
|
73
|
|
|
$this->eventDispatcher->dispatch(new StatusChanged($trackable, $oldStatus, $status)); |
|
74
|
|
|
$this->trackingUpdate->log( |
|
75
|
|
|
(string)$trackable->getIdentifier(), |
|
76
|
|
|
get_class($trackable), |
|
77
|
|
|
(string)$status->getIdentifier() |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|