Completed
Push — master ( 7f838a...c1c400 )
by Bene
02:56
created

FingerprintPatcher::patchTermList()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
eloc 7
nc 4
nop 2
crap 3
1
<?php
2
3
namespace Wikibase\DataModel\Services\Diff\Internal;
4
5
use Diff\DiffOp\Diff\Diff;
6
use Diff\Patcher\MapPatcher;
7
use InvalidArgumentException;
8
use Wikibase\DataModel\Services\Diff\EntityDiff;
9
use Wikibase\DataModel\Term\AliasGroupList;
10
use Wikibase\DataModel\Term\Fingerprint;
11
use Wikibase\DataModel\Term\TermList;
12
13
/**
14
 * Package private.
15
 *
16
 * @licence GNU GPL v2+
17
 * @author Jeroen De Dauw < [email protected] >
18
 * @author Thiemo Mättig
19
 */
20
class FingerprintPatcher {
21
22
	/**
23
	 * @var MapPatcher
24
	 */
25
	private $patcher;
26
27 4
	public function __construct() {
28 4
		$this->patcher = new MapPatcher();
29 4
	}
30
31
	/**
32
	 * @param Fingerprint $fingerprint
33
	 * @param EntityDiff $patch
34
	 *
35
	 * @throws InvalidArgumentException
36
	 */
37 4
	public function patchFingerprint( Fingerprint $fingerprint, EntityDiff $patch ) {
38 4
		$this->patchTermList( $fingerprint->getLabels(), $patch->getLabelsDiff() );
39 4
		$this->patchTermList( $fingerprint->getDescriptions(), $patch->getDescriptionsDiff() );
40 4
41 4
		$this->patchAliasGroupList( $fingerprint->getAliasGroups(), $patch->getAliasesDiff() );
42
	}
43 4
44
	private function patchTermList( TermList $terms, Diff $patch ) {
45 4
		$original = $terms->toTextArray();
46 4
		$patched = $this->patcher->patch( $original, $patch );
47 4
48 4
		foreach ( $patched as $languageCode => $text ) {
49
			$terms->setTextForLanguage( $languageCode, $text );
50 4
		}
51
52 4
		foreach ( array_diff_key( $original, $patched ) as $languageCode => $text ) {
53 4
			$terms->removeByLanguage( $languageCode );
54
		}
55
	}
56
57
	private function patchAliasGroupList( AliasGroupList $groups, Diff $patch ) {
58
		$original = $groups->toTextArray();
59
		$patched = $this->patcher->patch( $original, $patch );
60 4
61 4
		foreach ( $patched as $languageCode => $aliases ) {
62
			$groups->setAliasesForLanguage( $languageCode, $aliases );
63 4
		}
64 4
65 4
		foreach ( array_diff_key( $original, $patched ) as $languageCode => $aliases ) {
66
			$groups->removeByLanguage( $languageCode );
67 4
		}
68
	}
69
70
}
71