Completed
Push — master ( cb2b26...7fdd78 )
by Hugues
02:41
created

Trace::http()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

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