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

StaticMapHelper::setChannel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
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;
15
16
use Ivory\GoogleMap\Helper\Event\StaticMapEvent;
17
use Ivory\GoogleMap\Helper\Event\StaticMapEvents;
18
use Ivory\GoogleMap\Map;
19
use Ivory\GoogleMap\Service\UrlSigner;
20
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
21
22
class StaticMapHelper extends AbstractHelper
23
{
24
    /** @var string|null */
25
    private $secret;
26
27
    /** @var string|null */
28
    private $clientId;
29
30
    /** @var string|null */
31
    private $channel;
32
33 1
    public function __construct(
34
        EventDispatcherInterface $eventDispatcher,
35
        ?string $secret = null,
36
        ?string $clientId = null,
37
        ?string $channel = null
38
    ) {
39 1
        parent::__construct($eventDispatcher);
40
41 1
        $this->secret   = $secret;
42 1
        $this->clientId = $clientId;
43 1
        $this->channel  = $channel;
44 1
    }
45
46
    public function hasSecret(): bool
47
    {
48
        return null !== $this->secret;
49
    }
50
51
    public function getSecret(): ?string
52
    {
53
        return $this->secret;
54
    }
55
56
    public function setSecret(?string $secret): void
57
    {
58
        $this->secret = $secret;
59
    }
60
61
    public function hasClientId(): bool
62
    {
63
        return null !== $this->clientId;
64
    }
65
66
    public function getClientId(): ?string
67
    {
68
        return $this->clientId;
69
    }
70
71
    public function setClientId(?string $clientId): void
72
    {
73
        $this->clientId = $clientId;
74
    }
75
76
    public function hasChannel(): bool
77
    {
78
        return null !== $this->channel;
79
    }
80
81
    public function getChannel(): ?string
82
    {
83
        return $this->channel;
84
    }
85
86
    public function setChannel($channel): void
87
    {
88
        $this->channel = $channel;
89
    }
90
91
    public function render(Map $map): string
92
    {
93
        $this->getEventDispatcher()->dispatch($event = new StaticMapEvent($map), StaticMapEvents::HTML);
94
95
        $query = preg_replace('/(%5B[0-9]+%5D)+=/', '=', http_build_query($event->getParameters(), '', '&'));
96
        $url   = 'https://maps.googleapis.com/maps/api/staticmap?'.$query;
97
98
        if ($this->hasSecret()) {
99
            $url = UrlSigner::sign($url, $this->secret, $this->clientId, $this->channel);
100
        }
101
102
        return $url;
103
    }
104
}
105