Passed
Push — master ( 761a52...bac275 )
by Daimona
02:26
created

TaskManager::setLastFullRunDate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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