Completed
Push — AUTOMATED_TESTING ( 4cfed4...8e1a94 )
by Gordon
18:08
created

MapUtil   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 251
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4
Metric Value
wmc 20
lcom 1
cbo 4
dl 0
loc 251
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A reset() 0 15 1
A set_api_key() 0 3 1
A set_map_size() 0 4 1
A set_map_type() 0 3 1
A set_info_window_width() 0 4 1
A set_center() 0 4 1
A set_icon_size() 0 5 1
B instance() 0 35 3
A sanitize() 0 3 1
A get_map() 0 10 4
B ChooseToAddDataobject() 0 13 5
1
<?php
2
3
class MapUtil
4
{
5
6
	/**
7
	 * @var string The Google Maps API key
8
	 */
9
	protected static $api_key;
10
11
	/**
12
	 * @var int Number of active {@see GoogleMapsAPI} instances (for the HTML ID)
13
	 */
14
	protected static $instances = 0;
15
16
	/**
17
	 * @var int The default width of a Google Map
18
	 */
19
	public static $map_width = '100%';
20
21
	/**
22
	 * @var int The default height of a Google Map
23
	 */
24
	public static $map_height = '400px';
25
26
	/** @var int Icon width of the gmarker **/
27
	public static $iconWidth = 24;
28
29
	/** @var int Icon height of the gmarker **/
30
	public static $iconHeight = 24;
31
32
	/**
33
	 * @var int Prefix for the div ID of the map
34
	 */
35
	public static $div_id = "google_map";
36
37
	/**
38
	 * @var boolean Automatic center/zoom for the map
39
	 */
40
	public static $automatic_center = true;
41
42
	/**
43
	 * @var boolean Show the marker fields on the map
44
	 */
45
	public static $hide_marker = false;
46
47
48
49
50
	/**
51
	 * @var boolean Show the marker fields on the map
52
	 */
53
	public static $map_type = 'google.maps.MapTypeId.ROADMAP';
54
55
	/**
56
	 * @var string $center Center of map (adress)
57
	 */
58
	public static $center = 'Paris, France';
59
60
	/* Width of the map information window */
61
	public static $info_window_width = 250;
62
63
	/* Whether or not to allow full screen */
64
	private static $allow_full_screen = null;
65
66
67
	public static function reset() {
68
		self::$api_key = null;
69
		self::$instances = 0;
70
		self::$map_width = '100%';
0 ignored issues
show
Documentation Bug introduced by
The property $map_width was declared of type integer, but '100%' is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
71
		self::$map_height = '400px';
0 ignored issues
show
Documentation Bug introduced by
The property $map_height was declared of type integer, but '400px' is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
72
		self::$iconWidth = 24;
73
		self::$iconHeight = 24;
74
		self::$div_id = "google_map";
0 ignored issues
show
Documentation Bug introduced by
The property $div_id was declared of type integer, but 'google_map' is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
75
		self::$automatic_center = true;
76
		self::$hide_marker = false;
77
		self::$map_type = 'google.maps.MapTypeId.ROADMAP';
0 ignored issues
show
Documentation Bug introduced by
The property $map_type was declared of type boolean, but 'google.maps.MapTypeId.ROADMAP' is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
78
		self::$center = 'Paris, France';
79
		self::$info_window_width = 250;
80
		self::$allow_full_screen = null;
81
	}
82
83
	/**
84
	 * Set the API key for Google Maps
85
	 *
86
	 * @param string $key
87
	 */
88
	public static function set_api_key($key) {
89
		self::$api_key = $key;
90
	}
91
92
	/**
93
	 * Set the default size of the map
94
	 *
95
	 * @param int $width
96
	 * @param int $height
97
	 */
98
	public static function set_map_size($width, $height) {
99
		self:: $map_width = $width;
100
		self::$map_height = $height;
101
	}
102
103
	/**
104
	 * FIXME - NOT USED?
105
	 * Set the type of the gmap
106
	 *
107
	 * @param string $mapType (can be 'google.maps.MapTypeId.ROADMAP', 'G_SATELLITE_MAP',
108
	 * 'G_HYBRID_MAP', 'G_PHYSICAL_MAP')
109
	 *
110
	 * @return void
111
	 */
112
	public static function set_map_type($mapType) {
113
		self::$map_type = $mapType;
0 ignored issues
show
Documentation Bug introduced by
The property $map_type was declared of type boolean, but $mapType is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
114
	}
115
116
	/**
117
	 * FIXME - NOT USED?
118
	 * Set the with of the gmap infowindow (on marker clik)
119
	 *
120
	 * @param int $info_window_width GoogleMap info window width
121
	 *
122
	 * @return void
123
	 */
124
	public static function set_info_window_width($info_window_width)
125
	{
126
		self::$info_window_width = $info_window_width;
127
	}
128
129
	/**
130
	 * FIXME - NOT USED?
131
	 * Set the center of the gmap (an address)
132
	 *
133
	 * @param string $center GoogleMap  center (an address)
134
	 *
135
	 * @return void
136
	 */
137
	public static function set_center($center)
138
	{
139
		self::$center = $center;
140
	}
141
142
	/**
143
	 * FIXME Is this used?
144
	 *
145
	 * Set the size of the icon markers
146
	 *
147
	 * @param int $iconWidth GoogleMap  marker icon width
148
	 * @param int $iconHeight GoogleMap  marker icon height
149
	 *
150
	 * @return void
151
	 */
152
153
	public static function set_icon_size($iconWidth, $iconHeight)
154
	{
155
		self::$iconWidth = $iconWidth;
156
		self::$iconHeight = $iconHeight;
157
	}
158
159
	/**
160
	 * Get a new GoogleMapAPI object and load it with the default settings
161
	 *
162
	 * @return MapAPI
163
	 */
164
	public static function instance()
165
	{
166
		self::$instances++;
167
168
		if (self::$allow_full_screen == null) {
169
			self::$allow_full_screen = Config::inst()->get('Mappable', 'allow_full_screen');
170
		}
171
172
		$url = Director::absoluteBaseURL();
173
174
		// remove http and https
175
		$url = str_replace('http://', '', $url);
176
		$url = str_replace('https://', '', $url);
177
		$parts = explode('/', $url);
178
		$host = $parts[0];
179
180
		$key = self::$api_key;
181
182
		// if an array, get the key by an array keyed by host
183
		if (is_array($key)) {
184
			$key = $key[$host];
185
		}
186
187
188
		$gmap = new MapAPI($key);
189
		$gmap->setDivId(self::$div_id."_".self::$instances);
190
		$gmap->setEnableAutomaticCenterZoom(self::$automatic_center);
0 ignored issues
show
Documentation introduced by
self::$automatic_center is of type boolean, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
191
		$gmap->setSize(self::$map_width, self::$map_height);
192
		$gmap->setDefaultHideMarker(self::$hide_marker);
193
		$gmap->setMapType(self::$map_type);
0 ignored issues
show
Documentation introduced by
self::$map_type is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
194
		$gmap->setCenter(self::$center);
195
		$gmap->setIconSize(self::$iconWidth, self::$iconHeight);
196
		$gmap->setAllowFullScreen(self::$allow_full_screen);
197
		return $gmap;
198
	}
199
200
201
	/**
202
	 * Sanitize a string of HTML content for safe inclusion in the JavaScript
203
	 * for a Google Map
204
	 *
205
	 * @return string
206
	 */
207
	public static function sanitize($content) {
208
		return addslashes(str_replace(array("\n", "\r", "\t"), '', $content));
209
	}
210
211
212
	/**
213
	 * Creates a new {@link GoogleMapsAPI} object loaded with the default settings
214
	 * and places all of the items in a {@link SS_List}
215
	 * e.g. {@link DataList} or {@link ArrayList} on the map
216
	 *
217
	 * @param SS_List list of objects to display on a map
218
	 * @param  array $infowindowtemplateparams Optional array of extra parameters to pass to the map info window
219
	 * @return MapAPI
220
	 */
221
	public static function get_map(SS_List $list, $infowindowtemplateparams) {
222
		$gmap = self::instance();
223
		if ($list) {
224
			foreach ($list as $mappable) {
225
				if (self::ChooseToAddDataobject($mappable))
226
					$gmap->addMarkerAsObject($mappable, $infowindowtemplateparams);
227
			}
228
		}
229
		return $gmap;
230
	}
231
232
	/**
233
	 * Determines if the current DataObject should be included to the map
234
	 * Checks if it has Mappable interface implemented
235
	 * If it has MapExtension included, the value of MapPinEdited is also checked
236
	 *
237
	 * @param DataObject $do
238
	 * @return bool
239
	 */
240
	private static function ChooseToAddDataobject(DataObject $do) {
241
		$isMappable = $do->is_a('Mappable');
242
243
		foreach ($do->getExtensionInstances() as $extension) {
244
			$isMappable = $isMappable || $extension instanceof Mappable;
245
		}
246
247
		$filterMapPinEdited = $do->hasExtension('MapExtension')
248
			? $do->MapPinEdited
249
			: true;
250
251
		return $isMappable && $filterMapPinEdited;
252
	}
253
}
254