1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of PHP-Task library. |
4
|
|
|
* |
5
|
|
|
* (c) php-task |
6
|
|
|
* |
7
|
|
|
* This source file is subject to the MIT license that is bundled |
8
|
|
|
* with this source code in the file LICENSE. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Task; |
12
|
|
|
|
13
|
|
|
use Cron\CronExpression; |
14
|
|
|
use Task\FrequentTask\CronTask; |
15
|
|
|
use Task\FrequentTask\DailyTask; |
16
|
|
|
use Task\FrequentTask\HourlyTask; |
17
|
|
|
use Task\FrequentTask\MonthlyTask; |
18
|
|
|
use Task\FrequentTask\WeeklyTask; |
19
|
|
|
use Task\FrequentTask\YearlyTask; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Builder for tasks. |
23
|
|
|
* |
24
|
|
|
* @author @wachterjohannes <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class TaskBuilder implements TaskBuilderInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var SchedulerInterface |
30
|
|
|
*/ |
31
|
|
|
private $scheduler; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var TaskInterface |
35
|
|
|
*/ |
36
|
|
|
private $task; |
37
|
|
|
|
38
|
10 |
|
public static function create(SchedulerInterface $scheduler, $taskName, $workload) |
39
|
|
|
{ |
40
|
10 |
|
return new self($scheduler, new Task($taskName, $workload)); |
41
|
|
|
} |
42
|
|
|
|
43
|
10 |
|
private function __construct(SchedulerInterface $scheduler, TaskInterface $task) |
44
|
|
|
{ |
45
|
10 |
|
$this->scheduler = $scheduler; |
46
|
10 |
|
$this->task = $task; |
47
|
10 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
|
public function hourly(\DateTime $start = null, \DateTime $end = null) |
53
|
|
|
{ |
54
|
|
|
$this->task = new HourlyTask($this->task, $start ?: new \DateTime(), $end); |
55
|
|
|
|
56
|
|
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
4 |
|
public function daily(\DateTime $start = null, \DateTime $end = null) |
63
|
|
|
{ |
64
|
4 |
|
$this->task = new DailyTask($this->task, $start ?: new \DateTime(), $end); |
65
|
|
|
|
66
|
4 |
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
|
|
public function weekly(\DateTime $start = null, \DateTime $end = null) |
73
|
|
|
{ |
74
|
|
|
$this->task = new WeeklyTask($this->task, $start ?: new \DateTime(), $end); |
75
|
|
|
|
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
82
|
|
|
public function monthly(\DateTime $start = null, \DateTime $end = null) |
83
|
|
|
{ |
84
|
|
|
$this->task = new MonthlyTask($this->task, $start ?: new \DateTime(), $end); |
85
|
|
|
|
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
|
|
public function yearly(\DateTime $start = null, \DateTime $end = null) |
93
|
|
|
{ |
94
|
|
|
$this->task = new YearlyTask($this->task, $start ?: new \DateTime(), $end); |
95
|
|
|
$this->setExecutionDate($this->task->getNextRunDateTime()); |
96
|
|
|
|
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritdoc} |
102
|
|
|
*/ |
103
|
|
|
public function cron($cronExpression, \DateTime $start = null, \DateTime $end = null) |
104
|
|
|
{ |
105
|
|
|
$this->task = new CronTask(CronExpression::factory($cronExpression), $this->task, $start, $end); |
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
*/ |
111
|
1 |
|
public function setExecutionDate(\DateTime $executionDate) |
112
|
|
|
{ |
113
|
1 |
|
$this->task->setExecutionDate($executionDate); |
114
|
|
|
|
115
|
1 |
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* {@inheritdoc} |
120
|
|
|
*/ |
121
|
1 |
|
public function setKey($key) |
122
|
|
|
{ |
123
|
1 |
|
$this->task->setKey($key); |
124
|
|
|
|
125
|
1 |
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* {@inheritdoc} |
130
|
|
|
*/ |
131
|
1 |
|
public function immediately() |
132
|
|
|
{ |
133
|
1 |
|
$this->task->setExecutionDate(new \DateTime()); |
134
|
|
|
|
135
|
1 |
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* {@inheritdoc} |
140
|
|
|
*/ |
141
|
8 |
|
public function schedule() |
142
|
|
|
{ |
143
|
8 |
|
return $this->scheduler->schedule($this->task); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.
We recommend to add an additional type check (or disallow null for the parameter):