ReferenceSetter::set()   B
last analyzed

Complexity

Conditions 8
Paths 19

Size

Total Lines 35

Duplication

Lines 9
Ratio 25.71 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
dl 9
loc 35
ccs 0
cts 26
cp 0
rs 8.1155
c 0
b 0
f 0
cc 8
nc 19
nop 4
crap 72
1
<?php
2
3
namespace Wikibase\Api\Service;
4
5
use Mediawiki\DataModel\EditInfo;
6
use Serializers\Serializer;
7
use UnexpectedValueException;
8
use Wikibase\Api\WikibaseApi;
9
use Wikibase\DataModel\Reference;
10
use Wikibase\DataModel\Statement\Statement;
11
use Wikibase\DataModel\Statement\StatementGuid;
12
13
/**
14
 * @access private
15
 *
16
 * @author Addshore
17
 */
18
class ReferenceSetter {
19
20
	/**
21
	 * @var WikibaseApi
22
	 */
23
	private $api;
24
25
	/**
26
	 * @var Serializer
27
	 */
28
	private $referenceSerializer;
29
30
	/**
31
	 * @param WikibaseApi $api
32
	 * @param Serializer $referenceSerializer
33
	 */
34
	public function __construct( WikibaseApi $api, Serializer $referenceSerializer ) {
35
		$this->api = $api;
36
		$this->referenceSerializer = $referenceSerializer;
37
	}
38
39
	/**
40
	 * @since 0.2
41
	 *
42
	 * @param Reference $reference new reference value
43
	 * @param Statement|StatementGuid|string $statement Statement object or GUID which has the reference
44
	 * @param Reference|string|null $targetReference target (old) reference of hash
45
	 * @param EditInfo|null $editInfo
46
	 *
47
	 * @return bool
48
	 * @throws UnexpectedValueException
49
	 */
50
	public function set( Reference $reference, $statement, $targetReference = null, EditInfo $editInfo = null ) {
51 View Code Duplication
		if ( is_string( $statement ) ) {
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...
52
			$guid = $statement;
53
		} elseif ( $statement instanceof StatementGuid ) {
54
			$guid = $statement->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...
55
		} elseif ( $statement instanceof Statement ) {
56
			$guid = $statement->getGuid();
57
		} else {
58
			throw new UnexpectedValueException( 'Could not get statement guid from $statement' );
59
		}
60
		if ( !is_string( $guid ) ) {
61
			throw new UnexpectedValueException( 'Unexpected statement guid got from $statement' );
62
		}
63
64
		$referenceSerialization = $this->referenceSerializer->serialize( $reference );
65
66
		$params = [
67
			'statement' => $guid,
68
			'snaks' => json_encode( $referenceSerialization['snaks'] ),
69
			'snaks-order' => json_encode( $referenceSerialization['snaks-order'] ),
70
		];
71
72
		if ( $targetReference !== null ) {
73
			if ( $targetReference instanceof Reference ) {
74
				$targetReference = $reference->getHash();
75
			}
76
			if ( !is_string( $targetReference ) ) {
77
				throw new UnexpectedValueException( 'Could not get reference hash from $targetReference' );
78
			}
79
			$params['reference'] = $targetReference;
80
		}
81
82
		$this->api->postRequest( 'wbsetreference', $params, $editInfo );
83
		return true;
84
	}
85
86
}
87