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

PageDeleter::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
dl 7
loc 7
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 2
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 Addwiki\Mediawiki\DataModel\PageIdentifier;
8
use Addwiki\Mediawiki\DataModel\Revision;
9
use Addwiki\Mediawiki\DataModel\Title;
10
11
/**
12
 * @access private
13
 *
14
 * @author Addshore
15
 */
16
class PageDeleter extends Service {
17
18
	/**
19
	 * @since 0.2
20
	 *
21
	 * @param Page $page
22
	 * @param array $extraParams
23
	 */
24 View Code Duplication
	public function delete( Page $page, array $extraParams = [] ): bool {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
25
		$this->api->postRequest( new SimpleRequest(
26
			'delete',
27
			$this->getDeleteParams( $page->getPageIdentifier(), $extraParams )
28
		) );
29
		return true;
30
	}
31
32
	/**
33
	 * @since 0.2
34
	 *
35
	 * @param Revision $revision
36
	 * @param array $extraParams
37
	 */
38 View Code Duplication
	public function deleteFromRevision( Revision $revision, array $extraParams = [] ): bool {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
39
		$this->api->postRequest( new SimpleRequest(
40
			'delete',
41
			$this->getDeleteParams( $revision->getPageIdentifier(), $extraParams )
42
		) );
43
		return true;
44
	}
45
46
	/**
47
	 * @since 0.2
48
	 *
49
	 *
50
	 */
51
	public function deleteFromPageId( int $pageid, array $extraParams = [] ): bool {
52
		$this->api->postRequest( new SimpleRequest(
53
			'delete',
54
			$this->getDeleteParams( new PageIdentifier( null, $pageid ), $extraParams )
55
		) );
56
		return true;
57
	}
58
59
	/**
60
	 * @since 0.5
61
	 *
62
	 * @param Title|string $title
63
	 * @param array $extraParams
64
	 */
65
	public function deleteFromPageTitle( $title, array $extraParams = [] ): bool {
66
		if ( is_string( $title ) ) {
67
			$title = new Title( $title );
68
		}
69
		$this->api->postRequest( new SimpleRequest(
70
			'delete',
71
			$this->getDeleteParams( new PageIdentifier( $title ), $extraParams )
72
		) );
73
		return true;
74
	}
75
76
	/**
77
	 *
78
	 * @return mixed[]
79
	 */
80
	private function getDeleteParams( PageIdentifier $identifier, array $extraParams ): array {
81
		$params = [];
82
83
		if ( $identifier->getId() !== null ) {
84
			$params['pageid'] = $identifier->getId();
85
		} else {
86
			$params['title'] = $identifier->getTitle()->getTitle();
87
		}
88
89
		$params['token'] = $this->api->getToken( 'delete' );
90
91
		return array_merge( $extraParams, $params );
92
	}
93
94
}
95