PropertyPatcher::patchProperty()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Wikibase\DataModel\Services\Diff;
4
5
use InvalidArgumentException;
6
use Wikibase\DataModel\Entity\EntityDocument;
7
use Wikibase\DataModel\Entity\Property;
8
use Wikibase\DataModel\Services\Diff\Internal\FingerprintPatcher;
9
10
/**
11
 * @since 1.0
12
 *
13
 * @license GPL-2.0-or-later
14
 * @author Jeroen De Dauw < [email protected] >
15
 */
16
class PropertyPatcher implements EntityPatcherStrategy {
17
18
	/**
19
	 * @var FingerprintPatcher
20
	 */
21
	private $fingerprintPatcher;
22
23
	/**
24
	 * @var StatementListPatcher
25
	 */
26
	private $statementListPatcher;
27
28
	public function __construct() {
29 4
		$this->fingerprintPatcher = new FingerprintPatcher();
30 4
		$this->statementListPatcher = new StatementListPatcher();
31 4
	}
32 4
33
	/**
34
	 * @param string $entityType
35
	 *
36
	 * @return bool
37
	 */
38
	public function canPatchEntityType( $entityType ) {
39 1
		return $entityType === 'property';
40 1
	}
41
42
	/**
43
	 * @param EntityDocument $entity
44
	 * @param EntityDiff $patch
45
	 *
46
	 * @throws InvalidArgumentException
47
	 */
48
	public function patchEntity( EntityDocument $entity, EntityDiff $patch ) {
49
		$property = $this->assertIsPropertyAndCast( $entity );
50 3
51 3
		$this->patchProperty( $property, $patch );
52
	}
53 2
54 2
	private function assertIsPropertyAndCast( EntityDocument $property ): Property {
55
		if ( !( $property instanceof Property ) ) {
56 3
			throw new InvalidArgumentException( '$property must be an instance of Property' );
57 3
		}
58 1
		return $property;
59
	}
60 2
61
	private function patchProperty( Property $property, EntityDiff $patch ) {
62 2
		$this->fingerprintPatcher->patchFingerprint( $property->getFingerprint(), $patch );
63 2
64
		$this->statementListPatcher->patchStatementList(
65 2
			$property->getStatements(),
66 2
			$patch->getClaimsDiff()
67 2
		);
68 2
	}
69 2
70
}
71