1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of php-task library. |
5
|
|
|
* |
6
|
|
|
* (c) php-task |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Task; |
13
|
|
|
|
14
|
|
|
use Cron\CronExpression; |
15
|
|
|
use Ramsey\Uuid\Uuid; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Task information. |
19
|
|
|
*/ |
20
|
|
|
class Task implements TaskInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $uuid; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $handlerClass; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string|\Serializable |
34
|
|
|
*/ |
35
|
|
|
protected $workload; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var CronExpression |
39
|
|
|
*/ |
40
|
|
|
protected $interval; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var \DateTime |
44
|
|
|
*/ |
45
|
|
|
protected $firstExecution; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var \DateTime |
49
|
|
|
*/ |
50
|
|
|
protected $lastExecution; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $handlerClass |
54
|
|
|
* @param string|\Serializable $workload |
55
|
|
|
* @param string $uuid |
56
|
|
|
*/ |
57
|
8 |
View Code Duplication |
public function __construct($handlerClass, $workload = null, $uuid = null) |
|
|
|
|
58
|
|
|
{ |
59
|
8 |
|
$this->uuid = $uuid ?: Uuid::uuid4()->toString(); |
60
|
8 |
|
$this->handlerClass = $handlerClass; |
61
|
8 |
|
$this->workload = $workload; |
62
|
|
|
|
63
|
8 |
|
$this->firstExecution = new \DateTime(); |
64
|
8 |
|
$this->lastExecution = new \DateTime(); |
65
|
8 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
2 |
|
public function getUuid() |
71
|
|
|
{ |
72
|
2 |
|
return $this->uuid; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
|
|
public function getHandlerClass() |
79
|
|
|
{ |
80
|
|
|
return $this->handlerClass; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
|
|
public function getWorkload() |
87
|
|
|
{ |
88
|
|
|
return $this->workload; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
|
|
public function getInterval() |
95
|
|
|
{ |
96
|
|
|
return $this->interval; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* {@inheritdoc} |
101
|
|
|
*/ |
102
|
|
|
public function getFirstExecution() |
103
|
|
|
{ |
104
|
|
|
return $this->firstExecution; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* {@inheritdoc} |
109
|
|
|
*/ |
110
|
|
|
public function setFirstExecution(\DateTime $firstExecution) |
111
|
|
|
{ |
112
|
|
|
$this->firstExecution = $firstExecution; |
113
|
|
|
$this->lastExecution = null; |
114
|
|
|
|
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* {@inheritdoc} |
120
|
|
|
*/ |
121
|
1 |
|
public function getLastExecution() |
122
|
|
|
{ |
123
|
1 |
|
return $this->lastExecution; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritdoc} |
128
|
|
|
*/ |
129
|
1 |
|
public function setInterval( |
130
|
|
|
CronExpression $interval, |
131
|
|
|
\DateTime $firstExecution = null, |
132
|
|
|
\DateTime $lastExecution = null |
133
|
|
|
) { |
134
|
1 |
|
$this->interval = $interval; |
135
|
1 |
|
$this->firstExecution = $firstExecution ?: new \DateTime(); |
136
|
1 |
|
$this->lastExecution = $lastExecution; |
137
|
|
|
|
138
|
1 |
|
return $this; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.