Passed
Push — master ( 42aaff...766431 )
by Daimona
01:42
created

ClosePages::updateNews()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 38
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 25
nc 3
nop 1
dl 0
loc 38
rs 9.2088
c 0
b 0
f 0
1
<?php declare( strict_types=1 );
2
3
namespace BotRiconferme\Task\Subtask;
4
5
use BotRiconferme\Page;
6
use BotRiconferme\PageRiconferma;
7
use BotRiconferme\TaskResult;
8
9
/**
10
 * For each open page, protect it, add a closing text if it was a vote, and
11
 * update the text in the base page
12
 */
13
class ClosePages extends Subtask {
14
	/**
15
	 * @inheritDoc
16
	 */
17
	public function run() : TaskResult {
18
		$this->getLogger()->info( 'Starting task ClosePages' );
19
20
		$pages = $this->getDataProvider()->getPagesToClose();
21
		$protectReason = $this->getConfig()->get( 'close-protect-summary' );
22
		foreach ( $pages as $page ) {
23
			if ( $page->isVote() ) {
24
				$this->addVoteCloseText( $page );
25
			}
26
			$this->getController()->protectPage( $page->getTitle(), $protectReason );
27
			$this->updateBasePage( $page );
28
		}
29
30
		$this->getLogger()->info( 'Task ClosePages completed successfully' );
31
		return new TaskResult( self::STATUS_OK );
32
	}
33
34
	/**
35
	 * @param PageRiconferma $page
36
	 */
37
	protected function addVoteCloseText( PageRiconferma $page ) {
38
		$content = $page->getContent();
39
		$beforeReg = '!è necessario ottenere una maggioranza .+ votanti\.!';
40
		$newContent = preg_replace( $beforeReg, '$0' . "\n" . $page->getOutcomeText(), $content );
41
42
		$params = [
43
			'text' => $newContent,
44
			'summary' => $this->getConfig()->get( 'close-result-summary' )
45
		];
46
		$page->edit( $params );
47
	}
48
49
	/**
50
	 * @param PageRiconferma $page
51
	 * @see CreatePages::updateBasePage()
52
	 */
53
	protected function updateBasePage( PageRiconferma $page ) {
54
		$this->getLogger()->info( "Updating base page for $page" );
55
56
		$basePage = new Page( $page->getBaseTitle() );
57
		$current = $basePage->getContent();
58
59
		$outcomeText = $page->getOutcome() & PageRiconferma::OUTCOME_FAIL ?
60
			'non riconfermato' :
61
			'riconfermato';
62
		$text = $page->isVote() ? "votazione: $outcomeText" : 'riconferma tacita';
63
64
		$newContent = str_replace( 'riconferma in corso', $text, $current );
65
		$params = [
66
			'text' => $newContent,
67
			'summary' => $this->getConfig()->get( 'close-base-page-summary-update' )
68
		];
69
70
		$basePage->edit( $params );
71
	}
72
}
73