EntityPatcher::patchEntity()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Wikibase\DataModel\Services\Diff;
4
5
use InvalidArgumentException;
6
use RuntimeException;
7
use Wikibase\DataModel\Entity\EntityDocument;
8
9
/**
10
 * @since 1.0
11
 *
12
 * @license GPL-2.0-or-later
13
 * @author Jeroen De Dauw < [email protected] >
14
 * @author Christoph Fischer < [email protected] >
15
 */
16
class EntityPatcher {
17
18
	/**
19
	 * @var EntityPatcherStrategy[]
20
	 */
21
	private $patcherStrategies;
22
23 3
	public function __construct() {
24 3
		$this->registerEntityPatcherStrategy( new ItemPatcher() );
25 3
		$this->registerEntityPatcherStrategy( new PropertyPatcher() );
26 3
	}
27
28 3
	public function registerEntityPatcherStrategy( EntityPatcherStrategy $patcherStrategy ) {
29 3
		$this->patcherStrategies[] = $patcherStrategy;
30 3
	}
31
32
	/**
33
	 * @param EntityDocument $entity
34
	 * @param EntityDiff $patch
35
	 *
36
	 * @throws InvalidArgumentException
37
	 * @throws RuntimeException
38
	 */
39 3
	public function patchEntity( EntityDocument $entity, EntityDiff $patch ) {
40 3
		$this->getPatcherStrategy( $entity->getType() )->patchEntity( $entity, $patch );
41 2
	}
42
43
	/**
44
	 * @param string $entityType
45
	 *
46
	 * @throws RuntimeException
47
	 * @return EntityPatcherStrategy
48
	 */
49 3
	private function getPatcherStrategy( $entityType ) {
50 3
		foreach ( $this->patcherStrategies as $patcherStrategy ) {
51 3
			if ( $patcherStrategy->canPatchEntityType( $entityType ) ) {
52 2
				return $patcherStrategy;
53
			}
54 1
		}
55
56 1
		throw new RuntimeException( 'Patching the provided types of entities is not supported' );
57
	}
58
59
}
60