Passed
Push — master ( 35639c...18326f )
by Daimona
01:42
created

UserNotice::addMsg()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 2
dl 0
loc 13
rs 9.9666
c 0
b 0
f 0
1
<?php declare( strict_types=1 );
2
3
namespace BotRiconferme\Task\Subtask;
4
5
use BotRiconferme\TaskResult;
6
7
/**
8
 * Notify the affected users
9
 */
10
class UserNotice extends Subtask {
11
	/**
12
	 * @inheritDoc
13
	 */
14
	public function run() : TaskResult {
15
		$this->getLogger()->info( 'Starting task UserNotice' );
16
17
		$pages = $this->getDataProvider()->getCreatedPages();
18
		$users = $this->getDataProvider()->getUsersToProcess();
19
		if ( $pages && $users ) {
20
			$ricNums = [];
21
			foreach ( $pages as $page ) {
22
				$ricNums[ $page->getUser() ] = $page->getNum();
23
			}
24
25
			foreach ( $users as $user => $_ ) {
26
				$this->addMsg( $user, $ricNums[ $user ] );
27
			}
28
		} else {
29
			$this->getLogger()->info( 'No messages to leave.' );
30
		}
31
32
		$this->getLogger()->info( 'Task UserNotice completed successfully' );
33
		return new TaskResult( self::STATUS_OK );
34
	}
35
36
	/**
37
	 * Leaves a message to the talk page
38
	 *
39
	 * @param string $user
40
	 * @param int $ricNum
41
	 */
42
	protected function addMsg( string $user, int $ricNum ) {
43
		$this->getLogger()->info( "Leaving msg to $user" );
44
		$msg = $this->msg( 'user-notice-msg' )->params( [ '$num' => $ricNum ] )->text();
45
46
		$params = [
47
			'title' => "User talk:$user",
48
			'section' => 'new',
49
			'text' => $msg,
50
			'sectiontitle' => $this->getConfig()->get( 'user-notice-title' ),
51
			'summary' => $this->getConfig()->get( 'user-notice-summary' )
52
		];
53
54
		$this->getController()->editPage( $params );
55
	}
56
}
57