Completed
Push — master ( 364942...120590 )
by
unknown
11s
created

LocationController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Bantenprov\WilayahIndonesia\Http\Controllers;
3
4
/* Require */
5
use App\Http\Controllers\Controller;
6
use Illuminate\Http\Request;
7
8
/* Models */
9
use Laravolt\Indonesia\Indonesia;
10
use Laravolt\Indonesia\Models\Province;
11
use Laravolt\Indonesia\Models\City;
12
use Laravolt\Indonesia\Models\District;
13
use Laravolt\Indonesia\Models\Village;
14
15
/* Etc */
16
17
/**
18
 * The LocationController class.
19
 *
20
 * @package Bantenprov\WilayahIndonesia
21
 * @author  bantenprov <[email protected]>
22
 */
23
class LocationController extends Controller
24
{
25
    protected $indonesia;
26
    protected $province;
27
    protected $city;
28
    protected $district;
29
    protected $village;
30
31
    /**
32
     * Create a new controller instance.
33
     *
34
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
35
     */
36
    public function __construct()
37
    {
38
		$this->indonesia    = new Indonesia;
39
		$this->province     = new Province;
40
		$this->city         = new City;
41
		$this->district     = new District;
42
		$this->village      = new Village;
43
    }
44
45
    /**
46
     * Display a listing of the province resource.
47
     *
48
     * @return \Illuminate\Http\Response
49
     */
50 View Code Duplication
    public function allProvince()
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...
51
    {
52
        $provinces = $this->indonesia->allProvinces();
53
54
        foreach($provinces as $province){
55
            array_set($province, 'label', $province->name);
56
        }
57
58
        $response['province']   = $provinces;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
59
        $response['status']     = true;
60
61
        return response()->json($response);
62
    }
63
64
    /**
65
     * Display a listing of the province resource.
66
     *
67
     * @return \Illuminate\Http\Response
68
     */
69 View Code Duplication
    public function detailProvince($id)
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...
70
    {
71
        $provinces = $this->indonesia->findProvince($id);
72
73
        array_set($provinces, 'label', $provinces->name);
74
75
        $response['province']   = $provinces;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
76
        $response['status']     = true;
77
78
        return response()->json($response);
79
    }
80
81
    /**
82
     * Display a listing of the city resource.
83
     *
84
     * @return \Illuminate\Http\Response
85
     */
86 View Code Duplication
    public function allCity()
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...
87
    {
88
        $cities = $this->indonesia->allCities();
89
90
        foreach($cities as $city){
91
            array_set($city, 'label', $city->name);
92
        }
93
94
        $response['city']   = $cities;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
95
        $response['status'] = true;
96
97
        return response()->json($response);
98
    }
99
100
    /**
101
     * Display a listing of the city resource.
102
     *
103
     * @return \Illuminate\Http\Response
104
     */
105 View Code Duplication
    public function allCityByProvince($id)
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...
106
    {
107
        $cities = $this->indonesia->findProvince($id, ['cities']);
108
109
        if (isset($cities->cities)) {
110
            $cities = $cities->cities;
111
        } else {
112
            $cities = $this->city->getAttributes();
113
        }
114
115
        foreach($cities as $city){
116
            array_set($city, 'label', $city->name);
117
        }
118
119
        $response['city']   = $cities;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
120
        $response['status'] = true;
121
122
        return response()->json($response);
123
    }
124
125
    /**
126
     * Display a listing of the city resource.
127
     *
128
     * @return \Illuminate\Http\Response
129
     */
130 View Code Duplication
    public function detailCity($id)
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...
131
    {
132
        $cities = $this->indonesia->findCity($id);
133
134
        array_set($cities, 'label', $cities->name);
135
136
        $response['city']   = $cities;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
137
        $response['status'] = true;
138
139
        return response()->json($response);
140
    }
141
142
    /**
143
     * Display a listing of the district resource.
144
     *
145
     * @return \Illuminate\Http\Response
146
     */
147 View Code Duplication
    public function allDistrict()
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...
148
    {
149
        $districts = $this->indonesia->allDistricts();
150
151
        foreach($districts as $district){
152
            array_set($district, 'label', $district->name);
153
        }
154
155
        $response['district']   = $districts;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
156
        $response['status']     = true;
157
158
        return response()->json($response);
159
    }
160
161
    /**
162
     * Display a listing of the district resource.
163
     *
164
     * @return \Illuminate\Http\Response
165
     */
166 View Code Duplication
    public function allDistrictByCity($id)
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...
167
    {
168
        $districts = $this->indonesia->findCity($id, ['districts']);
169
170
        if (isset($districts->districts)) {
171
            $districts = $districts->districts;
172
        } else {
173
            $districts = $this->district->getAttributes();
174
        }
175
176
        foreach($districts as $district){
177
            array_set($district, 'label', $district->name);
178
        }
179
180
        $response['district']   = $districts;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
181
        $response['status']     = true;
182
183
        return response()->json($response);
184
    }
185
186
    /**
187
     * Display a listing of the district resource.
188
     *
189
     * @return \Illuminate\Http\Response
190
     */
191 View Code Duplication
    public function detailDistrict($id)
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...
192
    {
193
        $districts = $this->indonesia->findDistrict($id);
194
195
        array_set($districts, 'label', $districts->name);
196
197
        $response['district']   = $districts;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
198
        $response['status']     = true;
199
200
        return response()->json($response);
201
    }
202
203
    /**
204
     * Display a listing of the village resource.
205
     *
206
     * @return \Illuminate\Http\Response
207
     */
208 View Code Duplication
    public function allVillage()
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...
209
    {
210
        $villages = $this->indonesia->allVillages();
211
212
        foreach($villages as $village){
213
            array_set($village, 'label', $village->name);
214
        }
215
216
        $response['village']    = $villages;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
217
        $response['status']     = true;
218
219
        return response()->json($response);
220
    }
221
222
    /**
223
     * Display a listing of the village resource.
224
     *
225
     * @return \Illuminate\Http\Response
226
     */
227 View Code Duplication
    public function allVillageByDistrict($id)
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...
228
    {
229
        $villages = $this->indonesia->findDistrict($id, ['villages']);
230
231
        if (isset($villages->villages)) {
232
            $villages = $villages->villages;
233
        } else {
234
            $villages = $this->village->getAttributes();
235
        }
236
237
        foreach($villages as $village){
238
            array_set($village, 'label', $village->name);
239
        }
240
241
        $response['village']    = $villages;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
242
        $response['status']     = true;
243
244
        return response()->json($response);
245
    }
246
247
    /**
248
     * Display a listing of the village resource.
249
     *
250
     * @return \Illuminate\Http\Response
251
     */
252 View Code Duplication
    public function detailVillage($id)
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...
253
    {
254
        $villages = $this->indonesia->findVillage($id);
255
256
        array_set($villages, 'label', $villages->name);
257
258
        $response['village']    = $villages;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
259
        $response['status']     = true;
260
261
        return response()->json($response);
262
    }
263
}
264