Completed
Push — master ( 8cfcb0...8d42b2 )
by adam
02:02
created

src/Api/Service/AliasGroupSetter.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Wikibase\Api\Service;
4
5
use Mediawiki\Api\MediawikiApi;
6
use Mediawiki\Api\SimpleRequest;
7
use Mediawiki\DataModel\EditInfo;
8
use UnexpectedValueException;
9
use Wikibase\Api\WikibaseApi;
10
use Wikibase\DataModel\Entity\EntityId;
11
use Wikibase\DataModel\Entity\Item;
12
use Wikibase\DataModel\Entity\Property;
13
use Wikibase\DataModel\SiteLink;
14
use Wikibase\DataModel\Term\AliasGroup;
15
16
/**
17
 * @access private
18
 *
19
 * @author Adam Shorland
20
 */
21
class AliasGroupSetter {
22
23
	/**
24
	 * @var WikibaseApi
25
	 */
26
	private $api;
27
28
	/**
29
	 * @param WikibaseApi $api
30
	 */
31
	public function __construct( WikibaseApi $api ) {
32
		$this->api = $api;
33
	}
34
35
	/**
36
	 * @since 0.2
37
	 *
38
	 * @param AliasGroup $aliasGroup
39
	 * @param EntityId|Item|Property|SiteLink $target
40
	 * @param EditInfo|null $editInfo
41
	 *
42
	 * @return bool
43
	 */
44
	public function set( AliasGroup $aliasGroup, $target, EditInfo $editInfo = null ) {
45
		$this->throwExceptionsOnBadTarget( $target );
46
47
		$params = $this->getTargetParamsFromTarget(
48
			$this->getEntityIdentifierFromTarget( $target )
0 ignored issues
show
It seems like $target defined by parameter $target on line 44 can also be of type object<Wikibase\DataModel\SiteLink>; however, Wikibase\Api\Service\Ali...yIdentifierFromTarget() does only seem to accept object<Wikibase\DataMode...aModel\Entity\Property>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
49
		);
50
51
		$params['language'] = $aliasGroup->getLanguageCode();
52
		$params['set'] = implode( '|', $aliasGroup->getAliases() );
53
54
		$this->api->postRequest( 'wbsetaliases', $params, $editInfo );
55
		return true;
56
	}
57
58
	/**
59
	 * @param mixed $target
60
	 *
61
	 * @throws UnexpectedValueException
62
	 *
63
	 * @todo Fix duplicated code
64
	 */
65
	private function throwExceptionsOnBadTarget( $target ) {
66
		if( !$target instanceof EntityId && !$target instanceof Item && !$target instanceof Property && ! $target instanceof SiteLink ) {
67
			throw new UnexpectedValueException( '$target needs to be an EntityId, Item, Property or SiteLink' );
68
		}
69
		if( ( $target instanceof Item || $target instanceof Property ) && is_null( $target->getId() ) ) {
70
			throw new UnexpectedValueException( '$target Entity object needs to have an Id set' );
71
		}
72
	}
73
74
	/**
75
	 * @param EntityId|Item|Property $target
76
	 *
77
	 * @throws UnexpectedValueException
78
	 * @return EntityId|SiteLink
79
	 *
80
	 * @todo Fix duplicated code
81
	 */
82
	private function getEntityIdentifierFromTarget( $target ) {
83
		if ( $target instanceof Item || $target instanceof Property ) {
84
			return $target->getId();
85
		} else {
86
			return $target;
87
		}
88
	}
89
90
	/**
91
	 * @param EntityId|SiteLink $target
92
	 *
93
	 * @throws UnexpectedValueException
94
	 * @return array
95
	 *
96
	 * @todo Fix duplicated code
97
	 */
98
	private function getTargetParamsFromTarget( $target ) {
99
		if( $target instanceof EntityId ) {
100
			return array( 'id' => $target->getSerialization() );
101
		} elseif( $target instanceof SiteLink ) {
102
			return array(
103
				'site' => $target->getSiteId(),
104
				'title' => $target->getPageName(),
105
			);
106
		} else {
107
			throw new UnexpectedValueException( '$target needs to be an EntityId or SiteLink' );
108
		}
109
	}
110
111
}