Passed
Push — master ( 35ca17...34b29b )
by Daryl
02:19
created

Map_Model::center()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 8.125

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 24
ccs 6
cts 12
cp 0.5
rs 8.5125
cc 5
eloc 12
nc 4
nop 0
crap 8.125
1
<?php
2
3
namespace Clubdeuce\WPGoogleMaps;
4
5
/**
6
 * Class Map
7
 * @package Clubdeuce\WPGoogleMaps
8
 *
9
 * @method array styles()
10
 */
11
class Map_Model extends Model_Base {
12
13
	/**
14
	 * @var null|string
15
	 */
16
	protected $_background_color = null;
17
18
	/**
19
	 * @var array
20
	 */
21
	protected $_center;
22
23
	/**
24
	 * The Map element height (default: 400px).
25
	 * @var string
26
	 */
27
	protected $_height = '400px';
28
29
	/**
30
	 * The string to use for the map HTML element id property
31
	 *
32
	 * @var string
33
	 */
34
	protected $_html_id;
35
36
	/**
37
	 * @var Marker[]
38
	 */
39
	protected $_markers = array();
40
41
	/**
42
	 * @var array
43
	 */
44
	protected $_styles = array();
45
46
	/**
47
	 * The Map element width (default: 100%).
48
	 * @var string
49
	 */
50
	protected $_width = '100%';
51
52
	/**
53
	 * @var int
54
	 */
55
	protected $_zoom = 5;
56
57
	/**
58
	 * @param Marker $marker
59
	 */
60 1
	public function add_marker( $marker ) {
61
62 1
		$this->_markers[] = $marker;
63
64 1
	}
65
66
	/**
67
	 * @param Marker[] $markers
68
	 */
69 1
	public function add_markers( $markers ) {
70
71 1
		$this->_markers = array_merge( $this->_markers, $markers );
72
73 1
	}
74
75
	/**
76
	 * @return array
77
	 */
78 2
	public function center() {
79
80 2
		$value = null;
81
82
		do {
83 2
			if ( isset( $this->_center ) ) {
84 2
				$value = $this->_center;
85 2
				break;
86
			}
87
88
			if ( 0 == count( $this->markers() ) ) {
89
				break;
90
			}
91
92
			$marker = $this->markers()[0];
93
94
			if ( $marker instanceof Marker ) {
95
96
				$value = $marker->position();
97
98
			}
99
		} while ( false );
100
101 2
		return $value;
102
103
	}
104
105
	/**
106
	 * @return string
107
	 */
108 2
	public function height() {
109
110 2
		return $this->_height;
111
112
	}
113
114
	/**
115
	 * @return string
116
	 */
117 2
	public function html_id() {
118
119 2
		if ( ! isset( $this->_html_id ) ) {
120 1
			$this->_html_id = sprintf( 'map-%1$s', md5( serialize( array( $this->center(), $this->markers() ) ) ) );
121
		}
122
123 2
		return $this->_html_id;
124
125
	}
126
127
	/**
128
	 * @return Marker[]
129
	 */
130 1
	public function markers() {
131
132 1
		return $this->_markers;
133
134
	}
135
136
	/**
137
	 * @return string
138
	 */
139 1
	public function width() {
140
141 1
		return $this->_width;
142
143
	}
144
145
	/**
146
	 * @return int
147
	 */
148 2
	public function zoom() {
149
150 2
		return $this->_zoom;
151
152
	}
153
154
	/**
155
	 * @param array $center
156
	 */
157 1
	public function set_center( $center ) {
158
159 1
		$center = wp_parse_args( $center, array(
160 1
			'lat' => null,
161
			'lng' => null,
162
		) );
163
164 1
		$this->_center = $center;
165
166 1
	}
167
168
	/**
169
	 * @param int $zoom
170
	 */
171 1
	public function set_zoom( $zoom ) {
172
173 1
		$this->_zoom = (int)$zoom;
174
175 1
	}
176
177
	/**
178
	 * @param string $height
179
	 */
180 1
	public function set_height( $height ) {
181
182 1
		$this->_height = $height;
183
184 1
	}
185
186
	/**
187
	 * @param string $html_id
188
	 */
189 1
	public function set_html_id( $html_id ) {
190
191 1
		$this->_html_id = $html_id;
192
193 1
	}
194
195
	/**
196
	 * @param array|string $styles
197
	 */
198
	public function set_styles( $styles ) {
199
200
		do {
201
			if ( is_string( $styles ) ) {
202
				$styles = json_decode( $styles, true );
203
			}
204
205
			if ( ! is_array( $styles ) ) {
206
				trigger_error( __( 'The style property must be an array' ) );
207
				break;
208
			}
209
210
			$this->_styles = $styles;
211
		} while ( false );
212
213
	}
214
215
	/**
216
	 * @return array
217
	 *
218
	 * @todo Refactor to make_params
219
	 */
220 1
	public function make_args() {
221
222
		return array(
223 1
			'center'          => $this->center(),
224 1
			'styles'          => $this->styles(),
225 1
			'zoom'            => (int)$this->zoom(),
226
		);
227
228
	}
229
230
}
231