Completed
Pull Request — master (#98)
by no
04:47 queued 02:22
created

LegacyEntityDeserializer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 5
Bugs 0 Features 1
Metric Value
wmc 6
c 5
b 0
f 1
lcom 1
cbo 2
dl 0
loc 53
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A deserialize() 0 11 3
A isDeserializerFor() 0 4 2
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
 * @licence GNU GPL v2+
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
	public function __construct(
26
		DispatchableDeserializer $itemDeserializer,
27
		DispatchableDeserializer $propertyDeserializer
28
	) {
29
		$this->itemDeserializer = $itemDeserializer;
30
		$this->propertyDeserializer = $propertyDeserializer;
31
	}
32
33
	/**
34
	 * @param mixed $serialization
35
	 *
36
	 * @return EntityDocument
37
	 * @throws DeserializationException
38
	 */
39
	public function deserialize( $serialization ) {
40
		if ( !is_array( $serialization ) ) {
41
			throw new DeserializationException( 'Entity serialization must be an array' );
42
		}
43
44
		if ( $this->propertyDeserializer->isDeserializerFor( $serialization ) ) {
45
			return $this->propertyDeserializer->deserialize( $serialization );
46
		}
47
48
		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