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