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

FingerprintPatcher   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 8
c 2
b 0
f 1
lcom 1
cbo 5
dl 0
loc 51
ccs 23
cts 23
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A patchFingerprint() 0 6 1
A patchTermList() 0 12 3
A patchAliasGroupList() 0 12 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