PageProtector::protect()   B
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 0
cts 25
cp 0
rs 8.8497
c 0
b 0
f 0
cc 6
nc 4
nop 3
crap 42
1
<?php
2
3
namespace Mediawiki\Api\Service;
4
5
use InvalidArgumentException;
6
use Mediawiki\Api\SimpleRequest;
7
use Mediawiki\DataModel\Page;
8
9
/**
10
 * @access private
11
 *
12
 * @author Addshore
13
 */
14
class PageProtector extends Service {
15
16
	/**
17
	 * @since 0.3
18
	 *
19
	 * @param Page $page
20
	 * @param string[] $protections where the 'key' is the action and the 'value' is the group
21
	 * @param array $extraParams
22
	 *
23
	 * @return bool
24
	 * @throws InvalidArgumentException
25
	 */
26
	public function protect( Page $page, $protections, array $extraParams = [] ) {
27
		if ( !is_array( $protections ) || empty( $protections ) ) {
28
			throw new InvalidArgumentException(
29
				'$protections must be an array with keys and values'
30
			);
31
		}
32
33
		$params = [
34
			'pageid' => $page->getId(),
0 ignored issues
show
Deprecated Code introduced by
The method Mediawiki\DataModel\Page::getId() has been deprecated with message: since 0.5

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
35
			'token' => $this->api->getToken( 'protect' ),
36
		];
37
		$protectionsString = '';
38
		foreach ( $protections as $action => $value ) {
39
			if ( !is_string( $action ) || !is_string( $value ) ) {
40
				throw new InvalidArgumentException(
41
					'All keys and elements of $protections must be strings'
42
				);
43
			}
44
			$protectionsString = $action . '=' . $value . '|';
45
		}
46
		$params['protections'] = rtrim( $protectionsString, '|' );
47
48
		$this->api->postRequest(
49
			new SimpleRequest( 'protect', array_merge( $extraParams, $params ) )
50
		);
51
52
		return true;
53
	}
54
55
}
56