Passed
Push — master ( 1624f2...3d792a )
by
unknown
01:39
created

SiteLinkDeserializer::deserializeItemId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0078

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 7
cts 8
cp 0.875
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 1
crap 2.0078
1
<?php
2
3
namespace Wikibase\DataModel\Deserializers;
4
5
use Deserializers\Deserializer;
6
use Deserializers\Exceptions\DeserializationException;
7
use Deserializers\Exceptions\InvalidAttributeException;
8
use Deserializers\Exceptions\MissingAttributeException;
9
use Wikibase\DataModel\Entity\ItemId;
10
use Wikibase\DataModel\SiteLink;
11
12
/**
13
 * Package private
14
 *
15
 * @license GPL-2.0+
16
 * @author Thomas Pellissier Tanon
17
 */
18
class SiteLinkDeserializer implements Deserializer {
19
20
	/**
21
	 * @var Deserializer
22
	 */
23
	private $entityIdDeserializer;
24
25
	/**
26
	 * @param Deserializer $entityIdDeserializer
27
	 */
28 29
	public function __construct( Deserializer $entityIdDeserializer ) {
29 29
		$this->entityIdDeserializer = $entityIdDeserializer;
30 29
	}
31
32
	/**
33
	 * @see Deserializer::deserialize
34
	 *
35
	 * @param array $serialization
36
	 *
37
	 * @throws DeserializationException
38
	 * @return SiteLink
39
	 */
40 17
	public function deserialize( $serialization ) {
41 17
		$this->assertCanDeserialize( $serialization );
42
43 12
		return $this->getDeserialized( $serialization );
44
	}
45
46
	/**
47
	 * @param array $serialization
48
	 *
49
	 * @return SiteLink
50
	 */
51 12
	private function getDeserialized( array $serialization ) {
52 12
		return new SiteLink(
53 12
			$serialization['site'],
54 12
			$serialization['title'],
55 12
			$this->getDeserializeBadges( $serialization )
56
		);
57
	}
58
59 12
	private function getDeserializeBadges( array $serialization ) {
60 12
		if ( !array_key_exists( 'badges', $serialization ) ) {
61 2
			return array();
62
		}
63 10
		$this->assertBadgesIsArray( $serialization );
64
65 9
		$badges = array();
66 9
		foreach ( $serialization['badges'] as $badgeSerialization ) {
67 4
			$badges[] = $this->deserializeItemId( $badgeSerialization );
68
		}
69 8
		return $badges;
70
	}
71
72 4
	private function deserializeItemId( $serialization ) {
73 4
		$itemId = $this->entityIdDeserializer->deserialize( $serialization );
74
75 4
		if ( !( $itemId instanceof ItemId ) ) {
76 1
			throw new InvalidAttributeException(
77 1
				'badges',
78
				$serialization,
79 1
				"'$serialization' is not a valid item ID"
80
			);
81
		}
82
83 3
		return $itemId;
84
	}
85
86 10
	private function assertBadgesIsArray( $serialization ) {
87 10
		if ( !is_array( $serialization['badges'] ) ) {
88 1
			throw new InvalidAttributeException(
89 1
				'badges',
90 1
				$serialization['badges'],
91 1
				"badges attribute is not a valid array"
92
			);
93
		}
94 9
	}
95
96 17
	private function assertCanDeserialize( $serialization ) {
97 17
		$this->requireAttribute( $serialization, 'site' );
98 13
		$this->requireAttribute( $serialization, 'title' );
99 12
	}
100
101 17
	private function requireAttribute( $serialization, $attribute ) {
102 17
		if ( !is_array( $serialization ) || !array_key_exists( $attribute, $serialization ) ) {
103 5
			throw new MissingAttributeException( $attribute );
104
		}
105 13
	}
106
107
}
108