Passed
Branch master (a4e902)
by Daimona
01:39
created

ClosePages   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 52
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

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