|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Scheduler\Action; |
|
4
|
|
|
|
|
5
|
|
|
use Scheduler\Exception\SchedulerException; |
|
6
|
|
|
use Scheduler\Job\JobInterface; |
|
7
|
|
|
use \DateTimeInterface; |
|
8
|
|
|
use SuperClosure\Serializer; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class CallableTask |
|
12
|
|
|
* @package Task |
|
13
|
|
|
* @author Aleh Hutnikau, <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
class CallableAction implements ActionInterface |
|
16
|
|
|
{ |
|
17
|
|
|
|
|
18
|
|
|
/** @var JobInterface */ |
|
19
|
|
|
private $job; |
|
20
|
|
|
|
|
21
|
|
|
/** @var DateTimeInterface */ |
|
22
|
|
|
private $time; |
|
23
|
|
|
|
|
24
|
|
|
/** @var integer */ |
|
25
|
|
|
private $state; |
|
26
|
|
|
|
|
27
|
|
|
/** @var mixed */ |
|
28
|
|
|
private $report; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @return mixed|void |
|
32
|
|
|
*/ |
|
33
|
7 |
|
public function __invoke() |
|
34
|
|
|
{ |
|
35
|
7 |
|
$this->state = self::STATE_IN_PROGRESS; |
|
36
|
7 |
|
$this->report = call_user_func($this->getJob()->getCallable(), $this); |
|
37
|
7 |
|
$this->state = self::STATE_FINISHED; |
|
38
|
7 |
|
return $this->report; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* AbstractTask constructor. |
|
43
|
|
|
* @param JobInterface $job - job related to action |
|
44
|
|
|
* @param DateTimeInterface $time - time of occurrence (note: this it not time of execution) |
|
45
|
|
|
*/ |
|
46
|
22 |
|
public function __construct(JobInterface $job, DateTimeInterface $time) |
|
47
|
|
|
{ |
|
48
|
22 |
|
$this->state = self::STATE_INITIAL; |
|
49
|
22 |
|
$this->job = $job; |
|
50
|
22 |
|
$this->time = $time; |
|
51
|
22 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @inheritdoc |
|
55
|
|
|
*/ |
|
56
|
11 |
|
public function getTime() |
|
57
|
|
|
{ |
|
58
|
11 |
|
return $this->time; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @inheritdoc |
|
63
|
|
|
*/ |
|
64
|
10 |
|
public function getJob() |
|
65
|
|
|
{ |
|
66
|
10 |
|
return $this->job; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @return mixed|void |
|
71
|
|
|
*/ |
|
72
|
5 |
|
public function getId() |
|
73
|
|
|
{ |
|
74
|
5 |
|
return $this->getTime()->getTimestamp().md5(implode('_', [ |
|
75
|
5 |
|
$this->getTime()->getTimestamp(), |
|
76
|
5 |
|
$this->getJob()->getRRule()->getStartDate()->getTimestamp(), |
|
77
|
5 |
|
$this->getJob()->getRRule()->getRrule(), |
|
78
|
5 |
|
$this->hashCallable($this->getJob()->getCallable()) |
|
79
|
|
|
])); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @inheritdoc |
|
84
|
|
|
*/ |
|
85
|
4 |
|
public function getState() |
|
86
|
|
|
{ |
|
87
|
4 |
|
return $this->state; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @inheritdoc |
|
92
|
|
|
*/ |
|
93
|
5 |
|
public function getReport() |
|
94
|
|
|
{ |
|
95
|
5 |
|
if ($this->state !== self::STATE_FINISHED) { |
|
96
|
1 |
|
throw new SchedulerException('Attempt to get report of not finished action'); |
|
97
|
|
|
} |
|
98
|
4 |
|
return $this->report; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Get unique hash from callable |
|
103
|
|
|
* @param $callable |
|
104
|
|
|
* @return string |
|
105
|
|
|
*/ |
|
106
|
5 |
|
private function hashCallable($callable) |
|
107
|
|
|
{ |
|
108
|
5 |
|
$result = ''; |
|
109
|
5 |
|
if (is_string($callable)) { |
|
110
|
2 |
|
$result = $callable; |
|
111
|
4 |
|
} else if (is_array($callable)) { |
|
112
|
2 |
|
$callableEntity = array_shift($callable); |
|
113
|
2 |
|
$result = is_object($callableEntity) ? get_class($callableEntity) : serialize($callableEntity); |
|
114
|
2 |
|
$result .= serialize($callable[0]); |
|
115
|
3 |
|
} else if ($callable instanceof \Closure) { |
|
116
|
3 |
|
$serializer = new Serializer(); |
|
117
|
3 |
|
return $serializer->serialize($callable); |
|
118
|
1 |
|
} else if (is_object($callable)) { |
|
119
|
1 |
|
$result = serialize($callable); |
|
120
|
|
|
} |
|
121
|
3 |
|
return md5($result); |
|
122
|
|
|
} |
|
123
|
|
|
} |