1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HMLB\UserBundle\Message\Trace; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use HMLB\UserBundle\Exception\Exception; |
7
|
|
|
use HMLB\UserBundle\User\User; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Trace value object for more information about the context in which a message has been handled. |
11
|
|
|
* |
12
|
|
|
* @author Hugues Maignol <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class Trace |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var DateTime |
18
|
|
|
*/ |
19
|
|
|
private $initiated; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var Initiator |
23
|
|
|
*/ |
24
|
|
|
private $initiator; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var Context |
28
|
|
|
*/ |
29
|
|
|
private $context; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Trace constructor. |
33
|
|
|
* |
34
|
|
|
* @param Context $context |
35
|
|
|
* @param User|null $initiatingUser |
36
|
|
|
*/ |
37
|
|
|
private function __construct(Context $context, User $initiatingUser = null) |
38
|
|
|
{ |
39
|
|
|
$this->context = $context; |
40
|
|
|
$this->initiated = new DateTime(); |
41
|
|
|
if ($initiatingUser) { |
42
|
|
|
$this->initiator = Initiator::fromUser($initiatingUser); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Message trace when a message has been initiated by a domain user. |
48
|
|
|
* |
49
|
|
|
* @param User $initiatingUser |
50
|
|
|
* |
51
|
|
|
* @return Trace |
52
|
|
|
*/ |
53
|
|
|
public static function user(User $initiatingUser) |
54
|
|
|
{ |
55
|
|
|
return new self(new Context(), $initiatingUser); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Message trace when a message has been initiated by php CLI interface. |
60
|
|
|
* |
61
|
|
|
* @return Trace |
62
|
|
|
* |
63
|
|
|
* @throws Exception |
64
|
|
|
*/ |
65
|
|
|
public static function cli() |
66
|
|
|
{ |
67
|
|
|
$context = new Context(); |
68
|
|
|
if (!$context->isCli()) { |
69
|
|
|
throw new Exception('Not in php CLI context'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return new self($context); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Trace Context. |
77
|
|
|
* |
78
|
|
|
* @return Context |
79
|
|
|
*/ |
80
|
|
|
public function getContext() |
81
|
|
|
{ |
82
|
|
|
return $this->context; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Message initiation date. |
87
|
|
|
* |
88
|
|
|
* @return DateTime |
89
|
|
|
*/ |
90
|
|
|
public function getInitiated(): DateTime |
91
|
|
|
{ |
92
|
|
|
return $this->initiated; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return bool |
97
|
|
|
*/ |
98
|
|
|
public function hasInitiator(): bool |
99
|
|
|
{ |
100
|
|
|
return null !== $this->initiator; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Getter de initiator. |
105
|
|
|
* |
106
|
|
|
* @return Initiator |
107
|
|
|
*/ |
108
|
|
|
public function getInitiator(): Initiator |
109
|
|
|
{ |
110
|
|
|
return $this->initiator; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|