ItemDiffer::getSiteLinksInDiffFormat()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2.0023

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 16
ccs 11
cts 12
cp 0.9167
crap 2.0023
rs 9.9666
1
<?php
2
3
namespace Wikibase\DataModel\Services\Diff;
4
5
use Diff\Differ\MapDiffer;
6
use InvalidArgumentException;
7
use Wikibase\DataModel\Entity\EntityDocument;
8
use Wikibase\DataModel\Entity\Item;
9
use Wikibase\DataModel\Entity\ItemId;
10
use Wikibase\DataModel\SiteLinkList;
11
12
/**
13
 * @since 1.0
14
 *
15
 * @license GPL-2.0-or-later
16
 * @author Jeroen De Dauw < [email protected] >
17
 */
18
class ItemDiffer implements EntityDifferStrategy {
19
20
	/**
21
	 * @var MapDiffer
22
	 */
23
	private $recursiveMapDiffer;
24
25
	/**
26
	 * @var StatementListDiffer
27
	 */
28
	private $statementListDiffer;
29
30
	public function __construct() {
31 6
		$this->recursiveMapDiffer = new MapDiffer( true );
32 6
		$this->statementListDiffer = new StatementListDiffer();
33 6
	}
34 6
35
	/**
36
	 * @param string $entityType
37
	 *
38
	 * @return bool
39
	 */
40
	public function canDiffEntityType( $entityType ) {
41
		return $entityType === 'item';
42
	}
43
44
	/**
45
	 * @param EntityDocument $from
46
	 * @param EntityDocument $to
47
	 *
48
	 * @return ItemDiff
49
	 * @throws InvalidArgumentException
50
	 */
51
	public function diffEntities( EntityDocument $from, EntityDocument $to ) {
52 4
		$fromItem = $this->assertIsItemAndCast( $from );
53 4
		$toItem = $this->assertIsItemAndCast( $to );
54 4
55
		return $this->diffItems( $fromItem, $toItem );
56 4
	}
57
58
	private function assertIsItemAndCast( EntityDocument $item ): Item {
59 4
		if ( !( $item instanceof Item ) ) {
60 4
			throw new InvalidArgumentException( '$item must be an instance of Item' );
61
		}
62
		return $item;
63 4
	}
64
65 6
	public function diffItems( Item $from, Item $to ): ItemDiff {
66 6
		$diffOps = $this->recursiveMapDiffer->doDiff(
67 6
			$this->toDiffArray( $from ),
68 6
			$this->toDiffArray( $to )
69 6
		);
70
71 6
		$diffOps['claim'] = $this->statementListDiffer->getDiff( $from->getStatements(), $to->getStatements() );
72
73 6
		return new ItemDiff( $diffOps );
74
	}
75
76 6
	private function toDiffArray( Item $item ): array {
77 6
		$array = [];
78
79 6
		$array['aliases'] = $item->getAliasGroups()->toTextArray();
80 6
		$array['label'] = $item->getLabels()->toTextArray();
81 6
		$array['description'] = $item->getDescriptions()->toTextArray();
82 6
		$array['links'] = $this->getSiteLinksInDiffFormat( $item->getSiteLinkList() );
83
84 6
		return $array;
85
	}
86
87 6
	private function getSiteLinksInDiffFormat( SiteLinkList $siteLinks ): array {
88 6
		$linksInDiffFormat = [];
89
90
		foreach ( $siteLinks->toArray() as $siteLink ) {
91
			$linksInDiffFormat[$siteLink->getSiteId()] = [
92
				'name' => $siteLink->getPageName(),
93 6
				'badges' => array_map(
94 1
					static function( ItemId $id ) {
95 1
						return $id->getSerialization();
96 1
					},
97 1
					$siteLink->getBadges()
98
				),
99 1
			];
100 1
		}
101 1
102 1
		return $linksInDiffFormat;
103 6
	}
104
105 6
	/**
106
	 * @param EntityDocument $entity
107
	 *
108
	 * @return ItemDiff
109
	 * @throws InvalidArgumentException
110
	 */
111
	public function getConstructionDiff( EntityDocument $entity ) {
112
		$item = $this->assertIsItemAndCast( $entity );
113
		return $this->diffEntities( new Item(), $item );
114 2
	}
115 2
116 2
	/**
117
	 * @param EntityDocument $entity
118
	 *
119
	 * @return ItemDiff
120
	 * @throws InvalidArgumentException
121
	 */
122
	public function getDestructionDiff( EntityDocument $entity ) {
123
		$item = $this->assertIsItemAndCast( $entity );
124
		return $this->diffEntities( $item, new Item() );
125 1
	}
126 1
127
}
128