tryNewSiteLinkFromSerialization()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Wikibase\InternalSerialization\Deserializers;
4
5
use Deserializers\Deserializer;
6
use Deserializers\Exceptions\DeserializationException;
7
use Deserializers\Exceptions\MissingAttributeException;
8
use InvalidArgumentException;
9
use Wikibase\DataModel\Entity\ItemId;
10
use Wikibase\DataModel\SiteLink;
11
use Wikibase\DataModel\SiteLinkList;
12
13
/**
14
 * @license GPL-2.0-or-later
15
 * @author Jeroen De Dauw < [email protected] >
16
 */
17
class LegacySiteLinkListDeserializer implements Deserializer {
18
19
	/**
20
	 * @param array $serialization
21
	 *
22
	 * @return SiteLink[]
23
	 * @throws DeserializationException
24
	 */
25 18
	public function deserialize( $serialization ) {
26 18
		$this->assertStructureIsValid( $serialization );
27
28 9
		return $this->getDeserialized( $serialization );
29
	}
30
31 18
	private function assertStructureIsValid( $serialization ) {
32 18
		if ( !is_array( $serialization ) ) {
33 3
			throw new DeserializationException( 'SiteLink list serializations should be arrays' );
34
		}
35
36 15
		foreach ( $serialization as $key => $arrayElement ) {
37 13
			$this->assertKeyIsValid( $key );
38 12
			$this->assertElementIsValid( $arrayElement );
39
		}
40 9
	}
41
42 13
	private function assertKeyIsValid( $key ) {
43 13
		if ( !is_string( $key ) ) {
44 1
			throw new DeserializationException( 'All array keys should be strings' );
45
		}
46 12
	}
47
48 12
	private function assertElementIsValid( $arrayElement ) {
49 12
		if ( !is_string( $arrayElement ) && !is_array( $arrayElement ) ) {
50 1
			throw new DeserializationException( 'All array elements should be of type string or array' );
51
		}
52
53 11
		if ( is_array( $arrayElement ) ) {
54 9
			$this->assertElementIsValidArray( $arrayElement );
55
		}
56 7
	}
57
58 9
	private function assertElementIsValidArray( array $arrayElement ) {
59 9
		if ( !array_key_exists( 'name', $arrayElement ) ) {
60 3
			throw new MissingAttributeException( 'name' );
61
		}
62
63 6
		if ( !array_key_exists( 'badges', $arrayElement ) ) {
64 1
			throw new MissingAttributeException( 'badges' );
65
		}
66 5
	}
67
68
	/**
69
	 * @param array $siteLinkArray
70
	 *
71
	 * @return SiteLinkList
72
	 */
73 9
	private function getDeserialized( array $siteLinkArray ) {
74 9
		$siteLinks = array();
75
76 9
		foreach ( $siteLinkArray as $siteId => $siteLinkData ) {
77 7
			$siteLinks[] = $this->newSiteLinkFromSerialization( $siteId, $siteLinkData );
78
		}
79
80 7
		return new SiteLinkList( $siteLinks );
81
	}
82
83
	/**
84
	 * @param string $siteId
85
	 * @param string|array $siteLinkData
86
	 *
87
	 * @throws DeserializationException
88
	 * @return SiteLink
89
	 */
90 7
	private function newSiteLinkFromSerialization( $siteId, $siteLinkData ) {
91
		try {
92 7
			return $this->tryNewSiteLinkFromSerialization( $siteId, $siteLinkData );
93 2
		} catch ( InvalidArgumentException $ex ) {
94 2
			throw new DeserializationException( $ex->getMessage(), $ex );
95
		}
96
	}
97
98
	/**
99
	 * @param string $siteId
100
	 * @param string|array $siteLinkData
101
	 *
102
	 * @return SiteLink
103
	 */
104 7
	private function tryNewSiteLinkFromSerialization( $siteId, $siteLinkData ) {
105 7
		if ( is_array( $siteLinkData ) ) {
106 5
			$pageName = $siteLinkData['name'];
107 5
			$badges = $this->getDeserializedBadges( $siteLinkData['badges'] );
108
		} else {
109 3
			$pageName = $siteLinkData;
110 3
			$badges = array();
111
		}
112
113 5
		return new SiteLink( $siteId, $pageName, $badges );
114
	}
115
116
	/**
117
	 * @param string[] $badgesSerialization
118
	 *
119
	 * @return ItemId[]
120
	 */
121 5
	private function getDeserializedBadges( array $badgesSerialization ) {
122 5
		$badges = array();
123
124 5
		foreach ( $badgesSerialization as $badgeSerialization ) {
125 4
			$badges[] = new ItemId( $badgeSerialization );
126
		}
127
128 3
		return $badges;
129
	}
130
131
}
132