Passed
Push — master ( 109b6e...716545 )
by Alaa
58s queued 11s
created

StaticTypeIdsStore   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
wmc 7
lcom 2
cbo 0
dl 0
loc 39
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A acquireTypeIds() 0 11 3
A resolveTypeIds() 0 9 3
1
<?php
2
3
namespace Wikibase\TermStore\MediaWiki\PackagePrivate;
4
5
use DomainException;
6
7
/**
8
 * A type IDs acquirer and resolver that only encapsulates access to a static array of IDs.
9
 */
10
class StaticTypeIdsStore implements TypeIdsAcquirer, TypeIdsResolver {
11
12
	/** @var int[] */
13
	private $typeIdsByName;
14
15
	/** @var string[] */
16
	private $typeNamesById;
17
18
	/**
19
	 * @param int[] $types Array from type name to type ID.
20
	 */
21
	public function __construct( array $types ) {
22
		$this->typeIdsByName = $types;
23
		$this->typeNamesById = array_flip( $types );
24
	}
25
26
	public function acquireTypeIds( array $types ): array {
27
		$ret = [];
28
		foreach ( $types as $typeName ) {
29
			if ( array_key_exists( $typeName, $this->typeIdsByName ) ) {
30
				$ret[$typeName] = $this->typeIdsByName[$typeName];
31
			} else {
32
				throw new DomainException( 'Unknown type ' . $typeName . ' not supported!' );
33
			}
34
		}
35
		return $ret;
36
	}
37
38
	public function resolveTypeIds( array $typeIds ): array {
39
		$ret = [];
40
		foreach ( $typeIds as $typeId ) {
41
			if ( array_key_exists( $typeId, $this->typeNamesById ) ) {
42
				$ret[$typeId] = $this->typeNamesById[$typeId];
43
			}
44
		}
45
		return $ret;
46
	}
47
48
}
49