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

Parser   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 5
c 2
b 0
f 2
lcom 1
cbo 5
dl 0
loc 46
ccs 0
cts 20
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A parsePage() 0 3 1
A parsePageAsync() 0 16 3
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