Completed
Pull Request — master (#19)
by Choraimy
17:14 queued 15:17
created

DeterminesGeoAccess   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 38
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A shouldHaveAccess() 0 16 3
1
<?php
2
3
namespace LaraCrafts\GeoRoutes;
4
5
use Illuminate\Http\Request;
6
use Stevebauman\Location\Facades\Location;
7
8
trait DeterminesGeoAccess
9
{
10
    /**
11
     * Get the countries.
12
     *
13
     * @return string[]
14
     */
15
    abstract public function getCountries();
16
17
    /**
18
     * Get the strategy.
19
     *
20
     * @return string
21
     */
22
    abstract public function getStrategy();
23
24
    /**
25
     * Determine if the request should be allowed through.
26
     *
27
     * @param \Illuminate\Http\Request $request
28
     * @return bool
29
     */
30 21
    public function shouldHaveAccess(Request $request)
31
    {
32 21
        $countries = $this->getCountries();
33 21
        $strategy = $this->getStrategy();
34
35 21
        if (!$countries) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $countries of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
36 6
            return $strategy !== 'allow';
37
        }
38
39 21
        $requestCountry = Location::get($request->ip())->countryCode;
40
41 21
        if ($strategy === 'allow') {
42 12
            return in_array($requestCountry, $countries);
43
        }
44
45 9
        return !in_array($requestCountry, $countries);
46
    }
47
}
48