Passed
Push — feature/v4 ( e5e380...dac4aa )
by Samuel
11:37
created

CenterSubscriber::handleMap()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 16
ccs 0
cts 10
cp 0
rs 9.6111
cc 5
nc 9
nop 1
crap 30
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Ivory Google Map package.
7
 *
8
 * (c) Eric GELOEN <[email protected]>
9
 *
10
 * For the full copyright and license information, please read the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Ivory\GoogleMap\Helper\Subscriber\Image;
15
16
use Ivory\GoogleMap\Base\Coordinate;
17
use Ivory\GoogleMap\Helper\Event\StaticMapEvent;
18
use Ivory\GoogleMap\Helper\Event\StaticMapEvents;
19
use Ivory\GoogleMap\Helper\Renderer\Image\Base\CoordinateRenderer;
20
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
21
22
class CenterSubscriber implements EventSubscriberInterface
23
{
24
    /** @var CoordinateRenderer */
25
    private $coordinateRenderer;
26
27 1
    public function __construct(CoordinateRenderer $coordinateRenderer)
28
    {
29 1
        $this->coordinateRenderer = $coordinateRenderer;
30 1
    }
31
32
    public function handleMap(StaticMapEvent $event): void
33
    {
34
        $map = $event->getMap();
35
36
        if ($map->hasStaticOption('center')) {
37
            $center = $map->getStaticOption('center');
38
        } elseif (!$map->isAutoZoom()) {
39
            $center = $map->getCenter();
40
        }
41
42
        if (isset($center)) {
43
            if ($center instanceof Coordinate) {
44
                $center = $this->coordinateRenderer->render($center);
45
            }
46
47
            $event->setParameter('center', $center);
48
        }
49
    }
50
51
    /** {@inheritdoc} */
52 1
    public static function getSubscribedEvents(): array
53
    {
54 1
        return [StaticMapEvents::CENTER => 'handleMap'];
55
    }
56
}
57