Completed
Push — master ( f5dd2d...861cfa )
by Daniel
24s
created

EntityDeserializer::fromUnknownSerialization()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 11
Bugs 1 Features 2
Metric Value
c 11
b 1
f 2
dl 0
loc 15
rs 9.4285
cc 3
eloc 11
nc 3
nop 1
1
<?php
2
3
namespace Wikibase\InternalSerialization\Deserializers;
4
5
use Deserializers\Deserializer;
6
use Deserializers\DispatchableDeserializer;
7
use Deserializers\Exceptions\DeserializationException;
8
use Wikibase\DataModel\Entity\EntityDocument;
9
10
/**
11
 * @licence GNU GPL v2+
12
 * @author Jeroen De Dauw < [email protected] >
13
 * @author Thiemo Mättig
14
 */
15
class EntityDeserializer implements Deserializer {
16
17
	/**
18
	 * @var DispatchableDeserializer
19
	 */
20
	private $legacyDeserializer;
21
22
	/**
23
	 * @var DispatchableDeserializer
24
	 */
25
	private $currentDeserializer;
26
27
	public function __construct(
28
		DispatchableDeserializer $legacyDeserializer,
29
		DispatchableDeserializer $currentDeserializer
30
	) {
31
		$this->legacyDeserializer = $legacyDeserializer;
32
		$this->currentDeserializer = $currentDeserializer;
33
	}
34
35
	/**
36
	 * @param array $serialization
37
	 *
38
	 * @return EntityDocument
39
	 * @throws DeserializationException
40
	 */
41
	public function deserialize( $serialization ) {
42
		if ( !is_array( $serialization ) ) {
43
			throw new DeserializationException( 'Entity serialization must be an array' );
44
		}
45
46
		if ( $this->currentDeserializer->isDeserializerFor( $serialization ) ) {
47
			return $this->currentDeserializer->deserialize( $serialization );
48
		} elseif ( $this->legacyDeserializer->isDeserializerFor( $serialization ) ) {
49
			return $this->legacyDeserializer->deserialize( $serialization );
50
		} else {
51
			return $this->fromUnknownSerialization( $serialization );
52
		}
53
	}
54
55
	private function fromUnknownSerialization( array $serialization ) {
56
		try {
57
			return $this->currentDeserializer->deserialize( $serialization );
58
		} catch ( DeserializationException $currentEx ) {
59
			try {
60
				return $this->legacyDeserializer->deserialize( $serialization );
61
			} catch ( DeserializationException $legacyEx ) {
62
				throw new DeserializationException(
63
					'The provided entity serialization is neither legacy ('
64
					. $legacyEx->getMessage() . ') nor current ('
65
					. $currentEx->getMessage() . ')'
66
				);
67
			}
68
		}
69
	}
70
71
}
72