Completed
Push — master ( 1bf879...10046a )
by
unknown
08:18 queued 10s
created

ApiServiceFactory::getApiEntityLookup()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
declare( strict_types = 1 );
4
namespace Wikibase\Repo\FederatedProperties;
5
6
use Exception;
7
use MediaWiki\Logger\LoggerFactory;
8
use MediaWiki\MediaWikiServices;
9
use Wikibase\Repo\WikibaseRepo;
10
11
/**
12
 * @license GPL-2.0-or-later
13
 */
14
class ApiServiceFactory {
15
16
	/**
17
	 * @var string
18
	 */
19
	private $federatedPropertiesSourceScriptUrl;
20
21
	/**
22
	 * @var string
23
	 */
24
	private $serverName;
25
26
	/**
27
	 * @var ApiEntityLookup|null
28
	 */
29
	private static $apiEntityLookupInstance = null;
30
31
	/**
32
	 * @var ApiEntityNamespaceInfoLookup|null
33
	 */
34
	private static $apiEntityNamespaceInfoLookup = null;
35
36
	public static function resetClassStatics() {
37
		if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
38
			throw new Exception(
39
				'Cannot reset ApiServiceFactory class statics outside of tests.'
40
			);
41
		}
42
		self::$apiEntityLookupInstance = null;
43
		self::$apiEntityNamespaceInfoLookup = null;
44
	}
45
46
	public function __construct(
47
		string $federatedPropertiesSourceScriptUrl,
48
		string $serverName
49
	) {
50
		$this->federatedPropertiesSourceScriptUrl = $federatedPropertiesSourceScriptUrl;
51
		$this->serverName = $serverName;
52
	}
53
54
	private function getUrlForScriptFile( $scriptFile ): string {
55
		return $this->federatedPropertiesSourceScriptUrl . $scriptFile;
56
	}
57
58
	private function newFederatedPropertiesApiClient(): GenericActionApiClient {
59
		return new GenericActionApiClient(
60
			MediaWikiServices::getInstance()->getHttpRequestFactory(),
61
			$this->getUrlForScriptFile( 'api.php' ),
62
			LoggerFactory::getInstance( 'Wikibase.FederatedProperties' ),
63
			$this->serverName
64
		);
65
	}
66
67
	public function newApiEntitySearchHelper(): ApiEntitySearchHelper {
68
		return new ApiEntitySearchHelper(
69
			$this->newFederatedPropertiesApiClient(),
70
			WikibaseRepo::getDefaultInstance()->getDataTypeDefinitions()->getTypeIds()
71
		);
72
	}
73
74
	private function getApiEntityNamespaceInfoLookup(): ApiEntityNamespaceInfoLookup {
75
		if ( self::$apiEntityNamespaceInfoLookup === null ) {
76
			self::$apiEntityNamespaceInfoLookup = new ApiEntityNamespaceInfoLookup(
77
				$this->newFederatedPropertiesApiClient(),
78
				WikibaseRepo::getDefaultInstance()->getContentModelMappings()
79
			);
80
		}
81
		return self::$apiEntityNamespaceInfoLookup;
82
	}
83
84
	public function newApiEntityTitleTextLookup(): ApiEntityTitleTextLookup {
85
		return new ApiEntityTitleTextLookup(
86
			$this->getApiEntityNamespaceInfoLookup()
87
		);
88
	}
89
90
	public function newApiEntityUrlLookup(): ApiEntityUrlLookup {
91
		return new ApiEntityUrlLookup(
92
			$this->newApiEntityTitleTextLookup(),
93
			$this->federatedPropertiesSourceScriptUrl
94
		);
95
	}
96
97
	public function newApiPropertyDataTypeLookup(): ApiPropertyDataTypeLookup {
98
		return new ApiPropertyDataTypeLookup(
99
			$this->getApiEntityLookup()
100
		);
101
	}
102
103
	public function newApiPrefetchingTermLookup(): ApiPrefetchingTermLookup {
104
		return new ApiPrefetchingTermLookup(
105
			$this->getApiEntityLookup()
106
		);
107
	}
108
109
	public function getApiEntityLookup(): ApiEntityLookup {
110
		if ( self::$apiEntityLookupInstance === null ) {
111
			self::$apiEntityLookupInstance = new ApiEntityLookup( $this->newFederatedPropertiesApiClient() );
112
		}
113
		return self::$apiEntityLookupInstance;
114
	}
115
116
	public function newApiEntityExistenceChecker(): ApiEntityExistenceChecker {
117
		return new ApiEntityExistenceChecker( $this->getApiEntityLookup() );
118
	}
119
120
}
121