Completed
Pull Request — master (#11)
by
unknown
33:00
created

MarkerInfoWindowOpenSubscriber::handleMap()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 0
cts 11
cp 0
rs 8.9777
c 0
b 0
f 0
cc 6
nc 5
nop 1
crap 42
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\Event\Event;
15
use Ivory\GoogleMap\Helper\Collector\Overlay\MarkerCollector;
16
use Ivory\GoogleMap\Helper\Event\MapEvent;
17
use Ivory\GoogleMap\Helper\Event\MapEvents;
18
use Ivory\GoogleMap\Helper\Formatter\Formatter;
19
use Ivory\GoogleMap\Helper\Renderer\Event\EventRenderer;
20
use Ivory\GoogleMap\Helper\Renderer\Overlay\InfoWindowOpenRenderer;
21
use Ivory\GoogleMap\Map;
22
use Ivory\GoogleMap\Overlay\Marker;
23
24
/**
25
 * @author GeLo <[email protected]>
26
 */
27
class MarkerInfoWindowOpenSubscriber extends AbstractMarkerSubscriber
28
{
29
    /**
30
     * @var InfoWindowOpenRenderer
31
     */
32
    private $infoWindowOpenRenderer;
33
34
    /**
35
     * @var EventRenderer
36
     */
37
    private $eventRenderer;
38
39 4
    public function __construct(
40
        Formatter $formatter,
41
        MarkerCollector $markerCollector,
42
        InfoWindowOpenRenderer $infoWindowOpenRenderer,
43
        EventRenderer $eventRenderer
44
    ) {
45 4
        parent::__construct($formatter, $markerCollector);
46
47 4
        $this->setInfoWindowOpenRenderer($infoWindowOpenRenderer);
48 4
        $this->setEventRenderer($eventRenderer);
49 4
    }
50
51
    /**
52
     * @return InfoWindowOpenRenderer
53
     */
54
    public function getInfoWindowOpenRenderer()
55
    {
56
        return $this->infoWindowOpenRenderer;
57
    }
58
59 4
    public function setInfoWindowOpenRenderer(InfoWindowOpenRenderer $infoWindowOpenRenderer)
60
    {
61 4
        $this->infoWindowOpenRenderer = $infoWindowOpenRenderer;
62 4
    }
63
64
    /**
65
     * @return EventRenderer
66
     */
67
    public function getEventRenderer()
68
    {
69
        return $this->eventRenderer;
70
    }
71
72 4
    public function setEventRenderer(EventRenderer $eventRenderer)
73
    {
74 4
        $this->eventRenderer = $eventRenderer;
75 4
    }
76
77
    public function handleMap(MapEvent $event)
78
    {
79
        $formatter = $this->getFormatter();
80
        $map = $event->getMap();
81
82
        foreach ($this->getMarkerCollector()->collect($map) as $marker) {
83
            if ($marker->hasInfoWindow() && $marker->getInfoWindow()->isAutoOpen()) {
84
                $event->addCode($formatter->renderContainerAssignment(
85
                    $map,
86
                    $this->getEventRenderer()->render($rawEvent = $this->createEvent($map, $marker)),
87
                    'events.events',
88
                    $rawEvent
89
                ));
90
            }
91
            if ($marker->hasInfoWindow() && $marker->getInfoWindow()->isOpen()) {
92
                $event->addCode($formatter->renderCode($this->infoWindowOpenRenderer->render($marker->getInfoWindow(), $map, $marker)));
0 ignored issues
show
Bug introduced by
It seems like $marker->getInfoWindow() can be null; however, render() 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...
93
                $marker->getInfoWindow()->setOpen(false);
94
            }
95
        }
96
    }
97 4
98
    /**
99 4
     * {@inheritdoc}
100
     */
101
    public static function getSubscribedEvents()
102
    {
103
        return [MapEvents::JAVASCRIPT_EVENT => 'handleMap'];
104
    }
105
106
    /**
107
     * @return Event
108
     */
109
    private function createEvent(Map $map, Marker $marker)
110
    {
111
        $formatter = $this->getFormatter();
112
113
        $rawEvent = new Event(
114
            $marker->getVariable(),
115
            $marker->getInfoWindow()->getOpenEvent(),
116
            $formatter->renderClosure($formatter->renderLines([
117
                $formatter->renderCall(
118
                    $formatter->renderProperty(
119
                        $formatter->renderContainerVariable($map, 'functions'),
120
                        'info_windows_close'
121
                    ),
122
                    [],
123
                    true
124
                ),
125
                $formatter->renderCode(
126
                    $this->getInfoWindowOpenRenderer()->render($marker->getInfoWindow(), $map, $marker),
0 ignored issues
show
Bug introduced by
It seems like $marker->getInfoWindow() can be null; however, render() 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...
127
                    true,
128
                    false
129
                ),
130
            ]))
131
        );
132
133
        $rawEvent->setVariable($marker->getVariable().'_info_window_open_event');
134
135
        return $rawEvent;
136
    }
137
}
138