Skip to content

Commit 2ac7bd9

Browse files
committed
code refactor
1 parent 96009e0 commit 2ac7bd9

File tree

1 file changed

+20
-34
lines changed

1 file changed

+20
-34
lines changed

src/Middleware/Geo.php

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,71 +9,57 @@ class Geo extends Middleware
99
{
1010
public function check($patterns)
1111
{
12-
$status = false;
12+
$places = ['continents', 'regions', 'countries', 'cities'];
1313

14-
if ($this->isEmpty()) {
15-
return $status;
14+
if ($this->isEmpty($places)) {
15+
return false;
1616
}
1717

1818
if (!$location = $this->getLocation()) {
19-
return $status;
20-
}
21-
22-
if ($this->isFiltered($location, 'continents')) {
23-
$status = true;
24-
}
25-
26-
if (!$status && $this->isFiltered($location, 'regions')) {
27-
$status = true;
19+
return false;
2820
}
2921

30-
if (!$status && $this->isFiltered($location, 'countries')) {
31-
$status = true;
32-
}
22+
foreach ($places as $place) {
23+
if (!$this->isFiltered($location, $place)) {
24+
continue;
25+
}
3326

34-
if (!$status && $this->isFiltered($location, 'cities')) {
35-
$status = true;
27+
return true;
3628
}
3729

38-
return $status;
30+
return false;
3931
}
4032

41-
protected function isEmpty()
33+
protected function isEmpty($places)
4234
{
43-
$status = true;
44-
45-
$types = ['continents', 'regions', 'countries', 'cities'];
46-
47-
foreach ($types as $type) {
48-
if (!$list = config('firewall.middleware.' . $this->middleware . '.' . $type)) {
35+
foreach ($places as $place) {
36+
if (!$list = config('firewall.middleware.' . $this->middleware . '.' . $place)) {
4937
continue;
5038
}
5139

5240
if (empty($list['allow']) && empty($list['block'])) {
5341
continue;
5442
}
5543

56-
$status = false;
57-
58-
break;
44+
return false;
5945
}
6046

61-
return $status;
47+
return true;
6248
}
6349

64-
protected function isFiltered($location, $type)
50+
protected function isFiltered($location, $place)
6551
{
66-
if (!$list = config('firewall.middleware.' . $this->middleware . '.' . $type)) {
52+
if (!$list = config('firewall.middleware.' . $this->middleware . '.' . $place)) {
6753
return false;
6854
}
6955

70-
$s_type = Str::singular($type);
56+
$s_place = Str::singular($place);
7157

72-
if (!empty($list['allow']) && !in_array((string) $location->$s_type, (array) $list['allow'])) {
58+
if (!empty($list['allow']) && !in_array((string) $location->$s_place, (array) $list['allow'])) {
7359
return true;
7460
}
7561

76-
if (in_array((string) $location->$s_type, (array) $list['block'])) {
62+
if (in_array((string) $location->$s_place, (array) $list['block'])) {
7763
return true;
7864
}
7965

0 commit comments

Comments
 (0)