ReferenceRemover::set()   B
last analyzed

Complexity

Conditions 7
Paths 16

Size

Total Lines 29

Duplication

Lines 9
Ratio 31.03 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
dl 9
loc 29
ccs 0
cts 21
cp 0
rs 8.5226
c 0
b 0
f 0
cc 7
nc 16
nop 3
crap 56
1
<?php
2
3
namespace Wikibase\Api\Service;
4
5
use Mediawiki\DataModel\EditInfo;
6
use UnexpectedValueException;
7
use Wikibase\Api\WikibaseApi;
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
	/**
20
	 * @var WikibaseApi
21
	 */
22
	private $api;
23
24
	/**
25
	 * @param WikibaseApi $api
26
	 */
27
	public function __construct( WikibaseApi $api ) {
28
		$this->api = $api;
29
	}
30
31
	/**
32
	 * @since 0.2
33
	 *
34
	 * @param Reference|string $reference Reference object or hash
35
	 * @param Statement|StatementGuid|string $target Statement object or GUID
36
	 * @param EditInfo|null $editInfo
37
	 *
38
	 * @throws UnexpectedValueException
39
	 * @return bool
40
	 */
41
	public function set( $reference, $target, EditInfo $editInfo = null ) {
42
		if ( $reference instanceof Reference ) {
43
			$reference = $reference->getHash();
44
		}
45
		if ( !is_string( $reference ) ) {
46
			throw new UnexpectedValueException( 'Could not get reference hash from $reference' );
47
		}
48
49 View Code Duplication
		if ( is_string( $target ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
			$guid = $target;
51
		} elseif ( $target instanceof StatementGuid ) {
52
			$guid = $target->getSerialization();
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\State...uid::getSerialization() has been deprecated with message: The value returned by this method might differ in case from the original, unparsed statement GUID
(the entity ID part might have been lowercase originally, but is always normalized in the return value here),
which means that the value should not be compared to other statement GUID serializations,
e.g. to look up a statement in a StatementList.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
53
		} elseif ( $target instanceof Statement ) {
54
			$guid = $target->getGuid();
55
		} else {
56
			throw new UnexpectedValueException( 'Could not get statement guid from $target' );
57
		}
58
		if ( !is_string( $guid ) ) {
59
			throw new UnexpectedValueException( 'Unexpected statement guid got from $target' );
60
		}
61
62
		$params = [
63
			'statement' => $guid,
64
			'references' => $reference,
65
		];
66
67
		$this->api->postRequest( 'wbremovereferences', $params, $editInfo );
68
		return true;
69
	}
70
71
}
72