Passed
Push — master ( 35b0b4...a423cc )
by Marius
01:13
created

LegacyEntityDeserializer::deserialize()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.9
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Wikibase\InternalSerialization\Deserializers;
4
5
use Deserializers\DispatchableDeserializer;
6
use Deserializers\Exceptions\DeserializationException;
7
use Wikibase\DataModel\Entity\EntityDocument;
8
9
/**
10
 * @license GPL-2.0-or-later
11
 * @author Jeroen De Dauw < [email protected] >
12
 */
13
class LegacyEntityDeserializer implements DispatchableDeserializer {
14
15
	/**
16
	 * @var DispatchableDeserializer
17
	 */
18
	private $itemDeserializer;
19
20
	/**
21
	 * @var DispatchableDeserializer
22
	 */
23
	private $propertyDeserializer;
24
25 5
	public function __construct(
26
		DispatchableDeserializer $itemDeserializer,
27
		DispatchableDeserializer $propertyDeserializer
28
	) {
29 5
		$this->itemDeserializer = $itemDeserializer;
30 5
		$this->propertyDeserializer = $propertyDeserializer;
31 5
	}
32
33
	/**
34
	 * @param mixed $serialization
35
	 *
36
	 * @return EntityDocument
37
	 * @throws DeserializationException
38
	 */
39 5
	public function deserialize( $serialization ) {
40 5
		if ( !is_array( $serialization ) ) {
41 2
			throw new DeserializationException( 'Entity serialization must be an array' );
42
		}
43
44 3
		if ( $this->propertyDeserializer->isDeserializerFor( $serialization ) ) {
45 2
			return $this->propertyDeserializer->deserialize( $serialization );
46
		}
47
48 1
		return $this->itemDeserializer->deserialize( $serialization );
49
	}
50
51
	/**
52
	 * @see DispatchableDeserializer::isDeserializerFor
53
	 *
54
	 * @since 2.2
55
	 *
56
	 * @param mixed $serialization
57
	 *
58
	 * @return bool
59
	 */
60
	public function isDeserializerFor( $serialization ) {
61
		return $this->itemDeserializer->isDeserializerFor( $serialization )
62
			|| $this->propertyDeserializer->isDeserializerFor( $serialization );
63
	}
64
65
}
66