LayerManager   A
last analyzed

Complexity

Total Complexity 42

Size/Duplication

Total Lines 249
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 42
lcom 2
cbo 1
dl 0
loc 249
ccs 91
cts 91
cp 1
rs 9.0399
c 0
b 0
f 0

27 Methods

Rating   Name   Duplication   Size   Complexity  
A hasMap() 0 4 1
A getMap() 0 4 1
A setMap() 0 8 2
A hasGeoJsonLayers() 0 4 1
A getGeoJsonLayers() 0 4 1
A setGeoJsonLayers() 0 5 1
A addGeoJsonLayers() 0 6 2
A hasGeoJsonLayer() 0 4 1
A addGeoJsonLayer() 0 6 2
A removeGeoJsonLayer() 0 5 2
A hasHeatmapLayers() 0 4 1
A getHeatmapLayers() 0 4 1
A setHeatmapLayers() 0 8 2
A addHeatmapLayers() 0 6 2
A hasHeatmapLayer() 0 4 1
A addHeatmapLayer() 0 8 2
A removeHeatmapLayer() 0 6 2
A hasKmlLayers() 0 4 1
A getKmlLayers() 0 4 1
A setKmlLayers() 0 8 2
A addKmlLayers() 0 6 2
A hasKmlLayer() 0 4 1
A addKmlLayer() 0 8 2
A removeKmlLayer() 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 LayerManager 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 LayerManager, 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\Layer;
13
14
use Ivory\GoogleMap\Map;
15
use Ivory\GoogleMap\Overlay\ExtendableInterface;
16
17
/**
18
 * @author GeLo <[email protected]>
19
 */
