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

StatementDeserializer::isDeserializerFor()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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