Completed
Push — main ( b55b92...e8d442 )
by
unknown
05:41
created

ItemMerger   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 51
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Addwiki\Wikibase\Api\Service;
4
5
use Addwiki\Mediawiki\DataModel\EditInfo;
6
use Addwiki\Wikibase\Api\WikibaseApi;
7
use InvalidArgumentException;
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
	private WikibaseApi $api;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
19
20
	/**
21
	 * @param WikibaseApi $api
22
	 */
23
	public function __construct( WikibaseApi $api ) {
24
		$this->api = $api;
25
	}
26
27
	/**
28
	 * @since 0.2
29
	 * @param Item|ItemId|string $from
30
	 * @param Item|ItemId|string $to
31
	 * @param EditInfo|null $editInfo
32
	 */
33
	public function merge( $from, $to, EditInfo $editInfo = null ): bool {
34
		$params = [
35
			'fromid' => $this->getIdFromInput( $from ),
36
			'toid' => $this->getIdFromInput( $to )
37
		];
38
39
		$this->api->postRequest( 'wbmergeitems', $params, $editInfo );
40
		return true;
41
	}
42
43
	/**
44
	 * @param Item|ItemId|string $input
45
	 *
46
	 * @throws InvalidArgumentException
47
	 * @return string|void the ItemId Serialization
48
	 */
49
	private function getIdFromInput( $input ) {
50
		if ( is_string( $input ) ) {
51
			return $input;
52
		} elseif ( $input instanceof ItemId ) {
53
			return $input->getSerialization();
54
		} elseif ( $input instanceof Item ) {
55
			return $input->getId()->getSerialization();
56
		} else {
57
			throw new InvalidArgumentException( 'Merge target should be either string, Item or ItemId' );
58
		}
59
	}
60
61
}
62