Completed
Push — master ( 4adc53...288f8e )
by adam
02:20
created

ValueParser::parseAsync()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 14
ccs 0
cts 11
cp 0
rs 9.4286
cc 1
eloc 8
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Wikibase\Api\Service;
4
5
use DataValues\DataValue;
6
use Deserializers\Deserializer;
7
use GuzzleHttp\Promise\Promise;
8
use Mediawiki\Api\MediawikiApi;
9
use Mediawiki\Api\SimpleRequest;
10
11
/**
12
 * @access private
13
 *
14
 * @author Adam Shorland
15
 * @author Thomas Arrow
16
 */
17
class ValueParser {
18
19
	/**
20
	 * @var MediawikiApi
21
	 */
22
	private $api;
23
24
	/**
25
	 * @var Deserializer
26
	 */
27
	private $dataValueDeserializer;
28
29
	/**
30
	 * @param MediawikiApi $api
31
	 * @param Deserializer $dataValueDeserializer
32
	 */
33
	public function __construct( MediawikiApi $api, Deserializer $dataValueDeserializer ) {
34
		$this->api = $api;
35
		$this->dataValueDeserializer = $dataValueDeserializer;
36
	}
37
38
	/**
39
	 * @since 0.2
40
	 *
41
	 * @param string $value
42
	 * @param string $parser Id of the ValueParser to use
43
	 *
44
	 * @returns DataValue
45
	 */
46
	public function parse( $value, $parser ) {
47
		return $this->parseAsync( $value, $parser )->wait();
48
	}
49
50
	/**
51
	 * @since 0.7
52
	 *
53
	 * @param string $value
54
	 * @param string $parser Id of the ValueParser to use
55
	 *
56
	 * @returns Promise of a DataValue object
57
	 */
58
	public function parseAsync( $value, $parser ) {
59
		$promise = $this->api->getRequestAsync(
60
			new SimpleRequest(
61
				'wbparsevalue',
62
				array( 'parser' => $parser, 'values' => $value )
63
			)
64
		);
65
66
		return $promise->then(
67
			function ( $result ) {
68
				return $this->dataValueDeserializer->deserialize( $result['results'][0] );
69
			}
70
		);
71
	}
72
73
}
74