1
|
|
|
<?php declare( strict_types=1 ); |
2
|
|
|
|
3
|
|
|
namespace BotRiconferme; |
4
|
|
|
|
5
|
|
|
use BotRiconferme\Task\CloseOld; |
6
|
|
|
use BotRiconferme\Task\StartNew; |
7
|
|
|
use BotRiconferme\Task\StartVote; |
8
|
|
|
use BotRiconferme\Task\Subtask\ArchivePages; |
9
|
|
|
use BotRiconferme\Task\Subtask\ClosePages; |
10
|
|
|
use BotRiconferme\Task\Subtask\CreatePages; |
11
|
|
|
use BotRiconferme\Task\Subtask\FailedUpdates; |
12
|
|
|
use BotRiconferme\Task\Subtask\SimpleUpdates; |
13
|
|
|
use BotRiconferme\Task\Subtask\Subtask; |
14
|
|
|
use BotRiconferme\Task\Subtask\OpenUpdates; |
15
|
|
|
use BotRiconferme\Task\Subtask\UserNotice; |
16
|
|
|
use BotRiconferme\Task\Task; |
17
|
|
|
use BotRiconferme\Task\UpdateList; |
18
|
|
|
use BotRiconferme\Wiki\Controller; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Wrapper for single tasks |
22
|
|
|
* @todo Reduce duplication with Task class and subclasses |
23
|
|
|
*/ |
24
|
|
|
class TaskManager { |
25
|
|
|
// Run modes |
26
|
|
|
public const MODE_COMPLETE = 'full process'; |
27
|
|
|
public const MODE_TASK = 'single task'; |
28
|
|
|
public const MODE_SUBTASK = 'single subtask'; |
29
|
|
|
|
30
|
|
|
/** @var string[] */ |
31
|
|
|
private const TASKS_MAP = [ |
32
|
|
|
'start-new' => StartNew::class, |
33
|
|
|
'close-old' => CloseOld::class, |
34
|
|
|
'update-list' => UpdateList::class, |
35
|
|
|
'start-vote' => StartVote::class |
36
|
|
|
]; |
37
|
|
|
private const SUBTASKS_MAP = [ |
38
|
|
|
'archive-pages' => ArchivePages::class, |
39
|
|
|
'close-pages' => ClosePages::class, |
40
|
|
|
'create-pages' => CreatePages::class, |
41
|
|
|
'failed-updates' => FailedUpdates::class, |
42
|
|
|
'simple-updates' => SimpleUpdates::class, |
43
|
|
|
'open-updates' => OpenUpdates::class, |
44
|
|
|
'user-notice' => UserNotice::class |
45
|
|
|
]; |
46
|
|
|
/** @var TaskDataProvider */ |
47
|
|
|
private $provider; |
48
|
|
|
/** @var Logger */ |
49
|
|
|
private $logger; |
50
|
|
|
/** @var Controller */ |
51
|
|
|
private $controller; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param Logger $logger |
55
|
|
|
* @param Controller $controller |
56
|
|
|
*/ |
57
|
|
|
public function __construct( Logger $logger, Controller $controller ) { |
58
|
|
|
$this->logger = $logger; |
59
|
|
|
$this->controller = $controller; |
60
|
|
|
$this->provider = new TaskDataProvider( $this->logger, $this->controller ); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Main entry point |
65
|
|
|
* |
66
|
|
|
* @param string $mode One of the MODE_ constants |
67
|
|
|
* @param string|null $name Only used in MODE_TASK and MODE_SUBTASK |
68
|
|
|
* @return TaskResult |
69
|
|
|
*/ |
70
|
|
|
public function run( string $mode, string $name = null ) : TaskResult { |
71
|
|
|
if ( $mode === self::MODE_COMPLETE ) { |
72
|
|
|
return $this->runAllTasks(); |
73
|
|
|
} elseif ( $name === null ) { |
74
|
|
|
throw new \BadMethodCallException( 'MODE_TASK and MODE_SUBTASK need a (sub)task name.' ); |
75
|
|
|
} else { |
76
|
|
|
return $mode === self::MODE_TASK ? $this->runTask( $name ) : $this->runSubtask( $name ); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Run everything |
82
|
|
|
* |
83
|
|
|
* @return TaskResult |
84
|
|
|
*/ |
85
|
|
|
protected function runAllTasks() : TaskResult { |
86
|
|
|
$orderedList = [ |
87
|
|
|
'update-list', |
88
|
|
|
'start-new', |
89
|
|
|
'start-vote', |
90
|
|
|
'close-old' |
91
|
|
|
]; |
92
|
|
|
|
93
|
|
|
$res = new TaskResult( TaskResult::STATUS_GOOD ); |
94
|
|
|
do { |
95
|
|
|
$res->merge( $this->runTask( current( $orderedList ) ) ); |
96
|
|
|
} while ( $res->isOK() && next( $orderedList ) ); |
97
|
|
|
|
98
|
|
|
return $res; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Run a single task |
103
|
|
|
* |
104
|
|
|
* @param string $name |
105
|
|
|
* @return TaskResult |
106
|
|
|
*/ |
107
|
|
|
protected function runTask( string $name ) : TaskResult { |
108
|
|
|
if ( !isset( self::TASKS_MAP[ $name ] ) ) { |
109
|
|
|
throw new \InvalidArgumentException( "'$name' is not a valid task." ); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$class = self::TASKS_MAP[ $name ]; |
113
|
|
|
return $this->getTaskInstance( $class )->run(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Run a single subtask |
118
|
|
|
* |
119
|
|
|
* @param string $name |
120
|
|
|
* @return TaskResult |
121
|
|
|
*/ |
122
|
|
|
protected function runSubtask( string $name ) : TaskResult { |
123
|
|
|
if ( !isset( self::SUBTASKS_MAP[ $name ] ) ) { |
124
|
|
|
throw new \InvalidArgumentException( "'$name' is not a valid subtask." ); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$class = self::SUBTASKS_MAP[ $name ]; |
128
|
|
|
return $this->getSubtaskInstance( $class )->run(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Helper to make type inferencing easier |
133
|
|
|
* |
134
|
|
|
* @param string $class |
135
|
|
|
* @return Task |
136
|
|
|
*/ |
137
|
|
|
private function getTaskInstance( string $class ) : Task { |
138
|
|
|
return new $class( $this->logger, $this->controller, $this->provider ); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Helper to make type inferencing easier |
143
|
|
|
* |
144
|
|
|
* @param string $class |
145
|
|
|
* @return Subtask |
146
|
|
|
*/ |
147
|
|
|
private function getSubtaskInstance( string $class ) : Subtask { |
148
|
|
|
return new $class( $this->logger, $this->controller, $this->provider ); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|