OverlayManager   F
last analyzed

Complexity

Total Complexity 98

Size/Duplication

Total Lines 600
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 98
lcom 2
cbo 2
dl 0
loc 600
ccs 222
cts 222
cp 1
rs 2
c 0
b 0
f 0

65 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A hasMap() 0 4 1
A getMap() 0 4 1
A setMap() 0 8 2
A getMarkerCluster() 0 4 1
A setMarkerCluster() 0 8 2
A hasMarkers() 0 4 1
A getMarkers() 0 4 1
A setMarkers() 0 4 1
A addMarkers() 0 4 1
A hasMarker() 0 4 1
A addMarker() 0 4 1
A removeMarker() 0 4 1
A hasInfoWindows() 0 4 1
A getInfoWindows() 0 4 1
A setInfoWindows() 0 8 2
A addInfoWindows() 0 6 2
A hasInfoWindow() 0 4 1
A addInfoWindow() 0 8 2
A removeInfoWindow() 0 6 2
A hasPolylines() 0 4 1
A getPolylines() 0 4 1
A setPolylines() 0 8 2
A addPolylines() 0 6 2
A hasPolyline() 0 4 1
A addPolyline() 0 8 2
A removePolyline() 0 6 2
A hasEncodedPolylines() 0 4 1
A getEncodedPolylines() 0 4 1
A setEncodedPolylines() 0 8 2
A addEncodedPolylines() 0 6 2
A hasEncodedPolyline() 0 4 1
A addEncodedPolyline() 0 8 2
A removeEncodedPolyline() 0 6 2
A hasPolygons() 0 4 1
A getPolygons() 0 4 1
A setPolygons() 0 8 2
A addPolygons() 0 6 2
A hasPolygon() 0 4 1
A addPolygon() 0 8 2
A removePolygon() 0 6 2
A hasRectangles() 0 4 1
A getRectangles() 0 4 1
A setRectangles() 0 8 2
A addRectangles() 0 6 2
A hasRectangle() 0 4 1
A addRectangle() 0 8 2
A removeRectangle() 0 6 2
A hasCircles() 0 4 1
A getCircles() 0 4 1
A setCircles() 0 8 2
A addCircles() 0 6 2
A hasCircle() 0 4 1
A addCircle() 0 8 2
A removeCircle() 0 6 2
A hasGroundOverlays() 0 4 1
A getGroundOverlays() 0 4 1
A setGroundOverlays() 0 8 2
A addGroundOverlays() 0 6 2
A hasGroundOverlay() 0 4 1
A addGroundOverlay() 0 8 2
A removeGroundOverlay() 0 6 2
A addExtendable() 0 6 2
A removeExtendable() 0 6 2
A isAutoZoom() 0 4 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

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