JvectorMap   B
last analyzed

Complexity

Total Complexity 40

Size/Duplication

Total Lines 203
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 203
rs 8.2608
c 0
b 0
f 0
wmc 40

4 Methods

Rating   Name   Duplication   Size   Complexity  
D run() 0 46 28
A init() 0 16 4
A ErrorMap() 0 14 3
B MapJs() 0 14 5

How to fix   Complexity   

Complex Class

Complex classes like JvectorMap often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use JvectorMap, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
* This file is part of the CJTTERABYTESOFT yii2-jvectormap
5
*
6
* (c) CJT TERABYTE LLC yii2-widget <https://github.com/cjtterabytesoft/yii2-jvectormap>
7
* For the full copyright and license information, please view the LICENSE.md
8
* file that was distributed with this source code
9
*
10
* @link: https://github.com/cjtterabytesoft/yii2-jvectormap
11
* @author: Wilmer Arámbula <[email protected]>
12
* @copyright: (c) CJT TERABYTE LLC
13
* @Widget: [yii2-jvectormap]
14
* @Library: [JvectorMap]
15
* @since: 0.0.1-dev
16
**/
17
18
namespace cjtterabytesoft\widget\jvectormap;
19
20
use cjtterabytesoft\widget\jvectormap\assets\JvectorMapAsset;
21
use cjtterabytesoft\widget\jvectormap\assets\MapAsset;
22
use cjtterabytesoft\widget\jvectormap\assets\MapCustomAsset;
23
use yii;
24
use yii\base\Widget;
25
use yii\helpers\BaseFileHelper;
26
use yii\helpers\Html;
27
use yii\helpers\Json;
28
29
30
class JvectorMap extends Widget
31
{
32
    /** @var string jvpath js map **/
33
    public $jvpath = '@cjtterabytesoft/widget/jvectormap/assets/maps/js/';
34
35
    /** @var string jvpathcustom js map **/
36
    public $jvpathcustom = '@webroot/maps/js/';
37
38
    /** @var string $jvname js map **/
39
    public $jvname = 'jquery-jvectormap-';
40
41
    /** @var integer $jverror js map **/
42
    public $jverror = 0;
43
44
    /** @var string $jvimage404 js map **/
45
    public $jvimage404 = '/images/errors/404-map.png';
46
47
    /** @var string $jvimage500 js map **/
48
    public $jvimage500 = '/images/errors/500-map.png';
49
50
    /** AssetBundle Dinamic **/
51
    public $bundle;
52
53
    /** @var string tag container **/
54
    public $tag = 'div';
55
56
    /** @var string $id [div] [$id - function Jquery.JVectorMap] **/
57
    public $id;
58
59
    /** @var array [div - $htmlOptions[$style]] **/
60
    public $style = [];
61
62
    /** @var array [div - $htmlOptions] **/
63
    public $htmlOptions = [];
64
65
    /** @var string [$map] **/
66
    public $map;
67
68
    /** @var boolean jvdefault js map **/
69
    public $maptype = false;
70
71
    /** @var string [$config function Jquery.JVectorMap] **/
72
    public $config;
73
74
    /** @var string [$backgroundColor] **/
75
    public $backgroundColor;
76
77
    /** @var string [$focusOn] **/
78
    public $focusOn = [];
79
80
    /** @var array [$labels] **/
81
    public $labels = [];
82
83
    /** @var array [$markerLabelStyle] **/
84
    public $markerLabelStyle = [];
85
86
    /** @var array [$markers] **/
87
    public $markers = [];
88
89
    /** @var boolean [$markersSelectable] **/
90
    public $markersSelectable = false;
91
92
    /** @var boolean [$markersSelectableOne] **/
93
    public $markersSelectableOne = false;
94
95
    /** @var array [$markerStyle] **/
96
    public $markerStyle = [];
97
98
    /** @var boolean [$panOnDrag] **/
99
    public $panOnDrag = true;
100
101
    /** @var array [$regionLabelStyle] **/
102
    public $regionLabelStyle = [];
103
104
    /** @var boolean [$regionsSelectable] **/
105
    public $regionsSelectable = true;
106
107
    /** @var boolean [$regionsSelectableOne] **/
108
    public $regionsSelectableOne = false;
109
110
    /** @var array [$regionStyle] **/
111
    public $regionStyle = [];
112
113
    /** @var array [$selectedMarkers] **/
114
    public $selectedMarkers = [];
115
116
    /** @var array [$selectedRegions] **/
117
    public $selectedRegions = [];
118
119
    /** @var array [$series] **/
120
    public $series = [];
121
122
    /** @var boolean [$zoomAnimate] **/
123
    public $zoomAnimate = false;
124
125
    /** @var integer [$zoomMax] **/
126
    public $zoomMax;
127
128
    /** @var integer [$zoomMin] **/
129
    public $zoomMin;
130
131
    /** @var boolean [$zoomOnScroll] **/
132
    public $zoomOnScroll = true;
133
134
    public function init()
135
    {
136
        parent::init();
137
        if (!file_exists(\Yii::getAlias('@webroot/images/errors/'))) {
138
            BaseFileHelper::copyDirectory(\Yii::getAlias('@cjtterabytesoft/widget/jvectormap/images/errors'),
139
                \Yii::getAlias('@frontend/web/images/errors'));
140
        }
141
        if (empty($this->id)) {
142
            $this->id = 'vmap';
143
        }
144
        $this->htmlOptions['id'] = $this->id;
145
        $this->htmlOptions['style'] = $this->style;
146
        if (empty($this->map)) {
147
            $this->jverror = 1;
148
            } else {
149
                $this->MapJs();
150
        }
151
    }
152
153
    public function run()
154
    {
155
        if ($this->jverror == 0) {
156
            $this->config = Json::encode(
157
                [
158
                    'map' => str_replace('-', '_', $this->map),
159
                    !$this->backgroundColor ?: 'backgroundColor' =>
160
                        !$this->backgroundColor ?: $this->backgroundColor,
161
                    !$this->focusOn ?: 'focusOn' =>
162
                        !$this->focusOn ?: $this->focusOn,
163
                    !$this->labels ?: 'labels' =>
164
                        !$this->labels ?: $this->labels,
165
                    !$this->markerLabelStyle ?: 'markerLabelStyle' =>
166
                        !$this->markerLabelStyle ?: $this->markerLabelStyle,
167
                    !$this->markers ?: 'markers' =>
168
                        !$this->markers ?: $this->markers,
169
                    'markersSelectable' => $this->markersSelectable,
170
                    'markersSelectableOne' => $this->markersSelectableOne,
171
                    !$this->markerStyle ?: 'markerStyle' =>
172
                        !$this->markerStyle ?: $this->markerStyle,
173
                    'panOnDrag' => $this->panOnDrag,
174
                    !$this->regionLabelStyle ?: '$regionLabelStyle' =>
175
                        !$this->regionLabelStyle ?: $this->regionLabelStyle,
176
                    'regionsSelectable' => $this->regionsSelectable,
177
                    'regionsSelectableOne' => $this->regionsSelectableOne,
178
                    !$this->selectedMarkers ?: 'selectedMarkers' =>
179
                        !$this->selectedMarkers ?: $this->selectedMarkers,
180
                    !$this->selectedRegions ?: 'selectedRegions' =>
181
                        !$this->selectedRegions ?: $this->selectedRegions,
182
                    !$this->regionStyle ?: 'regionStyle' =>
183
                        !$this->regionStyle ?: $this->regionStyle,
184
                    !$this->series ?: 'series' =>
185
                        !$this->series ?: $this->series,
186
                    'zoomAnimate' => $this->zoomAnimate,
187
                    !$this->zoomMax ?: 'zoomMax' =>
188
                        !$this->zoomMax ?: $this->zoomMax,
189
                    !$this->zoomMin ?: 'zoomMin' =>
190
                        !$this->zoomMin ?: $this->zoomMin,
191
                    'zoomOnScroll' => $this->zoomOnScroll,
192
                ]
193
            );
194
            echo Html::beginTag($this->tag, $this->htmlOptions);
195
            echo Html::endTag($this->tag);
196
            Yii::$app->view->registerJs("jQuery('#{$this->id}').vectorMap($this->config)");
197
            } else {
198
                $this->ErrorMap();
199
        }
200
    }
201
202
    public function MapJs() {
203
        $this->jvname = $this->jvname . str_replace('_', '-', $this->map) . ".js";
204
        if (file_exists(yii::getAlias((!$this->maptype ? $this->jvpath : $this->jvpathcustom) . $this->jvname))) {
205
            JvectorMapAsset::register(Yii::$app->view);
206
            $this->bundle = !$this->maptype ? MapAsset::register(Yii::$app->view) :
207
                MapCustomAsset::register(Yii::$app->view);
208
            $this->bundle->js[] = $this->jvname; // dynamic map added
209
            $this->bundle->publishOptions[] = [
210
                'only' => [
211
                    yii::getalias((!$this->maptype ? $this->jvpath : $this->jvpathcustom) . $this->jvname),
212
                ]
213
            ];
214
            } else {
215
                $this->jverror = 2;
216
        }
217
    }
218
219
    public function ErrorMap() {
220
        switch ($this->jverror) {
221
            case 1:
222
                echo Html::beginTag($this->tag, $this->htmlOptions);
223
                echo Html::img($this->jvimage500, $options = ['alt' => '500 - The Map Option is Required.',
224
                    'style' => 'max-width:100%;width:100%;']);
225
                echo Html::endTag($this->tag);
226
                break;
227
            case 2:
228
                echo Html::beginTag($this->tag, $this->htmlOptions);
229
                    echo Html::img($this->jvimage404, $options = ['alt' => '404 - Map Not Found',
230
                        'style' => 'max-width:100%;width:100%;']);
231
                echo Html::endTag($this->tag);
232
                break;
233
        }
234
    }
235
}
236