MultipleEntitySourceServices   A
last analyzed

Complexity

Total Complexity 32

Size/Duplication

Total Lines 176
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 14

Importance

Changes 0
Metric Value
wmc 32
lcom 2
cbo 14
dl 0
loc 176
rs 9.84
c 0
b 0
f 0

19 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A getEntityRevisionLookup() 0 14 3
A getTermSearchInteractorFactory() 0 14 3
A getPrefetchingTermLookup() 0 14 3
A getEntityPrefetcher() 0 14 3
A getPropertyInfoLookup() 0 7 2
A getEntityStoreWatcher() 0 3 1
A entityUpdated() 0 6 2
A redirectUpdated() 0 6 2
A entityDeleted() 0 6 2
A getEntityNamespaceLookup() 0 9 2
A getFullEntitySerializer() 0 3 1
A getCompactEntitySerializer() 0 3 1
A getStorageEntitySerializer() 0 3 1
A getBaseDataModelSerializerFactory() 0 3 1
A getCompactBaseDataModelSerializerFactory() 0 3 1
A getLanguageFallbackChainFactory() 0 3 1
A getStringNormalizer() 0 3 1
A getTermBuffer() 0 3 1
1
<?php
2
3
namespace Wikibase\DataAccess;
4
5
use Wikibase\DataModel\Entity\EntityId;
6
use Wikibase\DataModel\Entity\EntityRedirect;
7
use Wikibase\DataModel\Entity\Property;
8
use Wikibase\Lib\Interactors\DispatchingTermSearchInteractorFactory;
9
use Wikibase\Lib\Store\EntityNamespaceLookup;
10
use Wikibase\Lib\Store\EntityRevision;
11
use Wikibase\Lib\Store\EntityStoreWatcher;
12
use Wikimedia\Assert\Assert;
13
14
/**
15
 * TODO this has been introduced into data-access with a couple of points that still bind to
16
 * wikibase lib:
17
 *   - Wikibase\Lib\Store\EntityRevision; (could already be moved to data-access)
18
 *   - Wikibase\Lib\Store\EntityStoreWatcher; (only binds to EntityRevision within lib)
19
 *
20
 * @license GPL-2.0-or-later
21
 */
