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

AliasGroupSetter::throwExceptionsOnBadTarget()   B

Complexity

Conditions 8
Paths 3

Size

Total Lines 8

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
dl 8
loc 8
rs 8.4444
c 0
b 0
f 0
cc 8
nc 3
nop 1
1
<?php
2
3
namespace Addwiki\Wikibase\Api\Service;
4
5
use Addwiki\Mediawiki\DataModel\EditInfo;
6
use Addwiki\Wikibase\Api\WikibaseApi;
7
use UnexpectedValueException;
8
use Wikibase\DataModel\Entity\EntityId;
9
use Wikibase\DataModel\Entity\Item;
10
use Wikibase\DataModel\Entity\Property;
11
use Wikibase\DataModel\SiteLink;
12
use Wikibase\DataModel\Term\AliasGroup;
13
14
/**
15
 * @access private
16
 *
17
 * @author Addshore
18
 */
19
class AliasGroupSetter {
20
21
	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...
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
	 *
33
	 * @param AliasGroup $aliasGroup
34
	 * @param EntityId|Item|Property|SiteLink $target
35
	 * @param EditInfo|null $editInfo
36
	 */
37
	public function set( AliasGroup $aliasGroup, $target, EditInfo $editInfo = null ): bool {
38
		$this->throwExceptionsOnBadTarget( $target );
39
40
		$params = $this->getTargetParamsFromTarget(
41
			$this->getEntityIdentifierFromTarget( $target )
42
		);
43
44
		$params['language'] = $aliasGroup->getLanguageCode();
45
		$params['set'] = implode( '|', $aliasGroup->getAliases() );
46
47
		$this->api->postRequest( 'wbsetaliases', $params, $editInfo );
48
		return true;
49
	}
50
51
	/**
52
	 * @param mixed $target
53
	 *
54
	 * @throws UnexpectedValueException
55
	 *
56
	 * @todo Fix duplicated code
57
	 */
58
	private function throwExceptionsOnBadTarget( $target ): void {
59
		if ( !$target instanceof EntityId && !$target instanceof Item && !$target instanceof Property && !$target instanceof SiteLink ) {
60
			throw new UnexpectedValueException( '$target needs to be an EntityId, Item, Property or SiteLink' );
61
		}
62
		if ( ( $target instanceof Item || $target instanceof Property ) && $target->getId() === null ) {
63
			throw new UnexpectedValueException( '$target Entity object needs to have an Id set' );
64
		}
65
	}
66
67
	/**
68
	 * @param EntityId|Item|Property $target
69
	 *
70
	 * @throws UnexpectedValueException
71
	 * @return EntityId|SiteLink
72
	 *
73
	 * @todo Fix duplicated code
74
	 */
75
	private function getEntityIdentifierFromTarget( $target ) {
76
		if ( $target instanceof Item || $target instanceof Property ) {
77
			return $target->getId();
78
		} else {
79
			return $target;
80
		}
81
	}
82
83
	/**
84
	 * @param EntityId|SiteLink $target
85
	 *
86
	 * @throws UnexpectedValueException
87
	 * @return array
88
	 *
89
	 * @todo Fix duplicated code
90
	 */
91
	private function getTargetParamsFromTarget( $target ) {
92
		if ( $target instanceof EntityId ) {
93
			return [ 'id' => $target->getSerialization() ];
94
		} elseif ( $target instanceof SiteLink ) {
95
			return [
96
				'site' => $target->getSiteId(),
97
				'title' => $target->getPageName(),
98
			];
99
		} else {
100
			throw new UnexpectedValueException( '$target needs to be an EntityId or SiteLink' );
101
		}
102
	}
103
104
}
105