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

ArchivePages   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 83
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A addToArchive() 0 19 3
A removeFromMainPage() 0 14 2
A runInternal() 0 6 1
A reallyAddToArchive() 0 16 2
1
<?php declare( strict_types=1 );
2
3
namespace BotRiconferme\Task\Subtask;
4
5
use BotRiconferme\Message;
6
use BotRiconferme\Page\Page;
7
use BotRiconferme\Page\PageRiconferma;
8
9
/**
10
 * Remove pages from the main page and add them to the archive
11
 */
12
class ArchivePages extends Subtask {
13
	/**
14
	 * @inheritDoc
15
	 */
16
	public function runInternal() : int {
17
		$pages = $this->getDataProvider()->getPagesToClose();
18
		$this->removeFromMainPage( $pages );
19
		$this->addToArchive( $pages );
20
21
		return self::STATUS_GOOD;
22
	}
23
24
	/**
25
	 * Removes pages from WP:A/Riconferme annuali
26
	 *
27
	 * @param PageRiconferma[] $pages
28
	 * @see UpdatesAround::addToMainPage()
29
	 */
30
	protected function removeFromMainPage( array $pages ) {
31
		$this->getLogger()->info(
32
			'Removing from main: ' . implode( ', ', array_map( 'strval', $pages ) )
33
		);
34
35
		$mainPage = new Page( $this->getConfig()->get( 'ric-main-page' ) );
36
		$remove = [];
37
		foreach ( $pages as $page ) {
38
			$remove[] = '{{' . $page->getTitle() . '}}';
39
		}
40
41
		$mainPage->edit( [
42
			'text' => str_replace( $remove, '', $mainPage->getContent() ),
43
			'summary' => $this->getConfig()->get( 'close-main-summary' )
44
		] );
45
	}
46
47
	/**
48
	 * Adds closed pages to the current archive
49
	 *
50
	 * @param PageRiconferma[] $pages
51
	 */
52
	protected function addToArchive( array $pages ) {
53
		$this->getLogger()->info(
54
			'Adding to archive: ' . implode( ', ', array_map( 'strval', $pages ) )
55
		);
56
57
		$simple = $votes = [];
58
		foreach ( $pages as $page ) {
59
			if ( $page->isVote() ) {
60
				$votes[] = $page;
61
			} else {
62
				$simple[] = $page;
63
			}
64
		}
65
66
		$simpleTitle = $this->getConfig()->get( 'close-simple-archive-title' );
67
		$voteTitle = $this->getConfig()->get( 'close-vote-archive-title' );
68
69
		$this->reallyAddToArchive( $simpleTitle, $simple );
70
		$this->reallyAddToArchive( $voteTitle, $votes );
71
	}
72
73
	/**
74
	 * Really add $pages to the given archive
75
	 *
76
	 * @param string $archiveTitle
77
	 * @param array $pages
78
	 */
79
	private function reallyAddToArchive( string $archiveTitle, array $pages ) {
80
		$archivePage = new Page( "$archiveTitle/" . date( 'Y' ) );
81
82
		$append = '';
83
		$archivedList = [];
84
		foreach ( $pages as $page ) {
85
			$append .= '{{' . $page->getTitle() . "}}\n";
86
			$archivedList[] = $page->getUserNum();
87
		}
88
89
		$summary = $this->msg( 'close-archive-summary' )
90
			->params( [ '$usernums' => Message::commaList( $archivedList ) ] )->text();
91
92
		$archivePage->edit( [
93
			'appendtext' => $append,
94
			'summary' => $summary
95
		] );
96
	}
97
}
98