Completed
Push — master ( ddde46...ec2f81 )
by adam
12:43 queued 09:38
created

PageDeleter   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 7.89%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 8
lcom 1
cbo 6
dl 0
loc 102
ccs 3
cts 38
cp 0.0789
rs 10
c 5
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A delete() 0 7 1
A deleteFromRevision() 0 7 1
A deleteFromPageId() 0 7 1
A deleteFromPageTitle() 0 10 2
A getDeleteParams() 0 13 2
A __construct() 0 3 1
1
<?php
2
3
namespace Mediawiki\Api\Service;
4
5
use Mediawiki\Api\MediawikiApi;
6
use Mediawiki\Api\SimpleRequest;
7
use Mediawiki\DataModel\Page;
8
use Mediawiki\DataModel\PageIdentifier;
9
use Mediawiki\DataModel\Revision;
10
use Mediawiki\DataModel\Title;
11
12
/**
13
 * @access private
14
 *
15
 * @author Addshore
16
 */
17
class PageDeleter {
18
19
	/**
20
	 * @var MediawikiApi
21
	 */
22
	private $api;
23
24
	/**
25
	 * @param MediawikiApi $api
26
	 */
27 4
	public function __construct( MediawikiApi $api ) {
28 4
		$this->api = $api;
29 4
	}
30
31
	/**
32
	 * @since 0.2
33
	 *
34
	 * @param Page $page
35
	 * @param array $extraParams
36
	 *
37
	 * @return bool
38
	 */
39
	public function delete( Page $page, array $extraParams = [] ) {
40
		$this->api->postRequest( new SimpleRequest(
41
			'delete',
42
			$this->getDeleteParams( $page->getPageIdentifier(), $extraParams )
43
		) );
44
		return true;
45
	}
46
47
	/**
48
	 * @since 0.2
49
	 *
50
	 * @param Revision $revision
51
	 * @param array $extraParams
52
	 *
53
	 * @return bool
54
	 */
55
	public function deleteFromRevision( Revision $revision, array $extraParams = [] ) {
56
		$this->api->postRequest( new SimpleRequest(
57
			'delete',
58
			$this->getDeleteParams( $revision->getPageIdentifier(), $extraParams )
0 ignored issues
show
Bug introduced by
It seems like $revision->getPageIdentifier() can be null; however, getDeleteParams() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
59
		) );
60
		return true;
61
	}
62
63
	/**
64
	 * @since 0.2
65
	 *
66
	 * @param int $pageid
67
	 * @param array $extraParams
68
	 *
69
	 * @return bool
70
	 */
71
	public function deleteFromPageId( $pageid, array $extraParams = [] ) {
72
		$this->api->postRequest( new SimpleRequest(
73
			'delete',
74
			$this->getDeleteParams( new PageIdentifier( null, $pageid ), $extraParams )
75
		) );
76
		return true;
77
	}
78
79
	/**
80
	 * @since 0.5
81
	 *
82
	 * @param Title|string $title
83
	 * @param array $extraParams
84
	 *
85
	 * @return bool
86
	 */
87
	public function deleteFromPageTitle( $title, array $extraParams = [] ) {
88
		if ( is_string( $title ) ) {
89
			$title = new Title( $title );
90
		}
91
		$this->api->postRequest( new SimpleRequest(
92
			'delete',
93
			$this->getDeleteParams( new PageIdentifier( $title ), $extraParams )
94
		) );
95
		return true;
96
	}
97
98
	/**
99
	 * @param PageIdentifier $identifier
100
	 * @param array $extraParams
101
	 *
102
	 * @return array
103
	 */
104
	private function getDeleteParams( PageIdentifier $identifier, $extraParams ) {
105
		$params = [];
106
107
		if ( !is_null( $identifier->getId() ) ) {
108
			$params['pageid'] = $identifier->getId();
109
		} else {
110
			$params['title'] = $identifier->getTitle()->getTitle();
111
		}
112
113
		$params['token'] = $this->api->getToken( 'delete' );
114
115
		return array_merge( $extraParams, $params );
116
	}
117
118
}
119