Passed
Push — feature/v4 ( e5e380...dac4aa )
by Samuel
11:37
created

OverlayManager   F

Complexity

Total Complexity 98

Size/Duplication

Total Lines 472
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 98
eloc 130
c 1
b 0
f 0
dl 0
loc 472
ccs 222
cts 222
cp 1
rs 2

65 Methods

Rating   Name   Duplication   Size   Complexity  
A removeCircle() 0 5 2
A hasGroundOverlay() 0 3 1
A removePolyline() 0 5 2
A removeGroundOverlay() 0 5 2
A addEncodedPolyline() 0 7 2
A addMarkers() 0 3 1
A removeInfoWindow() 0 5 2
A setCircles() 0 7 2
A setInfoWindows() 0 7 2
A addInfoWindow() 0 7 2
A setMarkerCluster() 0 6 2
A addRectangle() 0 7 2
A hasRectangles() 0 3 1
A addCircle() 0 7 2
A getEncodedPolylines() 0 3 1
A hasMarkers() 0 3 1
A addPolygon() 0 7 2
A addGroundOverlays() 0 4 2
A setEncodedPolylines() 0 7 2
A __construct() 0 3 1
A getMarkerCluster() 0 3 1
A hasCircles() 0 3 1
A addRectangles() 0 4 2
A addInfoWindows() 0 4 2
A hasInfoWindow() 0 3 1
A getPolygons() 0 3 1
A hasMap() 0 3 1
A getGroundOverlays() 0 3 1
A addMarker() 0 3 1
A removeEncodedPolyline() 0 5 2
A hasPolygons() 0 3 1
A setPolygons() 0 7 2
A addPolylines() 0 4 2
A hasPolyline() 0 3 1
A hasPolygon() 0 3 1
A addExtendable() 0 4 2
A hasEncodedPolylines() 0 3 1
A removeExtendable() 0 4 2
A addPolygons() 0 4 2
A setRectangles() 0 7 2
A addGroundOverlay() 0 7 2
A hasGroundOverlays() 0 3 1
A removePolygon() 0 5 2
A addEncodedPolylines() 0 4 2
A hasRectangle() 0 3 1
A hasPolylines() 0 3 1
A getCircles() 0 3 1
A getMarkers() 0 3 1
A hasMarker() 0 3 1
A setMap() 0 6 2
A getMap() 0 3 1
A setMarkers() 0 3 1
A hasEncodedPolyline() 0 3 1
A removeMarker() 0 3 1
A setGroundOverlays() 0 7 2
A setPolylines() 0 7 2
A addCircles() 0 4 2
A isAutoZoom() 0 3 2
A removeRectangle() 0 5 2
A getInfoWindows() 0 3 1
A getRectangles() 0 3 1
A getPolylines() 0 3 1
A hasInfoWindows() 0 3 1
A hasCircle() 0 3 1
A addPolyline() 0 7 2

How to fix   Complexity   

Complex Class

Complex classes like OverlayManager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use OverlayManager, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Ivory Google Map package.
7
 *
8
 * (c) Eric GELOEN <[email protected]>
9
 *
10
 * For the full copyright and license information, please read the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Ivory\GoogleMap\Overlay;
