Parser::parsePage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Mediawiki\Api\Service;
4
5
use GuzzleHttp\Promise\PromiseInterface;
6
use Mediawiki\Api\SimpleRequest;
7
use Mediawiki\DataModel\PageIdentifier;
8
9
/**
10
 * @access private
11
 *
12
 * @author Addshore
13
 */
14
class Parser extends Service {
15
16
	/**
17
	 * @param PageIdentifier $pageIdentifier
18
	 *
19
	 * @return array the parse result (raw from the api)
20
	 */
21
	public function parsePage( PageIdentifier $pageIdentifier ) {
22
		return $this->parsePageAsync( $pageIdentifier )->wait();
23
	}
24
25
	/**
26
	 * @param PageIdentifier $pageIdentifier
27
	 *
28
	 * @return PromiseInterface of array the parse result (raw from the api)
29
	 */
30
	public function parsePageAsync( PageIdentifier $pageIdentifier ) {
31
		$params = [];
32
		if ( $pageIdentifier->getId() !== null ) {
33
			$params['pageid'] = $pageIdentifier->getId();
34
		} elseif ( $pageIdentifier->getTitle() !== null ) {
35
			$params['page'] = $pageIdentifier->getTitle()->getText();
36
		} else {
37
			throw new \RuntimeException( 'No way to identify page' );
38
		}
39
40
		$promise = $this->api->getRequestAsync( new SimpleRequest( 'parse', $params ) );
41
42
		return $promise->then( function ( $result ) {
43
			return $result['parse'];
44
		} );
45
	}
46
47
}
48