Completed
Push — cleanz ( 70f885...f1882e )
by Jeroen De
26:41 queued 16:46
created

MappingService   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 205
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 0

Importance

Changes 0
Metric Value
wmc 24
lcom 3
cbo 0
dl 0
loc 205
rs 10
c 0
b 0
f 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A addParameterInfo() 0 2 1
A addFeature() 0 3 1
A addDependencies() 0 10 2
A getDependencyHtml() 0 15 4
A getDependencies() 0 3 1
A getResourceModules() 0 3 1
A getName() 0 3 1
A getFeatureInstance() 0 9 3
A getFeature() 0 3 2
A getAliases() 0 3 1
A hasAlias() 0 3 1
A addResourceModules() 0 3 1
A addHtmlDependencies() 0 5 2
A addHtmlDependency() 0 3 1
A getEarthZoom() 0 3 1
getMapId() 0 1 ?
1
<?php
2
3
namespace Maps;
4
5
use MWException;
6
use ParserOutput;
7
8
/**
9
 * Base class for mapping services. Deriving classes hold mapping service specific
10
 * information and functionality, which can be used by any mapping feature.
11
 *
12
 * @since 0.6.3
13
 *
14
 * @licence GNU GPL v2+
15
 * @author Jeroen De Dauw < [email protected] >
16
 */
17
abstract class MappingService {
18
19
	/**
20
	 * The internal name of the service.
21
	 *
22
	 * @var string
23
	 */
24
	protected $serviceName;
25
26
	/**
27
	 * A list of aliases for the internal name.
28
	 *
29
	 * @var array
30
	 */
31
	protected $aliases;
32
33
	/**
34
	 * A list of features that support the service, used for validation and defaulting.
35
	 *
36
	 * @var array
37
	 */
38
	protected $features;
39
40
	/**
41
	 * A list of names of resource modules to add.
42
	 *
43
	 * @var array
44
	 */
45
	protected $resourceModules = [];
46
47
	/**
48
	 * A list of dependencies (header items) that have been added.
49
	 *
50
	 * @var array
51
	 */
52
	private $addedDependencies = [];
53
54
	/**
55
	 * A list of dependencies (header items) that need to be added.
56
	 *
57
	 * @var array
58
	 */
59
	private $dependencies = [];
60
61
	/**
62
	 * @param string $serviceName
63
	 * @param array $aliases
64
	 */
65
	public function __construct( $serviceName, array $aliases = [] ) {
66
		$this->serviceName = $serviceName;
67
		$this->aliases = $aliases;
68
	}
69
70
	/**
71
	 * @since 0.7
72
	 *
73
	 * @param $parameterInfo array of IParam
74
	 */
75
	public function addParameterInfo( array &$parameterInfo ) {
76
	}
77
78
	/**
79
	 * @since 0.6.3
80
	 */
81
	public function addFeature( $featureName, $handlingClass ) {
82
		$this->features[$featureName] = $handlingClass;
83
	}
84
85
	/**
86
	 * @since 5.2.0
87
	 *
88
	 * @param ParserOutput $parserOutput
89
	 */
90
	public final function addDependencies( ParserOutput $parserOutput ) {
91
		$dependencies = $this->getDependencyHtml();
92
93
		// Only add a head item when there are dependencies.
94
		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...
95
			$parserOutput->addHeadItem( $dependencies );
96
		}
97
98
		$parserOutput->addModules( $this->getResourceModules() );
99
	}
100
101
	/**
102
	 * @since 0.6.3
103
	 */
104
	public final function getDependencyHtml() {
105
		$allDependencies = array_merge( $this->getDependencies(), $this->dependencies );
106
		$dependencies = [];
107
108
		// Only add dependencies that have not yet been added.
109
		foreach ( $allDependencies as $dependency ) {
110
			if ( !in_array( $dependency, $this->addedDependencies ) ) {
111
				$dependencies[] = $dependency;
112
				$this->addedDependencies[] = $dependency;
113
			}
114
		}
115
116
		// If there are dependencies, put them all together in a string, otherwise return false.
117
		return $dependencies !== [] ? implode( '', $dependencies ) : false;
118
	}
119
120
	/**
121
	 * Returns a list of html fragments, such as script includes, the current service depends on.
122
	 *
123
	 * @since 0.6.3
124
	 *
125
	 * @return array
126
	 */
127
	protected function getDependencies() {
128
		return [];
129
	}
130
131
	/**
132
	 * Returns the resource modules that need to be loaded to use this mapping service.
133
	 *
134
	 * @since 0.7.3
135
	 *
136
	 * @return array of string
137
	 */
138
	public function getResourceModules() {
139
		return $this->resourceModules;
140
	}
141
142
	/**
143
	 * @since 0.6.3
144
	 */
145
	public function getName() {
146
		return $this->serviceName;
147
	}
148
149
	/**
150
	 * @since 0.6.6
151
	 */
152
	public function getFeatureInstance( $featureName ) {
153
		$className = $this->getFeature( $featureName );
154
155
		if ( $className === false || !class_exists( $className ) ) {
156
			throw new MWException( 'Could not create a mapping feature class instance' );
157
		}
158
159
		return new $className( $this );
160
	}
161
162
	/**
163
	 * @since 0.6.3
164
	 */
165
	public function getFeature( $featureName ) {
166
		return array_key_exists( $featureName, $this->features ) ? $this->features[$featureName] : false;
167
	}
168
169
	/**
170
	 * @since 0.6.3
171
	 */
172
	public function getAliases() {
173
		return $this->aliases;
174
	}
175
176
	/**
177
	 * @since 0.6.3
178
	 */
179
	public function hasAlias( $alias ) {
180
		return in_array( $alias, $this->aliases );
181
	}
182
183
	/**
184
	 * Add one or more names of resource modules that should be loaded.
185
	 *
186
	 * @since 0.7.3
187
	 *
188
	 * @param mixed $modules Array of string or string
189
	 */
190
	public function addResourceModules( $modules ) {
191
		$this->resourceModules = array_merge( $this->resourceModules, (array)$modules );
192
	}
193
194
	/**
195
	 * @param array $dependencies
196
	 */
197
	public function addHtmlDependencies( array $dependencies ) {
198
		foreach ( $dependencies as $dependency ) {
199
			$this->addHtmlDependency( $dependency );
200
		}
201
	}
202
203
	/**
204
	 * @since 0.6.3
205
	 *
206
	 * @param $dependencyHtml
207
	 */
208
	public final function addHtmlDependency( $dependencyHtml ) {
209
		$this->dependencies[] = $dependencyHtml;
210
	}
211
212
	/**
213
	 * @since 1.0
214
	 */
215
	public function getEarthZoom() {
216
		return 1;
217
	}
218
219
	public abstract function getMapId( $increment = true );
220
221
}
222