CoordinateCollector::collect()   F
last analyzed

Complexity

Conditions 11
Paths 576

Size

Total Lines 41

Duplication

Lines 6
Ratio 14.63 %

Code Coverage

Tests 21
CRAP Score 11

Importance

Changes 0
Metric Value
dl 6
loc 41
ccs 21
cts 21
cp 1
rs 3.7388
c 0
b 0
f 0
cc 11
nc 576
nop 2
crap 11

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Helper\Collector\Base;
13
14
use Ivory\GoogleMap\Base\Coordinate;
15
use Ivory\GoogleMap\Helper\Collector\AbstractCollector;
16
use Ivory\GoogleMap\Helper\Collector\Layer\HeatmapLayerCollector;
17
use Ivory\GoogleMap\Helper\Collector\Overlay\CircleCollector;
18
use Ivory\GoogleMap\Helper\Collector\Overlay\InfoWindowCollector;
19
use Ivory\GoogleMap\Helper\Collector\Overlay\MarkerCollector;
20
use Ivory\GoogleMap\Helper\Collector\Overlay\PolygonCollector;
21
use Ivory\GoogleMap\Helper\Collector\Overlay\PolylineCollector;
22
use Ivory\GoogleMap\Map;
23
24
/**
25
 * @author GeLo <[email protected]>
26
 */
27
class CoordinateCollector extends AbstractCollector
28
{
29
    /**
30
     * @var BoundCollector
31
     */
32
    private $boundCollector;
33
34
    /**
35
     * @var CircleCollector
36
     */
37
    private $circleCollector;
38
39
    /**
40
     * @var InfoWindowCollector
41
     */
42
    private $infoWindowCollector;
43
44
    /**
45
     * @var MarkerCollector
46
     */
47
    private $markerCollector;
48
49
    /**
50
     * @var PolygonCollector
51
     */
52
    private $polygonCollector;
53
54
    /**
55
     * @var PolylineCollector
56
     */
57
    private $polylineCollector;
58
59
    /**
60
     * @var HeatmapLayerCollector
61
     */
62
    private $heatmapLayerCollector;
63
64 68
    public function __construct(
65
        BoundCollector $boundCollector,
66
        CircleCollector $circleCollector,
67
        InfoWindowCollector $infoWindowCollector,
68
        MarkerCollector $markerCollector,
69
        PolygonCollector $polygonCollector,
70
        PolylineCollector $polylineCollector,
71
        HeatmapLayerCollector $heatmapLayerCollector
72
    ) {
73 68
        $this->setBoundCollector($boundCollector);
74 68
        $this->setCircleCollector($circleCollector);
75 68
        $this->setInfoWindowCollector($infoWindowCollector);
76 68
        $this->setMarkerCollector($markerCollector);
77 68
        $this->setPolygonCollector($polygonCollector);
78 68
        $this->setPolylineCollector($polylineCollector);
79 68
        $this->setHeatmapLayerCollector($heatmapLayerCollector);
80 68
    }
81
82
    /**
83
     * @return BoundCollector
84
     */
85 4
    public function getBoundCollector()
86
    {
87 4
        return $this->boundCollector;
88
    }
89
90 68
    public function setBoundCollector(BoundCollector $boundCollector)
91
    {
92 68
        $this->boundCollector = $boundCollector;
93 68
    }
94
95
    /**
96
     * @return CircleCollector
97
     */
98 4
    public function getCircleCollector()
99
    {
100 4
        return $this->circleCollector;
101
    }
102
103 68
    public function setCircleCollector(CircleCollector $circleCollector)
104
    {
105 68
        $this->circleCollector = $circleCollector;
106 68
    }
107
108
    /**
109
     * @return InfoWindowCollector
110
     */
111 4
    public function getInfoWindowCollector()
112
    {
113 4
        return $this->infoWindowCollector;
114
    }
115
116 68
    public function setInfoWindowCollector(InfoWindowCollector $infoWindowCollector)
117
    {
118 68
        $this->infoWindowCollector = $infoWindowCollector;
119 68
    }
120
121
    /**
122
     * @return MarkerCollector
123
     */
124 4
    public function getMarkerCollector()
125
    {
126 4
        return $this->markerCollector;
127
    }
128
129 68
    public function setMarkerCollector(MarkerCollector $markerCollector)
130
    {
131 68
        $this->markerCollector = $markerCollector;
132 68
    }
133
134
    /**
135
     * @return PolygonCollector
136
     */
137 4
    public function getPolygonCollector()
138
    {
139 4
        return $this->polygonCollector;
140
    }
141
142 68
    public function setPolygonCollector(PolygonCollector $polygonCollector)
143
    {
144 68
        $this->polygonCollector = $polygonCollector;
145 68
    }
146
147
    /**
148
     * @return PolylineCollector
149
     */
150 4
    public function getPolylineCollector()
151
    {
152 4
        return $this->polylineCollector;
153
    }
154
155 68
    public function setPolylineCollector(PolylineCollector $polylineCollector)
156
    {
157 68
        $this->polylineCollector = $polylineCollector;
158 68
    }
159
160
    /**
161
     * @return HeatmapLayerCollector
162
     */
163 4
    public function getHeatmapLayerCollector()
164
    {
165 4
        return $this->heatmapLayerCollector;
166
    }
167
168 68
    public function setHeatmapLayerCollector(HeatmapLayerCollector $heatmapLayerCollector)
169
    {
170 68
        $this->heatmapLayerCollector = $heatmapLayerCollector;
171 68
    }
172
173
    /**
174
     * @param Coordinate[] $coordinates
175
     *
176
     * @return Coordinate[]
177
     */
178 36
    public function collect(Map $map, array $coordinates = [])
179
    {
180 36
        if (!$map->isAutoZoom()) {
181 32
            $coordinates = $this->collectValue($map->getCenter(), $coordinates);
182
        }
183
184 36 View Code Duplication
        foreach ($this->boundCollector->collect($map) as $bound) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
185 8
            if ($bound->hasCoordinates()) {
186 4
                $coordinates = $this->collectValue($bound->getSouthWest(), $coordinates);
187 6
                $coordinates = $this->collectValue($bound->getNorthEast(), $coordinates);
188
            }
189
        }
190
191 36
        foreach ($this->circleCollector->collect($map) as $circle) {
192 4
            $coordinates = $this->collectValue($circle->getCenter(), $coordinates);
193
        }
194
195 36
        foreach ($this->infoWindowCollector->collect($map) as $infoWindow) {
196 4
            if ($infoWindow->hasPosition()) {
197 4
                $coordinates = $this->collectValue($infoWindow->getPosition(), $coordinates);
0 ignored issues
show
Bug introduced by
It seems like $infoWindow->getPosition() can be null; however, collectValue() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
198
            }
199
        }
200
201 36
        foreach ($this->markerCollector->collect($map) as $marker) {
202 4
            $coordinates = $this->collectValue($marker->getPosition(), $coordinates);
203
        }
204
205 36
        foreach ($this->polygonCollector->collect($map) as $polygon) {
206 4
            $coordinates = $this->collectValues($polygon->getCoordinates(), $coordinates);
207
        }
208
209 36
        foreach ($this->polylineCollector->collect($map) as $polyline) {
210 4
            $coordinates = $this->collectValues($polyline->getCoordinates(), $coordinates);
211
        }
212
213 36
        foreach ($this->heatmapLayerCollector->collect($map) as $heatmapLayer) {
214 4
            $coordinates = $this->collectValues($heatmapLayer->getCoordinates(), $coordinates);
215
        }
216
217 36
        return $coordinates;
218
    }
219
}
220