SerializedEntityDeserializer   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
c 2
b 0
f 0
lcom 1
cbo 3
dl 0
loc 43
ccs 15
cts 15
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A isDeserializerFor() 0 3 2
A deserialize() 0 10 2
A getIdFromSerialization() 0 7 2
1
<?php
2
3
namespace Wikibase\EntityStore\DataModel\Deserializers;
4
5
use Deserializers\DispatchableDeserializer;
6
use Deserializers\Exceptions\DeserializationException;
7
use Wikibase\DataModel\Deserializers\EntityIdDeserializer;
8
use Wikibase\EntityStore\DataModel\SerializedEntity;
9
10
/**
11
 * @licence GNU GPL v2+
12
 * @author Thomas Pellissier Tanon
13
 */
14
class SerializedEntityDeserializer implements DispatchableDeserializer {
15
16
	/**
17
	 * @var EntityIdDeserializer
18
	 */
19
	private $entityIdDeserializer;
20
21
	/**
22
	 * @param EntityIdDeserializer $entityIdDeserializer
23
	 */
24 5
	public function __construct( EntityIdDeserializer $entityIdDeserializer ) {
25 5
		$this->entityIdDeserializer = $entityIdDeserializer;
26 5
	}
27
28
	/**
29
	 * @see Deserializer::isDeserializerFor
30
	 */
31 5
	public function isDeserializerFor( $object ) {
32 5
		return is_array( $object ) && array_key_exists( 'type', $object );
33
	}
34
35
	/**
36
	 * @see Deserializer::deserialize
37
	 */
38 4
	public function deserialize( $serialization ) {
39 4
		if ( !$this->isDeserializerFor( $serialization ) ) {
40 2
			throw new DeserializationException( 'SerializedEntityDeserializer serialization should be an array' );
41
		}
42
43 2
		return new SerializedEntity(
44 2
			$this->getIdFromSerialization( $serialization ),
45
			$serialization
46 2
		);
47
	}
48
49 2
	private function getIdFromSerialization( array $serialization ) {
50 2
		if ( !array_key_exists( 'id', $serialization ) ) {
51 1
			return null;
52
		}
53
54 2
		return $this->entityIdDeserializer->deserialize( $serialization['id'] );
55
	}
56
}
57