Completed
Push — mapview ( 59d78b )
by Jeroen De
03:31
created

MapsMappingService::getFeatureInstance()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 5
cp 0
rs 9.9666
c 0
b 0
f 0
cc 3
nc 2
nop 1
crap 12
1
<?php
2
3
/**
4
 * Base class for mapping services. Deriving classes hold mapping service specific
5
 * information and functionality, which can be used by any mapping feature.
6
 *
7
 * @since 0.6.3
8
 *
9
 * @licence GNU GPL v2+
10
 * @author Jeroen De Dauw < [email protected] >
11
 */
12
abstract class MapsMappingService {
13
14
	/**
15
	 * The internal name of the service.
16
	 *
17
	 * @var string
18
	 */
19
	protected $serviceName;
20
21
	/**
22
	 * A list of aliases for the internal name.
23
	 *
24
	 * @var array
25
	 */
26
	protected $aliases;
27
28
	/**
29
	 * A list of features that support the service, used for validation and defaulting.
30
	 *
31
	 * @var array
32
	 */
33
	protected $features;
34
35
	/**
36
	 * A list of names of resource modules to add.
37
	 *
38
	 * @var array
39
	 */
40
	protected $resourceModules = [];
41
42
	/**
43
	 * A list of dependencies (header items) that have been added.
44
	 *
45
	 * @var array
46
	 */
47
	private $addedDependencies = [];
48
49
	/**
50
	 * A list of dependencies (header items) that need to be added.
51
	 *
52
	 * @var array
53
	 */
54
	private $dependencies = [];
55
56
	/**
57
	 * @param string $serviceName
58
	 * @param array $aliases
59
	 */
60
	public function __construct( $serviceName, array $aliases = [] ) {
61
		$this->serviceName = $serviceName;
62
		$this->aliases = $aliases;
63
	}
64
65
	/**
66
	 * @since 0.7
67
	 *
68
	 * @param $parameterInfo array of IParam
69
	 */
70
	public function addParameterInfo( array &$parameterInfo ) {
71
	}
72
73
	/**
74
	 * @since 0.6.3
75
	 */
76
	public function addFeature( $featureName, $handlingClass ) {
77
		$this->features[$featureName] = $handlingClass;
78
	}
79
80
	/**
81
	 * @since 5.2.0
82
	 * @param ParserOutput $parserOutput
83
	 */
84 19
	public final function addDependencies( ParserOutput $parserOutput ) {
85 19
		$dependencies = $this->getDependencyHtml();
86
87
		// Only add a head item when there are dependencies.
88 19
		if ( $dependencies ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $dependencies of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
89 1
			$parserOutput->addHeadItem( $dependencies );
90
		}
91
92 19
		$parserOutput->addModules( $this->getResourceModules() );
93 19
	}
94
95
	/**
96
	 * @since 0.6.3
97
	 */
98 19
	public final function getDependencyHtml() {
99 19
		$allDependencies = array_merge( $this->getDependencies(), $this->dependencies );
100 19
		$dependencies = [];
101
102
		// Only add dependencies that have not yet been added.
103 19
		foreach ( $allDependencies as $dependency ) {
104 7
			if ( !in_array( $dependency, $this->addedDependencies ) ) {
105 1
				$dependencies[] = $dependency;
106 7
				$this->addedDependencies[] = $dependency;
107
			}
108
		}
109
110
		// If there are dependencies, put them all together in a string, otherwise return false.
111 19
		return $dependencies !== [] ? implode( '', $dependencies ) : false;
112
	}
113
114
	/**
115
	 * Returns a list of html fragments, such as script includes, the current service depends on.
116
	 *
117
	 * @since 0.6.3
118
	 *
119
	 * @return array
120
	 */
121 12
	protected function getDependencies() {
122 12
		return [];
123
	}
124
125
	/**
126
	 * Returns the resource modules that need to be loaded to use this mapping service.
127
	 *
128
	 * @since 0.7.3
129
	 *
130
	 * @return array of string
131
	 */
132 19
	public function getResourceModules() {
133 19
		return $this->resourceModules;
134
	}
135
136
	/**
137
	 * Returns a list of all config variables that should be passed to the JS.
138
	 *
139
	 * @since 1.0.1
140
	 *
141
	 * @return array
142
	 */
143 19
	public function getConfigVariables() {
144 19
		return [];
145
	}
146
147
	/**
148
	 * @since 0.6.3
149
	 */
150 19
	public function getName() {
151 19
		return $this->serviceName;
152
	}
153
154
	/**
155
	 * @since 0.6.6
156
	 */
157
	public function getFeatureInstance( $featureName ) {
158
		$className = $this->getFeature( $featureName );
159
160
		if ( $className === false || !class_exists( $className ) ) {
161
			throw new MWException( 'Could not create a mapping feature class instance' );
162
		}
163
164
		return new $className( $this );
165
	}
166
167
	/**
168
	 * @since 0.6.3
169
	 */
170
	public function getFeature( $featureName ) {
171
		return array_key_exists( $featureName, $this->features ) ? $this->features[$featureName] : false;
172
	}
173
174
	/**
175
	 * @since 0.6.3
176
	 */
177 19
	public function getAliases() {
178 19
		return $this->aliases;
179
	}
180
181
	/**
182
	 * @since 0.6.3
183
	 */
184 7
	public function hasAlias( $alias ) {
185 7
		return in_array( $alias, $this->aliases );
186
	}
187
188
	/**
189
	 * Add one or more names of resource modules that should be loaded.
190
	 *
191
	 * @since 0.7.3
192
	 *
193
	 * @param mixed $modules Array of string or string
194
	 */
195
	public function addResourceModules( $modules ) {
196
		$this->resourceModules = array_merge( $this->resourceModules, (array)$modules );
197
	}
198
199
	/**
200
	 * @param array $dependencies
201
	 */
202 19
	public function addHtmlDependencies( array $dependencies ) {
203 19
		foreach ( $dependencies as $dependency ) {
204
			$this->addHtmlDependency( $dependency );
205
		}
206 19
	}
207
208
	/**
209
	 * @since 0.6.3
210
	 *
211
	 * @param $dependencyHtml
212
	 */
213
	public final function addHtmlDependency( $dependencyHtml ) {
214
		$this->dependencies[] = $dependencyHtml;
215
	}
216
217
	/**
218
	 * @since 1.0
219
	 */
220
	public function getEarthZoom() {
221
		return 1;
222
	}
223
224
	public abstract function getMapId( $increment = true );
225
226
}
227