EntityTypeAwareDiffOpFactory   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 93.33%

Importance

Changes 0
Metric Value
eloc 13
c 0
b 0
f 0
dl 0
loc 40
ccs 14
cts 15
cp 0.9333
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A createOperations() 0 8 2
A newFromArray() 0 15 2
1
<?php
2
3
namespace Wikibase\DataModel\Services\Diff;
4
5
use Diff\DiffOp\DiffOp;
6
use Diff\DiffOpFactory;
7
use InvalidArgumentException;
8
9
/**
10
 * Class for changes that can be represented as a Diff.
11
 *
12
 * @since 1.2
13
 *
14
 * @license GPL-2.0-or-later
15
 * @author Daniel Kinzler
16
 */
17
class EntityTypeAwareDiffOpFactory extends DiffOpFactory {
18
19
	/**
20
	 * @param array $diffOp
21
	 *
22
	 * @return DiffOp
23
	 * @throws InvalidArgumentException
24
	 */
25 3
	public function newFromArray( array $diffOp ) {
26 3
		$this->assertHasKey( 'type', $diffOp );
27
28
		// see EntityDiff::getType() and ItemDiff::getType()
29 2
		if ( preg_match( '!^diff/(.*)$!', $diffOp['type'], $matches ) ) {
30 1
			$itemType = $matches[1];
31 1
			$this->assertHasKey( 'operations', $diffOp );
32
33 1
			$operations = $this->createOperations( $diffOp['operations'] );
34 1
			$diff = EntityDiff::newForType( $itemType, $operations );
35
36 1
			return $diff;
37
		}
38
39 1
		return parent::newFromArray( $diffOp );
40
	}
41
42
	/**
43
	 * Converts a list of diff operations represented by arrays into a list of
44
	 * DiffOp objects.
45
	 *
46
	 * @param array $data the input data
47
	 * @return DiffOp[] The diff ops
48
	 */
49 1
	private function createOperations( array $data ) {
50 1
		$operations = [];
51
52 1
		foreach ( $data as $key => $operation ) {
53
			$operations[$key] = $this->newFromArray( $operation );
54 1
		}
55
56 1
		return $operations;
57
	}
58
59
}
60