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

ReferenceRemover::set()   B

Complexity

Conditions 7
Paths 16

Size

Total Lines 29

Duplication

Lines 9
Ratio 31.03 %

Importance

Changes 0
Metric Value
dl 9
loc 29
rs 8.5226
c 0
b 0
f 0
cc 7
nc 16
nop 3
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\Reference;
9
use Wikibase\DataModel\Statement\Statement;
10
use Wikibase\DataModel\Statement\StatementGuid;
11
12
/**
13
 * @access private
14
 *
15
 * @author Addshore
16
 */
17
class ReferenceRemover {
18
19
	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...
20
21
	/**
22
	 * @param WikibaseApi $api
23
	 */
24
	public function __construct( WikibaseApi $api ) {
25
		$this->api = $api;
26
	}
27
28
	/**
29
	 * @since 0.2
30
	 *
31
	 * @param Reference|string $reference Reference object or hash
32
	 * @param Statement|StatementGuid|string $target Statement object or GUID
33
	 * @param EditInfo|null $editInfo
34
	 *
35
	 * @throws UnexpectedValueException
36
	 */
37
	public function set( $reference, $target, EditInfo $editInfo = null ): bool {
38
		if ( $reference instanceof Reference ) {
39
			$reference = $reference->getHash();
40
		}
41
		if ( !is_string( $reference ) ) {
42
			throw new UnexpectedValueException( 'Could not get reference hash from $reference' );
43
		}
44
45
		if ( is_string( $target ) ) {
46
			$guid = $target;
47
		} elseif ( $target instanceof StatementGuid ) {
48
			$guid = $target->getSerialization();
49
		} elseif ( $target instanceof Statement ) {
50
			$guid = $target->getGuid();
51
		} else {
52
			throw new UnexpectedValueException( 'Could not get statement guid from $target' );
53
		}
54
		if ( !is_string( $guid ) ) {
55
			throw new UnexpectedValueException( 'Unexpected statement guid got from $target' );
56
		}
57
58
		$params = [
59
			'statement' => $guid,
60
			'references' => $reference,
61
		];
62
63
		$this->api->postRequest( 'wbremovereferences', $params, $editInfo );
64
		return true;
65
	}
66
67
}
68