GeoSearch   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 87.18%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 5
dl 0
loc 91
ccs 34
cts 39
cp 0.8718
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B registerAssetBundle() 0 15 6
A getPluginName() 0 4 1
C encode() 0 40 7
1
<?php
2
/**
3
 * @copyright Copyright (c) 2013-2015 2amigOS! Consulting Group LLC
4
 * @link http://2amigos.us
5
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
6
 */
7
8
namespace dosamigos\leaflet\plugins\geosearch;
9
10
use dosamigos\leaflet\Plugin;
11
use yii\base\InvalidConfigException;
12
use yii\helpers\ArrayHelper;
13
use yii\web\JsExpression;
14
15
/**
16
 * GeoSearch adds geo search capabilities to your leaflet maps
17
 *
18
 * @author Antonio Ramirez <[email protected]>
19
 * @link http://www.ramirezcobos.com/
20
 * @link http://www.2amigos.us/
21
 * @package dosamigos\leaflet\plugins\geosearch
22
 */
23
class GeoSearch extends Plugin
24
{
25
    const SERVICE_BING = 'bing';
26
    const SERVICE_ESRI = 'esri';
27
    const SERVICE_GOOGLE = 'google';
28
    const SERVICE_NOKIA = 'nokia';
29
    const SERVICE_OPENSTREETMAP = 'openstreetmap';
30
31
    /**
32
     * @var string the service to register for the geosearch
33
     */
34
    public $service = self::SERVICE_OPENSTREETMAP;
35
36
    /**
37
     * Returns the plugin name
38
     * @return string
39
     */
40 1
    public function getPluginName()
41
    {
42 1
        return 'plugin:geosearch';
43
    }
44
45
    /**
46
     * Registers plugin asset bundle
47
     * @param \yii\web\View $view
48
     * @return mixed
49
     * @codeCoverageIgnore
50
     */
51
    public function registerAssetBundle($view)
52
    {
53
        switch ($this->service) {
54
            case static::SERVICE_OPENSTREETMAP:
55
            case static::SERVICE_BING:
56
            case static::SERVICE_ESRI:
57
            case static::SERVICE_GOOGLE:
58
            case static::SERVICE_NOKIA:
59
                GeoSearchAsset::register($view)->js[] = "js/l.geosearch.provider.{$this->service}.js";
60
                break;
61
            default:
62
                GeoSearchAsset::register($view);
63
        }
64
        return $this;
65
    }
66
67
    /**
68
     * Returns the javascript ready code for the object to render
69
     * @return \yii\web\JsExpression
70
     * @throws \yii\base\InvalidConfigException
71
     */
72 1
    public function encode()
73
    {
74 1
        switch ($this->service) {
75 1
            case static::SERVICE_OPENSTREETMAP:
76 1
                $provider = 'new L.GeoSearch.Provider.OpenStreetMap()';
77 1
                break;
78 1
            case static::SERVICE_BING:
79 1
                $provider = 'new L.GeoSearch.Provider.Bing()';
80 1
                break;
81 1
            case static::SERVICE_ESRI:
82 1
                $provider = 'new L.GeoSearch.Provider.Esri()';
83 1
                break;
84 1
            case static::SERVICE_GOOGLE:
85 1
                $provider = 'new L.GeoSearch.Provider.Google()';
86 1
                break;
87 1
            case static::SERVICE_NOKIA:
88 1
                $provider = 'new L.GeoSearch.Provider.Nokia()';
89 1
                break;
90 1
            default:
91 1
                throw new InvalidConfigException('"$service" holds an unrecognized service type.');
92 1
        }
93
94 1
        $this->clientOptions = ArrayHelper::merge([
95 1
            'provider' => new JsExpression($provider),
96 1
            'position' => 'topcenter',
97
            'showMarker' => true
98 1
        ], $this->clientOptions);
99
100 1
        $options = $this->getOptions();
101 1
        $name = $this->getName();
102 1
        $map = $this->map;
103
104 1
        $js = "new L.Control.GeoSearch($options).addTo($map)";
105
106 1
        if(!empty($name)) {
107 1
            $js = "var $name = $js;";
108 1
        }
109
110 1
        return new JsExpression($js);
111
    }
112
113
}
114