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

SiteLinkSetter   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 91
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 3
dl 91
loc 91
rs 10
c 0
b 0
f 0

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
13
/**
14
 * @access private
15
 *
16
 * @author Addshore
17
 */
18
class SiteLinkSetter {
19
20
	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...
21
22
	/**
23
	 * @param WikibaseApi $api
24
	 */
25
	public function __construct( WikibaseApi $api ) {
26
		$this->api = $api;
27
	}
28
29
	/**
30
	 * @since 0.2
31
	 *
32
	 * @param SiteLink $siteLink
33
	 * @param EntityId|Item|Property|SiteLink $target
34
	 * @param EditInfo|null $editInfo
35
	 */
36
	public function set( SiteLink $siteLink, $target, EditInfo $editInfo = null ): bool {
37
		$this->throwExceptionsOnBadTarget( $target );
38
39
		$params = $this->getTargetParamsFromTarget(
40
			$this->getEntityIdentifierFromTarget( $target )
41
		);
42
43
		$params['linksite'] = $siteLink->getSiteId();
44
		$params['linktitle'] = $siteLink->getPageName();
45
		$params['badges'] = implode( '|', $siteLink->getBadges() );
46
47
		$this->api->postRequest( 'wbsetsitelink', $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