EntityIdDeserializer::deserialize()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.9
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Wikibase\DataModel\Deserializers;
4
5
use Deserializers\Deserializer;
6
use Deserializers\Exceptions\DeserializationException;
7
use Wikibase\DataModel\Entity\EntityId;
8
use Wikibase\DataModel\Entity\EntityIdParser;
9
use Wikibase\DataModel\Entity\EntityIdParsingException;
10
11
/**
12
 * Package private
13
 *
14
 * @license GPL-2.0-or-later
15
 * @author Thomas Pellissier Tanon
16
 */
17
class EntityIdDeserializer implements Deserializer {
18
19
	/**
20
	 * @var EntityIdParser
21
	 */
22
	private $entityIdParser;
23
24 26
	public function __construct( EntityIdParser $entityIdParser ) {
25 26
		$this->entityIdParser = $entityIdParser;
26 26
	}
27
28
	/**
29
	 * @see Deserializer::deserialize
30
	 *
31
	 * @param string $serialization
32
	 *
33
	 * @throws DeserializationException
34
	 * @return EntityId
35
	 */
36 15
	public function deserialize( $serialization ) {
37 15
		if ( !is_string( $serialization ) ) {
38 2
			throw new DeserializationException( 'The serialization of an entity ID should be a string' );
39
		}
40
41
		try {
42 13
			return $this->entityIdParser->parse( $serialization );
43 1
		} catch ( EntityIdParsingException $e ) {
44 1
			throw new DeserializationException( "'$serialization' is not a valid entity ID", $e );
45
		}
46
	}
47
48
}
49