20
class LayerManager
21
{
22
    /**
23
     * @var Map|null
24
     */
25
    private $map;
26
27
    /**
28
     * @var GeoJsonLayer[]
29
     */
30
    private $geoJsonLayers = [];
31
32
    /**
33
     * @var HeatmapLayer[]
34
     */
35
    private $heatmapLayers = [];
36
37
    /**
38
     * @var KmlLayer[]
39
     */
40
    private $kmlLayers = [];
41
42
    /**
43
     * @return bool
44
     */
45 84
    public function hasMap()
46
    {
47 84
        return null !== $this->map;
48
    }
49
50
    /**
51
     * @return Map|null
52
     */
53 452
    public function getMap()
54
    {
55 452
        return $this->map;
56
    }
57
58 468
    public function setMap(Map $map)
59
    {
60 468
        $this->map = $map;
61
62 468
        if ($map->getLayerManager() !== $this) {
63 88
            $map->setLayerManager($this);
64
        }
65 468
    }
66
67
    /**
68
     * @return bool
69
     */
70 20
    public function hasGeoJsonLayers()
71
    {
72 20
        return !empty($this->geoJsonLayers);
73
    }
74
75
    /**
76
     * @return GeoJsonLayer[]
77
     */
78 24
    public function getGeoJsonLayers()
79
    {
80 24
        return $this->geoJsonLayers;
81
    }
82
83
    /**
84
     * @param GeoJsonLayer[] $geoJsonLayers
85
     */
86 8
    public function setGeoJsonLayers(array $geoJsonLayers)
87
    {
88 8
        $this->geoJsonLayers = [];
89 8
        $this->addGeoJsonLayers($geoJsonLayers);
90 8
    }
91
92
    /**
93
     * @param GeoJsonLayer[] $geoJsonLayers
94
     */
95 8
    public function addGeoJsonLayers(array $geoJsonLayers)
96
    {
97 8
        foreach ($geoJsonLayers as $geoJsonLayer) {
98 8
            $this->addGeoJsonLayer($geoJsonLayer);
99
        }
100 8
    }
101
102
    /**
103
     * @return bool
104
     */
105 20
    public function hasGeoJsonLayer(GeoJsonLayer $geoJsonLayer)
106
    {
107 20
        return in_array($geoJsonLayer, $this->geoJsonLayers, true);
108
    }
109
110 20
    public function addGeoJsonLayer(GeoJsonLayer $geoJsonLayer)
111
    {
112 20
        if (!$this->hasGeoJsonLayer($geoJsonLayer)) {
113 20
            $this->geoJsonLayers[] = $geoJsonLayer;
114
        }
115 20
    }
116
117 4
    public function removeGeoJsonLayer(GeoJsonLayer $geoJsonLayer)
118
    {
119 4
        unset($this->geoJsonLayers[array_search($geoJsonLayer, $this->geoJsonLayers, true)]);
120 4
        $this->geoJsonLayers = empty($this->geoJsonLayers) ? [] : array_values($this->geoJsonLayers);
121 4
    }
122
123
    /**
124
     * @return bool
125
     */
126 36
    public function hasHeatmapLayers()
127
    {
128 36
        return !empty($this->heatmapLayers);
129
    }
130
131
    /**
132
     * @return HeatmapLayer[]
133
     */
134 76
    public function getHeatmapLayers()
135
    {
136 76
        return $this->heatmapLayers;
137
    }
138
139
    /**
140
     * @param HeatmapLayer[] $heatmapLayers
141
     */
142 16
    public function setHeatmapLayers(array $heatmapLayers)
143
    {
144 16
        foreach ($this->heatmapLayers as $heatmapLayer) {
145 8
            $this->removeHeatmapLayer($heatmapLayer);
146
        }
147
148 16
        $this->addHeatmapLayers($heatmapLayers);
149 16
    }
150
151
    /**
152
     * @param HeatmapLayer[] $heatmapLayers
153
     */
154 16
    public function addHeatmapLayers(array $heatmapLayers)
155
    {
156 16
        foreach ($heatmapLayers as $heatmapLayer) {
157 16
            $this->addHeatmapLayer($heatmapLayer);
158
        }
159 16
    }
160
161
    /**
162
     * @return bool
163
     */
164 40
    public function hasHeatmapLayer(HeatmapLayer $heatmapLayer)
165
    {
166 40
        return in_array($heatmapLayer, $this->heatmapLayers, true);
167
    }
168
169 40
    public function addHeatmapLayer(HeatmapLayer $heatmapLayer)
170
    {
171 40
        if (!$this->hasHeatmapLayer($heatmapLayer)) {
172 40
            $this->heatmapLayers[] = $heatmapLayer;
173
        }
174
175 40
        $this->addExtendable($heatmapLayer);
176 40
    }
177
178 16
    public function removeHeatmapLayer(HeatmapLayer $heatmapLayer)
179
    {
180 16
        unset($this->heatmapLayers[array_search($heatmapLayer, $this->heatmapLayers, true)]);
181 16
        $this->heatmapLayers = empty($this->heatmapLayers) ? [] : array_values($this->heatmapLayers);
182 16
        $this->removeExtendable($heatmapLayer);
183 16
    }
184
185
    /**
186
     * @return bool
187
     */
188 36
    public function hasKmlLayers()
189
    {
190 36
        return !empty($this->kmlLayers);
191
    }
192
193
    /**
194
     * @return KmlLayer[]
195
     */
196 40
    public function getKmlLayers()
197
    {
198 40
        return $this->kmlLayers;
199
    }
200
201
    /**
202
     * @param KmlLayer[] $kmlLayers
203
     */
204 16
    public function setKmlLayers(array $kmlLayers)
205
    {
206 16
        foreach ($this->kmlLayers as $kmlLayer) {
207 8
            $this->removeKmlLayer($kmlLayer);
208
        }
209
210 16
        $this->addKmlLayers($kmlLayers);
211 16
    }
212
213
    /**
214
     * @param KmlLayer[] $kmlLayers
215
     */
216 16
    public function addKmlLayers(array $kmlLayers)
217
    {
218 16
        foreach ($kmlLayers as $kmlLayer) {
219 16
            $this->addKmlLayer($kmlLayer);
220
        }
221 16
    }
222
223
    /**
224
     * @return bool
225
     */
226 36
    public function hasKmlLayer(KmlLayer $kmlLayer)
227
    {
228 36
        return in_array($kmlLayer, $this->kmlLayers, true);
229
    }
230
231 36
    public function addKmlLayer(KmlLayer $kmlLayer)
232
    {
233 36
        if (!$this->hasKmlLayer($kmlLayer)) {
234 36
            $this->kmlLayers[] = $kmlLayer;
235
        }
236
237 36
        $this->addExtendable($kmlLayer);
238 36
    }
239
240 16
    public function removeKmlLayer(KmlLayer $kmlLayer)
241
    {
242 16
        unset($this->kmlLayers[array_search($kmlLayer, $this->kmlLayers, true)]);
243 16
        $this->kmlLayers = empty($this->kmlLayers) ? [] : array_values($this->kmlLayers);
244 16
        $this->removeExtendable($kmlLayer);
245 16
    }
246
247 76
    private function addExtendable(ExtendableInterface $extendable)
248
    {
249 76
        if ($this->isAutoZoom()) {
250 32
            $this->getMap()->getBound()->addExtendable($extendable);
251
        }
252 76
    }
253
254 32
    private function removeExtendable(ExtendableInterface $extendable)
255
    {
256 32
        if ($this->isAutoZoom()) {
257 16
            $this->getMap()->getBound()->removeExtendable($extendable);
258
        }
259 32
    }
260
261
    /**
262
     * @return bool
263
     */
264 76
    private function isAutoZoom()
265
    {
266 76
        return $this->hasMap() && $this->getMap()->isAutoZoom();
267
    }
268
}
269