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\Subscriber\Layer; |
13
|
|
|
|
14
|
|
|
use Ivory\GoogleMap\Helper\Collector\Layer\HeatmapLayerCollector; |
15
|
|
|
use Ivory\GoogleMap\Helper\Event\ApiEvent; |
16
|
|
|
use Ivory\GoogleMap\Helper\Event\ApiEvents; |
17
|
|
|
use Ivory\GoogleMap\Helper\Event\MapEvent; |
18
|
|
|
use Ivory\GoogleMap\Helper\Event\MapEvents; |
19
|
|
|
use Ivory\GoogleMap\Helper\Formatter\Formatter; |
20
|
|
|
use Ivory\GoogleMap\Helper\Renderer\Layer\HeatmapLayerRenderer; |
21
|
|
|
use Ivory\GoogleMap\Helper\Subscriber\AbstractSubscriber; |
22
|
|
|
use Ivory\GoogleMap\Map; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @author GeLo <[email protected]> |
26
|
|
|
*/ |
27
|
|
View Code Duplication |
class HeatmapLayerSubscriber extends AbstractSubscriber |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var HeatmapLayerCollector |
31
|
|
|
*/ |
32
|
|
|
private $heatmapLayerCollector; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var HeatmapLayerRenderer |
36
|
|
|
*/ |
37
|
|
|
private $heatmapLayerRenderer; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param Formatter $formatter |
41
|
|
|
* @param HeatmapLayerCollector $heatmapLayerCollector |
42
|
|
|
* @param HeatmapLayerRenderer $heatmapLayerRenderer |
43
|
|
|
*/ |
44
|
|
|
public function __construct( |
45
|
|
|
Formatter $formatter, |
46
|
|
|
HeatmapLayerCollector $heatmapLayerCollector, |
47
|
|
|
HeatmapLayerRenderer $heatmapLayerRenderer |
48
|
|
|
) { |
49
|
|
|
parent::__construct($formatter); |
50
|
|
|
|
51
|
|
|
$this->setHeatmapLayerCollector($heatmapLayerCollector); |
52
|
|
|
$this->setHeatmapLayerRenderer($heatmapLayerRenderer); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return HeatmapLayerCollector |
57
|
|
|
*/ |
58
|
|
|
public function getHeatmapLayerCollector() |
59
|
|
|
{ |
60
|
|
|
return $this->heatmapLayerCollector; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param HeatmapLayerCollector $heatmapLayerCollector |
65
|
|
|
*/ |
66
|
|
|
public function setHeatmapLayerCollector(HeatmapLayerCollector $heatmapLayerCollector) |
67
|
|
|
{ |
68
|
|
|
$this->heatmapLayerCollector = $heatmapLayerCollector; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return HeatmapLayerRenderer |
73
|
|
|
*/ |
74
|
|
|
public function getHeatmapLayerRenderer() |
75
|
|
|
{ |
76
|
|
|
return $this->heatmapLayerRenderer; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param HeatmapLayerRenderer $heatmapLayerRenderer |
81
|
|
|
*/ |
82
|
|
|
public function setHeatmapLayerRenderer(HeatmapLayerRenderer $heatmapLayerRenderer) |
83
|
|
|
{ |
84
|
|
|
$this->heatmapLayerRenderer = $heatmapLayerRenderer; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param ApiEvent $event |
89
|
|
|
*/ |
90
|
|
|
public function handleApi(ApiEvent $event) |
91
|
|
|
{ |
92
|
|
|
foreach ($event->getObjects(Map::class) as $map) { |
93
|
|
|
$heatmapLayers = $this->heatmapLayerCollector->collect($map); |
94
|
|
|
|
95
|
|
|
if (!empty($heatmapLayers)) { |
96
|
|
|
$event->addLibrary('visualization'); |
97
|
|
|
|
98
|
|
|
break; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param MapEvent $event |
105
|
|
|
*/ |
106
|
|
|
public function handleMap(MapEvent $event) |
107
|
|
|
{ |
108
|
|
|
$formatter = $this->getFormatter(); |
109
|
|
|
$map = $event->getMap(); |
110
|
|
|
|
111
|
|
|
foreach ($this->heatmapLayerCollector->collect($map) as $heatmapLayer) { |
112
|
|
|
$event->addCode($formatter->renderContainerAssignment( |
113
|
|
|
$map, |
114
|
|
|
$this->heatmapLayerRenderer->render($heatmapLayer, $map), |
115
|
|
|
'layers.heatmap_layers', |
116
|
|
|
$heatmapLayer |
117
|
|
|
)); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* {@inheritdoc} |
123
|
|
|
*/ |
124
|
|
|
public static function getSubscribedEvents() |
125
|
|
|
{ |
126
|
|
|
return [ |
127
|
|
|
ApiEvents::JAVASCRIPT_MAP => 'handleApi', |
128
|
|
|
MapEvents::JAVASCRIPT_LAYER_HEATMAP_LAYER => 'handleMap' |
129
|
|
|
]; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
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.