TaskBase   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 76
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A getDataProvider() 0 2 1
A run() 0 24 4
1
<?php declare( strict_types=1 );
2
3
namespace BotRiconferme\Task;
4
5
use BotRiconferme\ContextSource;
6
use BotRiconferme\MessageProvider;
7
use BotRiconferme\TaskHelper\TaskDataProvider;
8
use BotRiconferme\TaskHelper\TaskResult;
9
use BotRiconferme\Wiki\Page\PageBotList;
10
use BotRiconferme\Wiki\WikiGroup;
11
use LogicException;
12
use Psr\Log\LoggerInterface;
13
use ReflectionClass;
14
15
/**
16
 * Base framework for all kind of tasks and subtasks
17
 */
18
abstract class TaskBase extends ContextSource {
19
	/** @var string[] */
20
	protected $errors = [];
21
	/** @var TaskDataProvider */
22
	protected $dataProvider;
23
24
	/**
25
	 * Final to keep calls linear in the TaskManager
26
	 *
27
	 * @param LoggerInterface $logger
28
	 * @param WikiGroup $wikiGroup
29
	 * @param TaskDataProvider $dataProvider
30
	 * @param MessageProvider $mp
31
	 * @param PageBotList $pbl
32
	 */
33
	final public function __construct(
34
		LoggerInterface $logger,
35
		WikiGroup $wikiGroup,
36
		TaskDataProvider $dataProvider,
37
		MessageProvider $mp,
38
		PageBotList $pbl
39
	) {
40
		parent::__construct( $logger, $wikiGroup, $mp, $pbl );
41
		$this->dataProvider = $dataProvider;
42
	}
43
44
	/**
45
	 * Entry point
46
	 *
47
	 * @return TaskResult
48
	 */
49
	final public function run(): TaskResult {
50
		$class = ( new ReflectionClass( $this ) )->getShortName();
51
		$opName = $this->getOperationName();
52
		$this->getLogger()->info( "Starting $opName $class" );
53
54
		$status = $this->runInternal();
55
56
		switch ( $status ) {
57
			case TaskResult::STATUS_GOOD:
58
				$msg = ucfirst( $opName ) . " $class completed successfully.";
59
				break;
60
			case TaskResult::STATUS_NOTHING:
61
				$msg = ucfirst( $opName ) . " $class: nothing to do.";
62
				break;
63
			case TaskResult::STATUS_ERROR:
64
				// We're fine with it, but don't run other tasks
65
				$msg = ucfirst( $opName ) . " $class completed with warnings.";
66
				break;
67
			default:
68
				throw new LogicException( "Unexpected status: $status." );
69
		}
70
71
		$this->getLogger()->info( $msg );
72
		return new TaskResult( $status, $this->errors );
73
	}
74
75
	/**
76
	 * Actual main routine.
77
	 *
78
	 * @return int One of the STATUS_* constants
79
	 */
80
	abstract protected function runInternal(): int;
81
82
	/**
83
	 * How this operation should be called in logs
84
	 *
85
	 * @return string
86
	 */
87
	abstract public function getOperationName(): string;
88
89
	/**
90
	 * @return TaskDataProvider
91
	 */
92
	protected function getDataProvider(): TaskDataProvider {
93
		return $this->dataProvider;
94
	}
95
}
96