ItemPatcher   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 64
ccs 25
cts 25
cp 1
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A patchItem() 0 13 2
A assertIsItemAndCast() 0 5 2
A canPatchEntityType() 0 2 1
A __construct() 0 4 1
A patchEntity() 0 4 1
1
<?php
2
3
namespace Wikibase\DataModel\Services\Diff;
4
5
use InvalidArgumentException;
6
use Wikibase\DataModel\Entity\EntityDocument;
7
use Wikibase\DataModel\Entity\Item;
8
use Wikibase\DataModel\Services\Diff\Internal\FingerprintPatcher;
9
use Wikibase\DataModel\Services\Diff\Internal\SiteLinkListPatcher;
10
11
/**
12
 * @since 1.0
13
 *
14
 * @license GPL-2.0-or-later
15
 * @author Jeroen De Dauw < [email protected] >
16
 */
17
class ItemPatcher implements EntityPatcherStrategy {
18
19
	/**
20
	 * @var FingerprintPatcher
21
	 */
22
	private $fingerprintPatcher;
23
24
	/**
25
	 * @var StatementListPatcher
26
	 */
27
	private $statementListPatcher;
28
29
	/**
30
	 * @var SiteLinkListPatcher
31
	 */
32
	private $siteLinkListPatcher;
33
34
	public function __construct() {
35 4
		$this->fingerprintPatcher = new FingerprintPatcher();
36 4
		$this->statementListPatcher = new StatementListPatcher();
37 4
		$this->siteLinkListPatcher = new SiteLinkListPatcher();
38 4
	}
39 4
40
	/**
41
	 * @param string $entityType
42
	 *
43
	 * @return bool
44
	 */
45
	public function canPatchEntityType( $entityType ) {
46 1
		return $entityType === 'item';
47 1
	}
48
49
	/**
50
	 * @param EntityDocument $entity
51
	 * @param EntityDiff $patch
52
	 *
53
	 * @throws InvalidArgumentException
54
	 */
55
	public function patchEntity( EntityDocument $entity, EntityDiff $patch ) {
56
		$item = $this->assertIsItemAndCast( $entity );
57 3
58 3
		$this->patchItem( $item, $patch );
59
	}
60 2
61 2
	private function assertIsItemAndCast( EntityDocument $item ): Item {
62
		if ( !( $item instanceof Item ) ) {
63 3
			throw new InvalidArgumentException( '$item must be an instance of Item' );
64 3
		}
65 1
		return $item;
66
	}
67 2
68
	private function patchItem( Item $item, EntityDiff $patch ) {
69 2
		$this->fingerprintPatcher->patchFingerprint( $item->getFingerprint(), $patch );
70 2
71
		if ( $patch instanceof ItemDiff ) {
72 2
			$item->setSiteLinkList( $this->siteLinkListPatcher->getPatchedSiteLinkList(
73 2
				$item->getSiteLinkList(),
74 2
				$patch->getSiteLinkDiff()
75 2
			) );
76 2
		}
77 2
78
		$this->statementListPatcher->patchStatementList(
79 2
			$item->getStatements(),
80 2
			$patch->getClaimsDiff()
81 2
		);
82 2
	}
83 2
84
}
85