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

UserNotice::runInternal()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 5
nop 0
dl 0
loc 18
rs 9.6111
c 0
b 0
f 0
1
<?php declare( strict_types=1 );
2
3
namespace BotRiconferme\Task\Subtask;
4
5
/**
6
 * Notify the affected users
7
 */
8
class UserNotice extends Subtask {
9
	/**
10
	 * @inheritDoc
11
	 */
12
	public function runInternal() : int {
13
		$pages = $this->getDataProvider()->getCreatedPages();
14
		$users = $this->getDataProvider()->getUsersToProcess();
15
16
		if ( !$pages || !$users ) {
1 ignored issue
show
Bug Best Practice introduced by
The expression $pages of type BotRiconferme\Page\PageRiconferma[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
17
			return self::STATUS_NOTHING;
18
		}
19
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
29
		return self::STATUS_GOOD;
30
	}
31
32
	/**
33
	 * Leaves a message to the talk page
34
	 *
35
	 * @param string $user
36
	 * @param int $ricNum
37
	 */
38
	protected function addMsg( string $user, int $ricNum ) {
39
		$this->getLogger()->info( "Leaving msg to $user" );
40
		$msg = $this->msg( 'user-notice-msg' )->params( [ '$num' => $ricNum ] )->text();
41
42
		$params = [
43
			'title' => "User talk:$user",
44
			'section' => 'new',
45
			'text' => $msg,
46
			'sectiontitle' => $this->getConfig()->get( 'user-notice-title' ),
47
			'summary' => $this->getConfig()->get( 'user-notice-summary' )
48
		];
49
50
		$this->getController()->editPage( $params );
51
	}
52
}
53