LegacyEntityIdDeserializer   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 3
dl 0
loc 49
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A deserialize() 0 9 3
A isLegacyFormat() 0 4 4
A getParsedId() 0 7 2
A getIdFromLegacyFormat() 0 7 2
1
<?php
2
3
namespace Wikibase\InternalSerialization\Deserializers;
4
5
use Deserializers\Deserializer;
6
use Deserializers\Exceptions\DeserializationException;
7
use InvalidArgumentException;
8
use Wikibase\DataModel\Entity\EntityId;
9
use Wikibase\DataModel\Entity\EntityIdParser;
10
use Wikibase\DataModel\Entity\EntityIdParsingException;
11
use Wikibase\DataModel\LegacyIdInterpreter;
12
13
/**
14
 * @license GPL-2.0-or-later
15
 * @author Jeroen De Dauw < [email protected] >
16
 */
17
class LegacyEntityIdDeserializer implements Deserializer {
18
19
	/**
20
	 * @var EntityIdParser
21
	 */
22
	private $idParser;
23
24 14
	public function __construct( EntityIdParser $idParser ) {
25 14
		$this->idParser = $idParser;
26 14
	}
27
28
	/**
29
	 * @param string|array $serialization
30
	 *
31
	 * @return EntityId
32
	 * @throws DeserializationException
33
	 */
34 14
	public function deserialize( $serialization ) {
35 14
		if ( is_string( $serialization ) ) {
36 5
			return $this->getParsedId( $serialization );
37 9
		} elseif ( $this->isLegacyFormat( $serialization ) ) {
38 3
			return $this->getIdFromLegacyFormat( $serialization );
39
		} else {
40 6
			throw new DeserializationException( 'Entity id format not recognized' );
41
		}
42
	}
43
44 9
	private function isLegacyFormat( $serialization ) {
45 9
		return is_array( $serialization ) && count( $serialization ) == 2
46 9
			&& array_key_exists( 0, $serialization ) && array_key_exists( 1, $serialization );
47
	}
48
49 5
	private function getParsedId( $serialization ) {
50
		try {
51 5
			return $this->idParser->parse( $serialization );
52 1
		} catch ( EntityIdParsingException $ex ) {
53 1
			throw new DeserializationException( $ex->getMessage(), $ex );
54
		}
55
	}
56
57 3
	private function getIdFromLegacyFormat( array $serialization ) {
58
		try {
59 3
			return LegacyIdInterpreter::newIdFromTypeAndNumber( $serialization[0], $serialization[1] );
60 1
		} catch ( InvalidArgumentException $ex ) {
61 1
			throw new DeserializationException( $ex->getMessage(), $ex );
62
		}
63
	}
64
65
}
66