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

StatementDeserializer   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A deserialize() 0 13 4
A __construct() 0 7 1
A fromUnknownSerialization() 0 15 3
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\Statement\Statement;
9
10
/**
11
 * @licence GNU GPL v2+
12
 * @author Jeroen De Dauw < [email protected] >
13
 * @author Thiemo Mättig
14
 */
15
class StatementDeserializer 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 Statement
39
	 * @throws DeserializationException
40
	 */
41
	public function deserialize( $serialization ) {
42
		if ( !is_array( $serialization ) ) {
43
			throw new DeserializationException( 'Claim 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 claim serialization is neither legacy ('
64
					. $legacyEx->getMessage() . ') nor current ('
65
					. $currentEx->getMessage() . ')'
66
				);
67
			}
68
		}
69
	}
70
71
}
72