1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace SilverStripe\ContentReview\Jobs; |
4
|
|
|
|
5
|
|
|
use SilverStripe\ContentReview\Tasks\ContentReviewEmails; |
6
|
|
|
use SilverStripe\Control\HTTPRequest; |
7
|
|
|
use SilverStripe\Core\Config\Config; |
8
|
|
|
use Symbiote\QueuedJobs\Services\AbstractQueuedJob; |
9
|
|
|
use Symbiote\QueuedJobs\Services\QueuedJob; |
10
|
|
|
use Symbiote\QueuedJobs\Services\QueuedJobService; |
11
|
|
|
|
12
|
|
|
if (!class_exists(AbstractQueuedJob::class)) { |
13
|
|
|
return; |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Allows the content review module to use the optional queued jobs module to automatically |
18
|
|
|
* process content review emails. If the module isn't installed, nothing is done - SilverStripe |
19
|
|
|
* will never include this class declaration. |
20
|
|
|
* |
21
|
|
|
* If the module is installed, it will create a new job to be processed once every day by default. |
22
|
|
|
* |
23
|
|
|
* @see https://github.com/silverstripe-australia/silverstripe-queuedjobs |
24
|
|
|
*/ |
25
|
|
|
class ContentReviewNotificationJob extends AbstractQueuedJob implements QueuedJob |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* The hour that the first job will be created at (for the next day). All other jobs should |
29
|
|
|
* be triggered around this time too, as the next generation is queued when this job is run. |
30
|
|
|
* |
31
|
|
|
* @var int |
32
|
|
|
* |
33
|
|
|
* @config |
34
|
|
|
*/ |
35
|
|
|
private static $first_run_hour = 9; |
|
|
|
|
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The hour at which to run these jobs. |
39
|
|
|
* |
40
|
|
|
* @var int |
41
|
|
|
* |
42
|
|
|
* @config |
43
|
|
|
*/ |
44
|
|
|
private static $next_run_hour = 9; |
|
|
|
|
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* The minutes past the hour (see above) at which to run these jobs. |
48
|
|
|
* |
49
|
|
|
* @var int |
50
|
|
|
* |
51
|
|
|
* @config |
52
|
|
|
*/ |
53
|
|
|
private static $next_run_minute = 0; |
|
|
|
|
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* The number of days to skip between job runs (1 means run this job every day, |
57
|
|
|
* 2 means run it every second day etc). |
58
|
|
|
* |
59
|
|
|
* @var int |
60
|
|
|
* |
61
|
|
|
* @config |
62
|
|
|
*/ |
63
|
|
|
private static $next_run_in_days = 1; |
|
|
|
|
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
public function getTitle() |
69
|
|
|
{ |
70
|
|
|
return "Content Review Notification Job"; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
public function getJobType() |
77
|
|
|
{ |
78
|
|
|
$this->totalSteps = 1; |
79
|
|
|
|
80
|
|
|
return QueuedJob::QUEUED; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function process() |
84
|
|
|
{ |
85
|
|
|
$this->queueNextRun(); |
86
|
|
|
|
87
|
|
|
$task = new ContentReviewEmails(); |
88
|
|
|
$task->run(new HTTPRequest("GET", "/dev/tasks/ContentReviewEmails")); |
89
|
|
|
|
90
|
|
|
$this->currentStep = 1; |
91
|
|
|
$this->isComplete = true; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Queue up the next job to run. |
96
|
|
|
*/ |
97
|
|
|
protected function queueNextRun() |
98
|
|
|
{ |
99
|
|
|
$nextRun = new ContentReviewNotificationJob(); |
100
|
|
|
|
101
|
|
|
$nextRunTime = mktime( |
102
|
|
|
Config::inst()->get(__CLASS__, 'next_run_hour'), |
103
|
|
|
Config::inst()->get(__CLASS__, 'next_run_minute'), |
104
|
|
|
0, |
105
|
|
|
date("m"), |
106
|
|
|
date("d") + Config::inst()->get(__CLASS__, 'next_run_in_days'), |
107
|
|
|
date("Y") |
108
|
|
|
); |
109
|
|
|
|
110
|
|
|
singleton(QueuedJobService::class)->queueJob( |
111
|
|
|
$nextRun, |
112
|
|
|
date("Y-m-d H:i:s", $nextRunTime) |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.