15
16
use Ivory\GoogleMap\Map;
17
18
class OverlayManager
19
{
20
    /** @var Map|null */
21
    private $map;
22
23
    /** @var MarkerCluster */
24
    private $markerCluster;
25
26
    /** @var InfoWindow[] */
27
    private $infoWindows = [];
28
29
    /** @var Polyline[] */
30
    private $polylines = [];
31
32
    /** @var EncodedPolyline[] */
33
    private $encodedPolylines = [];
34
35
    /** @var Polygon[] */
36
    private $polygons = [];
37
38
    /** @var Rectangle[] */
39
    private $rectangles = [];
40
41
    /** @var Circle[] */
42
    private $circles = [];
43
44
    /** @var GroundOverlay[] */
45
    private $groundOverlays = [];
46
47 161
    public function __construct()
48
    {
49 161
        $this->setMarkerCluster(new MarkerCluster());
50 161
    }
51
52 83
    public function hasMap(): bool
53
    {
54 83
        return null !== $this->map;
55
    }
56
57 153
    public function getMap(): ?Map
58
    {
59 153
        return $this->map;
60
    }
61
62 161
    public function setMap(Map $map): void
63
    {
64 161
        $this->map = $map;
65
66 161
        if ($map->getOverlayManager() !== $this) {
67 66
            $map->setOverlayManager($this);
68
        }
69 161
    }
70
71 161
    public function getMarkerCluster(): ?MarkerCluster
72
    {
73 161
        return $this->markerCluster;
74
    }
75
76 161
    public function setMarkerCluster(MarkerCluster $markerCluster): void
77
    {
78 161
        $this->markerCluster = $markerCluster;
79
80 161
        if ($markerCluster->getOverlayManager() !== $this) {
81 161
            $markerCluster->setOverlayManager($this);
82
        }
83 161
    }
84
85 2
    public function hasMarkers(): bool
86
    {
87 2
        return $this->markerCluster->hasMarkers();
88
    }
89
90
    /** @return Marker[] */
91 27
    public function getMarkers(): array
92
    {
93 27
        return $this->markerCluster->getMarkers();
94
    }
95
96
    /** @param Marker[] $markers */
97 1
    public function setMarkers(array $markers): void
98
    {
99 1
        $this->markerCluster->setMarkers($markers);
100 1
    }
101
102
    /** @param Marker[] $markers */
103 1
    public function addMarkers(array $markers): void
104
    {
105 1
        $this->markerCluster->addMarkers($markers);
106 1
    }
107
108 1
    public function hasMarker(Marker $marker): bool
109
    {
110 1
        return $this->markerCluster->hasMarker($marker);
111
    }
112
113 9
    public function addMarker(Marker $marker): void
114
    {
115 9
        $this->markerCluster->addMarker($marker);
116 9
    }
117
118 1
    public function removeMarker(Marker $marker): void
119
    {
120 1
        $this->markerCluster->removeMarker($marker);
121 1
    }
122
123 8
    public function hasInfoWindows(): bool
124
    {
125 8
        return !empty($this->infoWindows);
126
    }
127
128
    /** @return InfoWindow[] */
129 26
    public function getInfoWindows(): array
130
    {
131 26
        return $this->infoWindows;
132
    }
133
134
    /** @param InfoWindow[] $infoWindows */
135 4
    public function setInfoWindows(array $infoWindows): void
136
    {
137 4
        foreach ($this->infoWindows as $infoWindow) {
138 2
            $this->removeInfoWindow($infoWindow);
139
        }
140
141 4
        $this->addInfoWindows($infoWindows);
142 4
    }
143
144
    /** @param InfoWindow[] $infoWindows */
145 4
    public function addInfoWindows(array $infoWindows): void
146
    {
147 4
        foreach ($infoWindows as $infoWindow) {
148 4
            $this->addInfoWindow($infoWindow);
149
        }
150 4
    }
151
152 10
    public function hasInfoWindow(InfoWindow $infoWindow): bool
153
    {
154 10
        return in_array($infoWindow, $this->infoWindows, true);
155
    }
156
157 10
    public function addInfoWindow(InfoWindow $infoWindow): void
158
    {
159 10
        if (!$this->hasInfoWindow($infoWindow)) {
160 10
            $this->infoWindows[] = $infoWindow;
161
        }
162
163 10
        $this->addExtendable($infoWindow);
164 10
    }
165
166 4
    public function removeInfoWindow(InfoWindow $infoWindow): void
167
    {
168 4
        unset($this->infoWindows[array_search($infoWindow, $this->infoWindows, true)]);
169 4
        $this->infoWindows = empty($this->infoWindows) ? [] : array_values($this->infoWindows);
170 4
        $this->removeExtendable($infoWindow);
171 4
    }
172
173 8
    public function hasPolylines(): bool
174
    {
175 8
        return !empty($this->polylines);
176
    }
177
178
    /** @return Polyline[] */
179 21
    public function getPolylines(): array
180
    {
181 21
        return $this->polylines;
182
    }
183
184
    /** @param Polyline[] $polylines */
185 4
    public function setPolylines(array $polylines): void
186
    {
187 4
        foreach ($this->polylines as $polyline) {
188 2
            $this->removePolyline($polyline);
189
        }
190
191 4
        $this->addPolylines($polylines);
192 4
    }
193
194
    /** @param Polyline[] $polylines */
195 4
    public function addPolylines(array $polylines): void
196
    {
197 4
        foreach ($polylines as $polyline) {
198 4
            $this->addPolyline($polyline);
199
        }
200 4
    }
201
202 13
    public function hasPolyline(Polyline $polyline): bool
203
    {
204 13
        return in_array($polyline, $this->polylines, true);
205
    }
206
207 13
    public function addPolyline(Polyline $polyline): void
208
    {
209 13
        if (!$this->hasPolyline($polyline)) {
210 13
            $this->polylines[] = $polyline;
211
        }
212
213 13
        $this->addExtendable($polyline);
214 13
    }
215
216 4
    public function removePolyline(Polyline $polyline): void
217
    {
218 4
        unset($this->polylines[array_search($polyline, $this->polylines, true)]);
219 4
        $this->polylines = empty($this->polylines) ? [] : array_values($this->polylines);
220 4
        $this->removeExtendable($polyline);
221 4
    }
222
223 8
    public function hasEncodedPolylines(): bool
224
    {
225 8
        return !empty($this->encodedPolylines);
226
    }
227
228
    /** @return EncodedPolyline[] */
229 10
    public function getEncodedPolylines(): array
230
    {
231 10
        return $this->encodedPolylines;
232
    }
233
234
    /** @param EncodedPolyline[] $encodedPolylines */
235 4
    public function setEncodedPolylines(array $encodedPolylines): void
236
    {
237 4
        foreach ($this->encodedPolylines as $encodedPolyline) {
238 2
            $this->removeEncodedPolyline($encodedPolyline);
239
        }
240
241 4
        $this->addEncodedPolylines($encodedPolylines);
242 4
    }
243
244
    /** @param EncodedPolyline[] $encodedPolylines */
245 4
    public function addEncodedPolylines(array $encodedPolylines): void
246
    {
247 4
        foreach ($encodedPolylines as $encodedPolyline) {
248 4
            $this->addEncodedPolyline($encodedPolyline);
249
        }
250 4
    }
251
252 10
    public function hasEncodedPolyline(EncodedPolyline $encodedPolyline): bool
253
    {
254 10
        return in_array($encodedPolyline, $this->encodedPolylines, true);
255
    }
256
257 10
    public function addEncodedPolyline(EncodedPolyline $encodedPolyline): void
258
    {
259 10
        if (!$this->hasEncodedPolyline($encodedPolyline)) {
260 10
            $this->encodedPolylines[] = $encodedPolyline;
261
        }
262
263 10
        $this->addExtendable($encodedPolyline);
264 10
    }
265
266 4
    public function removeEncodedPolyline(EncodedPolyline $encodedPolyline): void
267
    {
268 4
        unset($this->encodedPolylines[array_search($encodedPolyline, $this->encodedPolylines, true)]);
269 4
        $this->encodedPolylines = empty($this->encodedPolylines) ? [] : array_values($this->encodedPolylines);
270 4
        $this->removeExtendable($encodedPolyline);
271 4
    }
272
273 8
    public function hasPolygons(): bool
274
    {
275 8
        return !empty($this->polygons);
276
    }
277
278
    /** @return Polygon[] */
279 18
    public function getPolygons(): array
280
    {
281 18
        return $this->polygons;
282
    }
283
284
    /** @param Polygon[] $polygons */
285 4
    public function setPolygons(array $polygons): void
286
    {
287 4
        foreach ($this->polygons as $polygon) {
288 2
            $this->removePolygon($polygon);
289
        }
290
291 4
        $this->addPolygons($polygons);
292 4
    }
293
294
    /** @param Polygon[] $polygons */
295 4
    public function addPolygons(array $polygons): void
296
    {
297 4
        foreach ($polygons as $polygon) {
298 4
            $this->addPolygon($polygon);
299
        }
300 4
    }
301
302 10
    public function hasPolygon(Polygon $polygon): bool
303
    {
304 10
        return in_array($polygon, $this->polygons, true);
305
    }
306
307 10
    public function addPolygon(Polygon $polygon): void
308
    {
309 10
        if (!$this->hasPolygon($polygon)) {
310 10
            $this->polygons[] = $polygon;
311
        }
312
313 10
        $this->addExtendable($polygon);
314 10
    }
315
316 4
    public function removePolygon(Polygon $polygon): void
317
    {
318 4
        unset($this->polygons[array_search($polygon, $this->polygons, true)]);
319 4
        $this->polygons = empty($this->polygons) ? [] : array_values($this->polygons);
320 4
        $this->removeExtendable($polygon);
321 4
    }
322
323 8
    public function hasRectangles(): bool
324
    {
325 8
        return !empty($this->rectangles);
326
    }
327
328
    /** @return Rectangle[] */
329 22
    public function getRectangles(): array
330
    {
331 22
        return $this->rectangles;
332
    }
333
334
    /** @param Rectangle[] $rectangles */
335 4
    public function setRectangles(array $rectangles): void
336
    {
337 4
        foreach ($this->rectangles as $rectangle) {
338 2
            $this->removeRectangle($rectangle);
339
        }
340
341 4
        $this->addRectangles($rectangles);
342 4
    }
343
344
    /** @param Rectangle[] $rectangles */
345 4
    public function addRectangles(array $rectangles): void
346
    {
347 4
        foreach ($rectangles as $rectangle) {
348 4
            $this->addRectangle($rectangle);
349
        }
350 4
    }
351
352 11
    public function hasRectangle(Rectangle $rectangle): bool
353
    {
354 11
        return in_array($rectangle, $this->rectangles, true);
355
    }
356
357 11
    public function addRectangle(Rectangle $rectangle): void
358
    {
359 11
        if (!$this->hasRectangle($rectangle)) {
360 11
            $this->rectangles[] = $rectangle;
361
        }
362
363 11
        $this->addExtendable($rectangle);
364 11
    }
365
366 4
    public function removeRectangle(Rectangle $rectangle): void
367
    {
368 4
        unset($this->rectangles[array_search($rectangle, $this->rectangles, true)]);
369 4
        $this->rectangles = empty($this->rectangles) ? [] : array_values($this->rectangles);
370 4
        $this->removeExtendable($rectangle);
371 4
    }
372
373 8
    public function hasCircles(): bool
374
    {
375 8
        return !empty($this->circles);
376
    }
377
378
    /** @return Circle[] */
379 18
    public function getCircles(): array
380
    {
381 18
        return $this->circles;
382
    }
383
384
    /** @param Circle[] $circles */
385 4
    public function setCircles(array $circles): void
386
    {
387 4
        foreach ($this->circles as $circle) {
388 2
            $this->removeCircle($circle);
389
        }
390
391 4
        $this->addCircles($circles);
392 4
    }
393
394
    /** @param Circle[] $circles */
395 4
    public function addCircles(array $circles): void
396
    {
397 4
        foreach ($circles as $circle) {
398 4
            $this->addCircle($circle);
399
        }
400 4
    }
401
402 10
    public function hasCircle(Circle $circle): bool
403
    {
404 10
        return in_array($circle, $this->circles, true);
405
    }
406
407 10
    public function addCircle(Circle $circle): void
408
    {
409 10
        if (!$this->hasCircle($circle)) {
410 10
            $this->circles[] = $circle;
411
        }
412
413 10
        $this->addExtendable($circle);
414 10
    }
415
416 4
    public function removeCircle(Circle $circle): void
417
    {
418 4
        unset($this->circles[array_search($circle, $this->circles, true)]);
419 4
        $this->circles = empty($this->circles) ? [] : array_values($this->circles);
420 4
        $this->removeExtendable($circle);
421 4
    }
422
423 8
    public function hasGroundOverlays(): bool
424
    {
425 8
        return !empty($this->groundOverlays);
426
    }
427
428
    /** @return GroundOverlay[] */
429 22
    public function getGroundOverlays(): array
430
    {
431 22
        return $this->groundOverlays;
432
    }
433
434
    /** @param GroundOverlay[] $groundOverlays */
435 4
    public function setGroundOverlays(array $groundOverlays): void
436
    {
437 4
        foreach ($this->groundOverlays as $groundOverlay) {
438 2
            $this->removeGroundOverlay($groundOverlay);
439
        }
440
441 4
        $this->addGroundOverlays($groundOverlays);
442 4
    }
443
444
    /** @param GroundOverlay[] $groundOverlays */
445 4
    public function addGroundOverlays(array $groundOverlays): void
446
    {
447 4
        foreach ($groundOverlays as $groundOverlay) {
448 4
            $this->addGroundOverlay($groundOverlay);
449
        }
450 4
    }
451
452 10
    public function hasGroundOverlay(GroundOverlay $groundOverlay): bool
453
    {
454 10
        return in_array($groundOverlay, $this->groundOverlays, true);
455
    }
456
457 10
    public function addGroundOverlay(GroundOverlay $groundOverlay): void
458
    {
459 10
        if (!$this->hasGroundOverlay($groundOverlay)) {
460 10
            $this->groundOverlays[] = $groundOverlay;
461
        }
462
463 10
        $this->addExtendable($groundOverlay);
464 10
    }
465
466 4
    public function removeGroundOverlay(GroundOverlay $groundOverlay): void
467
    {
468 4
        unset($this->groundOverlays[array_search($groundOverlay, $this->groundOverlays, true)]);
469 4
        $this->groundOverlays = empty($this->groundOverlays) ? [] : array_values($this->groundOverlays);
470 4
        $this->removeExtendable($groundOverlay);
471 4
    }
472
473 74
    private function addExtendable(ExtendableInterface $extendable): void
474
    {
475 74
        if ($this->isAutoZoom()) {
476 28
            $this->getMap()->getBound()->addExtendable($extendable);
477
        }
478 74
    }
479
480 28
    private function removeExtendable(ExtendableInterface $extendable): void
481
    {
482 28
        if ($this->isAutoZoom()) {
483 14
            $this->getMap()->getBound()->removeExtendable($extendable);
484
        }
485 28
    }
486
487 74
    private function isAutoZoom(): bool
488
    {
489 74
        return $this->hasMap() && $this->getMap()->isAutoZoom();
490
    }
491
}
492