SiteLinkDeserializer::deserializeItemId()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

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