ItemMerger::getIdFromInput()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
ccs 0
cts 8
cp 0
cc 4
nc 4
nop 1
crap 20
1
<?php
2
3
namespace Wikibase\Api\Service;
4
5
use InvalidArgumentException;
6
use Mediawiki\DataModel\EditInfo;
7
use Wikibase\Api\WikibaseApi;
8
use Wikibase\DataModel\Entity\Item;
9
use Wikibase\DataModel\Entity\ItemId;
10
11
/**
12
 * @access private
13
 *
14
 * @author Addshore
15
 */
16
class ItemMerger {
17
18
	/**
19
	 * @var WikibaseApi
20
	 */
21
	private $api;
22
23
	/**
24
	 * @param WikibaseApi $api
25
	 */
26
	public function __construct( WikibaseApi $api ) {
27
		$this->api = $api;
28
	}
29
30
	/**
31
	 * @since 0.2
32
	 * @param Item|ItemId|string $from
33
	 * @param Item|ItemId|string $to
34
	 * @param EditInfo|null $editInfo
35
	 *
36
	 * @return bool
37
	 */
38
	public function merge( $from, $to, EditInfo $editInfo = null ) {
39
		$params = [
40
			'fromid' => $this->getIdFromInput( $from ),
41
			'toid' => $this->getIdFromInput( $to )
42
		];
43
44
		$this->api->postRequest( 'wbmergeitems', $params, $editInfo );
45
		return true;
46
	}
47
48
	/**
49
	 * @param Item|ItemId|string $input
50
	 *
51
	 * @throws InvalidArgumentException
52
	 * @return string the ItemId Serialization
53
	 */
54
	private function getIdFromInput( $input ) {
55
		if ( is_string( $input ) ) {
56
			return $input;
57
		} elseif ( $input instanceof ItemId ) {
58
			return $input->getSerialization();
59
		} elseif ( $input instanceof Item ) {
60
			return $input->getId()->getSerialization();
61
		} else {
62
			throw new InvalidArgumentException( 'Merge target should be either string, Item or ItemId' );
63
		}
64
	}
65
66
}
67