22
class MultipleEntitySourceServices implements WikibaseServices, EntityStoreWatcher {
23
24
	/**
25
	 * @var EntitySourceDefinitions
26
	 */
27
	private $entitySourceDefinitions;
28
29
	private $genericServices;
30
31
	/**
32
	 * @var SingleEntitySourceServices[] indexed by source name
33
	 */
34
	private $singleSourceServices;
35
36
	private $entityRevisionLookup = null;
37
38
	private $termSearchInteractorFactory = null;
39
40
	private $prefetchingTermLookup = null;
41
42
	private $entityPrefetcher = null;
43
44
	private $entityNamespaceLookup = null;
45
46
	/**
47
	 * @param EntitySourceDefinitions $entitySourceDefinitions
48
	 * @param GenericServices $genericServices
49
	 * @param SingleEntitySourceServices[] $singleSourceServices indexed by source name
50
	 */
51
	public function __construct(
52
		EntitySourceDefinitions $entitySourceDefinitions,
53
		GenericServices $genericServices,
54
		array $singleSourceServices
55
	) {
56
		Assert::parameterElementType( SingleEntitySourceServices::class, $singleSourceServices, '$singleSourceServices' );
57
		$this->entitySourceDefinitions = $entitySourceDefinitions;
58
		$this->genericServices = $genericServices;
59
		$this->singleSourceServices = $singleSourceServices;
60
	}
61
62
	public function getEntityRevisionLookup() {
63
		if ( $this->entityRevisionLookup === null ) {
64
			$lookupsPerType = [];
65
66
			/** @var EntitySource $source */
67
			foreach ( $this->entitySourceDefinitions->getEntityTypeToSourceMapping() as $entityType => $source ) {
0 ignored issues
show
Bug introduced by
The expression $this->entitySourceDefin...tyTypeToSourceMapping() of type null|array<integer,objec...taAccess\EntitySource>> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
68
				$lookupsPerType[$entityType] = $this->singleSourceServices[$source->getSourceName()]->getEntityRevisionLookup();
69
			}
70
71
			$this->entityRevisionLookup = new ByTypeDispatchingEntityRevisionLookup( $lookupsPerType );
72
		}
73
74
		return $this->entityRevisionLookup;
75
	}
76
77
	public function getTermSearchInteractorFactory() {
78
		if ( $this->termSearchInteractorFactory === null ) {
79
			$factoriesByType = [];
80
81
			/** @var EntitySource $source */
82
			foreach ( $this->entitySourceDefinitions->getEntityTypeToSourceMapping() as $entityType => $source ) {
0 ignored issues
show
Bug introduced by
The expression $this->entitySourceDefin...tyTypeToSourceMapping() of type null|array<integer,objec...taAccess\EntitySource>> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
83
				$factoriesByType[$entityType] = $this->singleSourceServices[$source->getSourceName()]->getTermSearchInteractorFactory();
84
			}
85
86
			$this->termSearchInteractorFactory = new DispatchingTermSearchInteractorFactory( $factoriesByType );
87
		}
88
89
		return $this->termSearchInteractorFactory;
90
	}
91
92
	public function getPrefetchingTermLookup() {
93
		if ( $this->prefetchingTermLookup === null ) {
94
			$lookupsByType = [];
95
96
			/** @var EntitySource $source */
97
			foreach ( $this->entitySourceDefinitions->getEntityTypeToSourceMapping() as $entityType => $source ) {
0 ignored issues
show
Bug introduced by
The expression $this->entitySourceDefin...tyTypeToSourceMapping() of type null|array<integer,objec...taAccess\EntitySource>> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
98
				$lookupsByType[$entityType] = $this->singleSourceServices[$source->getSourceName()]->getPrefetchingTermLookup();
99
			}
100
101
			$this->prefetchingTermLookup = new ByTypeDispatchingPrefetchingTermLookup( $lookupsByType );
102
		}
103
104
		return $this->prefetchingTermLookup;
105
	}
106
107
	public function getEntityPrefetcher() {
108
		if ( $this->entityPrefetcher === null ) {
109
			$prefetchersByType = [];
110
111
			/** @var EntitySource $source */
112
			foreach ( $this->entitySourceDefinitions->getEntityTypeToSourceMapping() as $entityType => $source ) {
0 ignored issues
show
Bug introduced by
The expression $this->entitySourceDefin...tyTypeToSourceMapping() of type null|array<integer,objec...taAccess\EntitySource>> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
113
				$prefetchersByType[$entityType] = $this->singleSourceServices[$source->getSourceName()]->getEntityPrefetcher();
114
			}
115
116
			$this->entityPrefetcher = new ByTypeDispatchingEntityPrefetcher( $prefetchersByType );
117
		}
118
119
		return $this->entityPrefetcher;
120
	}
121
122
	public function getPropertyInfoLookup() {
123
		$source = $this->entitySourceDefinitions->getSourceForEntityType( Property::ENTITY_TYPE );
124
		if ( $source === null ) {
125
			throw new \LogicException( 'No entity source provides properties!' );
126
		}
127
		return $this->singleSourceServices[$source->getSourceName()]->getPropertyInfoLookup();
128
	}
129
130
	public function getEntityStoreWatcher() {
131
		return $this;
132
	}
133
134
	public function entityUpdated( EntityRevision $entityRevision ) {
135
		$source = $this->entitySourceDefinitions->getSourceForEntityType( $entityRevision->getEntity()->getType() );
136
		if ( $source !== null ) {
137
			$this->singleSourceServices[$source->getSourceName()]->entityUpdated( $entityRevision );
138
		}
139
	}
140
141
	public function redirectUpdated( EntityRedirect $entityRedirect, $revisionId ) {
142
		$source = $this->entitySourceDefinitions->getSourceForEntityType( $entityRedirect->getEntityId()->getEntityType() );
143
		if ( $source !== null ) {
144
			$this->singleSourceServices[$source->getSourceName()]->redirectUpdated( $entityRedirect, $revisionId );
145
		}
146
	}
147
148
	public function entityDeleted( EntityId $entityId ) {
149
		$source = $this->entitySourceDefinitions->getSourceForEntityType( $entityId->getEntityType() );
150
		if ( $source !== null ) {
151
			$this->singleSourceServices[$source->getSourceName()]->entityDeleted( $entityId );
152
		}
153
	}
154
155
	public function getEntityNamespaceLookup(): EntityNamespaceLookup {
156
		if ( $this->entityNamespaceLookup === null ) {
157
			$this->entityNamespaceLookup = array_reduce( $this->singleSourceServices, function ( $nsLookup, $service ){
158
				return $nsLookup->merge( $service->getEntityNamespaceLookup() );
159
			}, new EntityNamespaceLookup( [], [] ) );
160
		}
161
162
		return $this->entityNamespaceLookup;
163
	}
164
165
	public function getFullEntitySerializer() {
166
		return $this->genericServices->getFullEntitySerializer();
167
	}
168
169
	public function getCompactEntitySerializer() {
170
		return $this->genericServices->getCompactEntitySerializer();
171
	}
172
173
	public function getStorageEntitySerializer() {
174
		return $this->genericServices->getStorageEntitySerializer();
175
	}
176
177
	public function getBaseDataModelSerializerFactory() {
178
		return $this->genericServices->getBaseDataModelSerializerFactory();
179
	}
180
181
	public function getCompactBaseDataModelSerializerFactory() {
182
		return $this->genericServices->getCompactBaseDataModelSerializerFactory();
183
	}
184
185
	public function getLanguageFallbackChainFactory() {
186
		return $this->genericServices->getLanguageFallbackChainFactory();
187
	}
188
189
	public function getStringNormalizer() {
190
		return $this->genericServices->getStringNormalizer();
191
	}
192
193
	public function getTermBuffer() {
194
		return $this->getPrefetchingTermLookup();
195
	}
196
197
}
198