Completed
Pull Request — master (#2140)
by Simon
03:54
created

Map   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 135
Duplicated Lines 61.48 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 83
loc 135
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getAssets() 0 10 2
A __construct() 0 20 2
B useGoogleMap() 37 39 1
A useTencentMap() 46 48 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Encore\Admin\Form\Field;
4
5
use Encore\Admin\Form\Field;
6
7
class Map extends Field
8
{
9
    /**
10
     * Column name.
11
     *
12
     * @var array
13
     */
14
    protected $column = [];
15
16
    /**
17
     * Get assets required by this field.
18
     *
19
     * @return array
20
     */
21
    public static function getAssets()
22
    {
23
        if (config('app.locale') == 'zh-CN') {
24
            $js = '//map.qq.com/api/js?v=2.exp';
25
        } else {
26
            $js = '//maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&key='.env('GOOGLE_API_KEY');
27
        }
28
29
        return compact('js');
30
    }
31
32
    public function __construct($column, $arguments)
33
    {
34
        $this->column['lat'] = $column;
35
        $this->column['lng'] = $arguments[0];
36
37
        array_shift($arguments);
38
39
        $this->label = $this->formatLabel($arguments);
40
        $this->id = $this->formatId($this->column);
41
42
        /*
43
         * Google map is blocked in mainland China
44
         * people in China can use Tencent map instead(;
45
         */
46
        if (config('app.locale') == 'zh-CN') {
47
            $this->useTencentMap();
48
        } else {
49
            $this->useGoogleMap();
50
        }
51
    }
52
53 View Code Duplication
    public function useGoogleMap()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54
    {
55
        $this->script = <<<EOT
56
        (function() {
57
            function initGoogleMap(name) {
58
                var lat = $('#{$this->id['lat']}');
59
                var lng = $('#{$this->id['lng']}');
60
    
61
                var LatLng = new google.maps.LatLng(lat.val(), lng.val());
62
    
63
                var options = {
64
                    zoom: 13,
65
                    center: LatLng,
66
                    panControl: false,
67
                    zoomControl: true,
68
                    scaleControl: true,
69
                    mapTypeId: google.maps.MapTypeId.ROADMAP
70
                }
71
    
72
                var container = document.getElementById("map_"+name);
73
                var map = new google.maps.Map(container, options);
74
    
75
                var marker = new google.maps.Marker({
76
                    position: LatLng,
77
                    map: map,
78
                    title: 'Drag Me!',
79
                    draggable: true
80
                });
81
    
82
                google.maps.event.addListener(marker, 'dragend', function (event) {
83
                    lat.val(event.latLng.lat());
84
                    lng.val(event.latLng.lng());
85
                });
86
            }
87
    
88
            initGoogleMap('{$this->id['lat']}{$this->id['lng']}');
89
        })();
90
EOT;
91
    }
92
93 View Code Duplication
    public function useTencentMap()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
94
    {
95
        $this->script = <<<EOT
96
        (function() {
97
            function initTencentMap(name) {
98
                var lat = $('#{$this->id['lat']}');
99
                var lng = $('#{$this->id['lng']}');
100
    
101
                var center = new qq.maps.LatLng(lat.val(), lng.val());
102
    
103
                var container = document.getElementById("map_"+name);
104
                var map = new qq.maps.Map(container, {
105
                    center: center,
106
                    zoom: 13
107
                });
108
    
109
                var marker = new qq.maps.Marker({
110
                    position: center,
111
                    draggable: true,
112
                    map: map
113
                });
114
    
115
                if( ! lat.val() || ! lng.val()) {
116
                    var citylocation = new qq.maps.CityService({
117
                        complete : function(result){
118
                            map.setCenter(result.detail.latLng);
119
                            marker.setPosition(result.detail.latLng);
120
                        }
121
                    });
122
    
123
                    citylocation.searchLocalCity();
124
                }
125
    
126
                qq.maps.event.addListener(map, 'click', function(event) {
127
                    marker.setPosition(event.latLng);
128
                });
129
    
130
                qq.maps.event.addListener(marker, 'position_changed', function(event) {
131
                    var position = marker.getPosition();
132
                    lat.val(position.getLat());
133
                    lng.val(position.getLng());
134
                });
135
            }
136
    
137
            initTencentMap('{$this->id['lat']}{$this->id['lng']}');
138
        })();
139
EOT;
140
    }
141
}
142