Completed
Push — master ( b8f377...7fb326 )
by adam
05:53
created

Parser::parsePageAsync()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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