Completed
Push — main ( 2daa48...b5d932 )
by
unknown
08:38
created

PageProtector::protect()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
c 0
b 0
f 0
rs 8.8497
cc 6
nc 4
nop 3
1
<?php
2
3
namespace Addwiki\Mediawiki\Api\Service;
4
5
use Addwiki\Mediawiki\Api\Client\SimpleRequest;
6
use Addwiki\Mediawiki\DataModel\Page;
7
use InvalidArgumentException;
8
9
/**
10
 * @access private
11
 *
12
 * @author Addshore
13
 */
14
class PageProtector extends Service {
15
16
	/**
17
	 * @since 0.3
18
	 *
19
	 * @param string[] $protections where the 'key' is the action and the 'value' is the group
20
	 *
21
	 * @throws InvalidArgumentException
22
	 */
23
	public function protect( Page $page, array $protections, array $extraParams = [] ): bool {
24
		if ( !is_array( $protections ) || empty( $protections ) ) {
25
			throw new InvalidArgumentException(
26
				'$protections must be an array with keys and values'
27
			);
28
		}
29
30
		$params = [
31
			'pageid' => $page->getId(),
32
			'token' => $this->api->getToken( 'protect' ),
33
		];
34
		$protectionsString = '';
35
		foreach ( $protections as $action => $value ) {
36
			if ( !is_string( $action ) || !is_string( $value ) ) {
37
				throw new InvalidArgumentException(
38
					'All keys and elements of $protections must be strings'
39
				);
40
			}
41
			$protectionsString = $action . '=' . $value . '|';
42
		}
43
		$params['protections'] = rtrim( $protectionsString, '|' );
44
45
		$this->api->postRequest(
46
			new SimpleRequest( 'protect', array_merge( $extraParams, $params ) )
47
		);
48
49
		return true;
50
	}
51
52
}
53