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\Overlay; |
13
|
|
|
|
14
|
|
|
use Ivory\GoogleMap\Helper\Collector\Overlay\InfoBoxCollector; |
15
|
|
|
use Ivory\GoogleMap\Helper\Collector\Overlay\InfoWindowCollector; |
16
|
|
|
use Ivory\GoogleMap\Helper\Event\ApiEvent; |
17
|
|
|
use Ivory\GoogleMap\Helper\Event\ApiEvents; |
18
|
|
|
use Ivory\GoogleMap\Helper\Event\MapEvent; |
19
|
|
|
use Ivory\GoogleMap\Helper\Event\MapEvents; |
20
|
|
|
use Ivory\GoogleMap\Helper\Formatter\Formatter; |
21
|
|
|
use Ivory\GoogleMap\Helper\Renderer\Overlay\InfoBoxRenderer; |
22
|
|
|
use Ivory\GoogleMap\Map; |
23
|
|
|
use Ivory\GoogleMap\Overlay\InfoWindow; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @author GeLo <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class InfoBoxSubscriber extends AbstractInfoWindowSubscriber |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var InfoBoxRenderer |
32
|
|
|
*/ |
33
|
|
|
private $infoBoxRenderer; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param Formatter $formatter |
37
|
|
|
* @param InfoBoxCollector $infoBoxCollector |
38
|
|
|
* @param InfoBoxRenderer $infoBoxRenderer |
39
|
|
|
*/ |
40
|
272 |
|
public function __construct( |
41
|
|
|
Formatter $formatter, |
42
|
|
|
InfoBoxCollector $infoBoxCollector, |
43
|
|
|
InfoBoxRenderer $infoBoxRenderer |
44
|
|
|
) { |
45
|
272 |
|
parent::__construct($formatter, $infoBoxCollector); |
46
|
|
|
|
47
|
272 |
|
$this->setInfoBoxRenderer($infoBoxRenderer); |
48
|
272 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return InfoBoxRenderer |
52
|
|
|
*/ |
53
|
28 |
|
public function getInfoBoxRenderer() |
54
|
|
|
{ |
55
|
28 |
|
return $this->infoBoxRenderer; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param InfoBoxRenderer $infoBoxRenderer |
60
|
|
|
*/ |
61
|
272 |
|
public function setInfoBoxRenderer(InfoBoxRenderer $infoBoxRenderer) |
62
|
|
|
{ |
63
|
272 |
|
$this->infoBoxRenderer = $infoBoxRenderer; |
64
|
272 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param ApiEvent $event |
68
|
|
|
*/ |
69
|
264 |
|
public function handleApi(ApiEvent $event) |
70
|
|
|
{ |
71
|
264 |
|
foreach ($event->getObjects(Map::class) as $map) { |
72
|
220 |
|
$infoBoxes = $this->getInfoWindowCollector()->collect($map); |
73
|
|
|
|
74
|
220 |
|
if (!empty($infoBoxes)) { |
75
|
28 |
|
$event->addSource($this->getInfoBoxRenderer()->renderSource()); |
76
|
28 |
|
$event->addRequirement($map, $this->getInfoBoxRenderer()->renderRequirement()); |
77
|
|
|
|
78
|
124 |
|
continue; |
79
|
|
|
} |
80
|
132 |
|
} |
81
|
264 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param MapEvent $event |
85
|
|
|
*/ |
86
|
220 |
View Code Duplication |
public function handleMap(MapEvent $event) |
|
|
|
|
87
|
|
|
{ |
88
|
220 |
|
$map = $event->getMap(); |
89
|
220 |
|
$collector = $this->getInfoWindowCollector(); |
90
|
|
|
|
91
|
220 |
|
foreach ($collector->collect($map, [], InfoWindowCollector::STRATEGY_MAP) as $infoWindow) { |
92
|
20 |
|
$event->addCode($this->renderInfoBox($map, $infoWindow)); |
93
|
110 |
|
} |
94
|
|
|
|
95
|
220 |
|
foreach ($collector->collect($map, [], InfoWindowCollector::STRATEGY_MARKER) as $infoWindow) { |
96
|
8 |
|
$event->addCode($this->renderInfoBox($map, $infoWindow, false)); |
97
|
110 |
|
} |
98
|
220 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritdoc} |
102
|
|
|
*/ |
103
|
272 |
|
public static function getSubscribedEvents() |
104
|
|
|
{ |
105
|
|
|
return [ |
106
|
272 |
|
ApiEvents::JAVASCRIPT_MAP => 'handleApi', |
107
|
272 |
|
MapEvents::JAVASCRIPT_OVERLAY_INFO_WINDOW => 'handleMap', |
108
|
136 |
|
]; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param Map $map |
113
|
|
|
* @param InfoWindow $infoWindow |
114
|
|
|
* @param bool $position |
115
|
|
|
* |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
28 |
|
private function renderInfoBox(Map $map, InfoWindow $infoWindow, $position = true) |
119
|
|
|
{ |
120
|
28 |
|
return $this->getFormatter()->renderContainerAssignment( |
121
|
14 |
|
$map, |
122
|
28 |
|
$this->getInfoBoxRenderer()->render($infoWindow, $position), |
123
|
28 |
|
'overlays.info_boxes', |
124
|
|
|
$infoWindow |
125
|
14 |
|
); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
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.