1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Tack Tracker - A library for tracking long-running task progress |
5
|
|
|
* |
6
|
|
|
* @license http://opensource.org/licenses/MIT |
7
|
|
|
* @link https://github.com/caseyamcl/tasktracker |
8
|
|
|
* @version 2.0 |
9
|
|
|
* @package caseyamcl/tasktracker |
10
|
|
|
* @author Casey McLaughlin <[email protected]> |
11
|
|
|
* |
12
|
|
|
* For the full copyright and license information, please view the LICENSE |
13
|
|
|
* file that was distributed with this source code. |
14
|
|
|
* |
15
|
|
|
* ------------------------------------------------------------------ |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace TaskTracker; |
19
|
|
|
|
20
|
|
|
use InvalidArgumentException; |
21
|
|
|
use Symfony\Component\EventDispatcher\Event; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Task Tracker Tick |
25
|
|
|
* |
26
|
|
|
* Represents a progress tick |
27
|
|
|
* |
28
|
|
|
* @author Casey McLaughlin <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
class Tick extends Event implements TickInterface |
31
|
|
|
{ |
32
|
|
|
const SUCCESS = 1; |
33
|
|
|
const FAIL = 0; |
34
|
|
|
const SKIP = -1; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string Custom Message |
38
|
|
|
*/ |
39
|
|
|
private $message; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var float Timestamp (microtime float) |
43
|
|
|
*/ |
44
|
|
|
private $timestamp; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var int The status of tick (success, fail, or skip) |
48
|
|
|
*/ |
49
|
|
|
private $status; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var int |
53
|
|
|
*/ |
54
|
|
|
private $incrementBy; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var Report |
58
|
|
|
*/ |
59
|
|
|
private $report; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var array |
63
|
|
|
*/ |
64
|
|
|
private $extraInfo; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Constructor |
68
|
|
|
* |
69
|
|
|
* @param Tracker $tracker |
70
|
|
|
* @param int $status |
71
|
|
|
* @param string $message |
72
|
|
|
* @param array $extraInfo |
73
|
|
|
* @param int $incrementBy |
74
|
|
|
*/ |
75
|
|
|
public function __construct(Tracker $tracker, $status = self::SUCCESS, $message = '', array $extraInfo = [], $incrementBy = 1) |
76
|
|
|
{ |
77
|
|
|
if ( ! in_array($status, [self::FAIL, self::SKIP, self::SUCCESS])) { |
78
|
|
|
throw new InvalidArgumentException("Invalid tick status"); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
//Set parameters |
82
|
|
|
$this->incrementBy = $incrementBy; |
83
|
|
|
$this->status = (int) $status; |
84
|
|
|
$this->timestamp = microtime(true); |
85
|
|
|
$this->message = $message; |
86
|
|
|
$this->extraInfo = $extraInfo; |
87
|
|
|
$this->report = new Report($this, $tracker); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Returns the message associated with the Tick event |
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
public function getMessage() |
96
|
|
|
{ |
97
|
|
|
return $this->message; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Returns the timestamp (microtime float) of the Tick event |
102
|
|
|
* |
103
|
|
|
* @return float |
104
|
|
|
*/ |
105
|
|
|
public function getTimestamp() |
106
|
|
|
{ |
107
|
|
|
return $this->timestamp; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Returns the status (Tick::SUCCESS, Tick::FAIL, TICK::SKIP) of the Tick event |
112
|
|
|
* |
113
|
|
|
* @return int |
114
|
|
|
*/ |
115
|
|
|
public function getStatus() |
116
|
|
|
{ |
117
|
|
|
return $this->status; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Returns the number of increments associated with with the Tick event |
122
|
|
|
* |
123
|
|
|
* @return int |
124
|
|
|
*/ |
125
|
|
|
public function getIncrementBy() |
126
|
|
|
{ |
127
|
|
|
return $this->incrementBy; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Returns the report associated with the Tick event |
132
|
|
|
* |
133
|
|
|
* @return Report |
134
|
|
|
*/ |
135
|
|
|
public function getReport() |
136
|
|
|
{ |
137
|
|
|
return $this->report; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Returns any extra information associated with the Tick event |
142
|
|
|
* |
143
|
|
|
* @return array |
144
|
|
|
*/ |
145
|
|
|
public function getExtraInfo() |
146
|
|
|
{ |
147
|
|
|
return $this->extraInfo; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|