Completed
Push — master ( f3f299...16dfc5 )
by
unknown
05:26 queued 02:39
created

StatementListPatcher::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
cc 1
eloc 5
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Wikibase\DataModel\Services\Diff\Internal;
4
5
use Diff\DiffOp\Diff\Diff;
6
use Diff\DiffOp\DiffOp;
7
use Diff\DiffOp\DiffOpAdd;
8
use Diff\DiffOp\DiffOpChange;
9
use Diff\DiffOp\DiffOpRemove;
10
use Diff\Patcher\PatcherException;
11
use InvalidArgumentException;
12
use Wikibase\DataModel\Statement\Statement;
13
use Wikibase\DataModel\Statement\StatementList;
14
15
/**
16
 * TODO: Class must be public.
17
 * TODO: Should this support actual edit conflict detection?
18
 *
19
 * Package private.
20
 *
21
 * @since 1.0
22
 *
23
 * @license GPL-2.0+
24
 * @author Jeroen De Dauw < [email protected] >
25
 * @author Thiemo Mättig
26
 */
27 2
class StatementListPatcher {
28 2
29
	/**
30 2
	 * @since 3.6
31 1
	 *
32 1
	 * @param StatementList $statements
33
	 * @param Diff $patch
34 2
	 *
35 2
	 * @throws PatcherException
36
	 */
37
	public function patchStatementList( StatementList $statements, Diff $patch ) {
38
		/** @var DiffOp $diffOp */
39
		foreach ( $patch as $diffOp ) {
40
			switch ( true ) {
41
				case $diffOp instanceof DiffOpAdd:
42
					/** @var DiffOpAdd $diffOp */
43
					$statements->addStatement( $diffOp->getNewValue() );
44 2
					break;
45 2
46
				case $diffOp instanceof DiffOpChange:
47
					/** @var DiffOpChange $diffOp */
48
					/** @var Statement $statement */
49
					$statement = $diffOp->getOldValue();
50 2
					$statements->removeStatementsWithGuid( $statement->getGuid() );
51 1
					$statements->addStatement( $diffOp->getNewValue() );
52 2
					break;
53
54 2
				case $diffOp instanceof DiffOpRemove:
55
					/** @var DiffOpRemove $diffOp */
56 2
					/** @var Statement $statement */
57 1
					$statement = $diffOp->getOldValue();
58 2
					$statements->removeStatementsWithGuid( $statement->getGuid() );
59
					break;
60 2
61
				default:
62
					throw new PatcherException( 'Invalid statement list diff' );
63
			}
64
		}
65
	}
66
67
	/**
68
	 * @deprecated since 3.6, use patchStatementList instead
69
	 *
70
	 * @param StatementList $statements
71
	 * @param Diff $patch
72
	 *
73
	 * @throws InvalidArgumentException
74
	 * @return StatementList
75
	 */
76
	public function getPatchedStatementList( StatementList $statements, Diff $patch ) {
77
		$patched = clone $statements;
78
		$this->patchStatementList( $patched, $patch );
79
		return $patched;
80
	}
81
82
}
83