StatementGuidParser::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 2
ccs 1
cts 1
cp 1
crap 1
rs 10
1
<?php declare( strict_types=1 );
2
3
namespace Wikibase\DataModel\Services\Statement;
4
5
use Wikibase\DataModel\Entity\EntityIdParser;
6
use Wikibase\DataModel\Entity\EntityIdParsingException;
7
use Wikibase\DataModel\Statement\StatementGuid;
8
9
/**
10
 * A parser capable of splitting a statement id into the entity id of the entity the statement
11
 * belongs to, and the randomly generated global unique identifier (GUID).
12
 *
13
 * @see StatementGuid
14
 *
15
 * @since 1.0
16
 *
17
 * @license GPL-2.0-or-later
18
 * @author Addshore
19
 */
20
class StatementGuidParser {
21
22
	private EntityIdParser $entityIdParser;
23
24
	public function __construct( EntityIdParser $entityIdParser ) {
25 11
		$this->entityIdParser = $entityIdParser;
26 11
	}
27 11
28
	/**
29
	 * @throws StatementGuidParsingException
30
	 */
31
	public function parse( string $serialization ): StatementGuid {
32
		$keyParts = explode( StatementGuid::SEPARATOR, $serialization, 2 );
33
		if ( count( $keyParts ) !== 2 ) {
34
			throw new StatementGuidParsingException( '$serialization does not have the correct number of parts' );
35 11
		}
36 11
37 3
		try {
38
			return new StatementGuid( $this->entityIdParser->parse( $keyParts[0] ), $keyParts[1], $serialization );
39
		} catch ( EntityIdParsingException $ex ) {
40 8
			throw new StatementGuidParsingException(
41
				'$serialization contains invalid EntityId: '
42 8
				. $ex->getMessage()
43 4
			);
44
		}
45
	}
46
47
}
48