Completed
Push — master ( 1bd981...d065a3 )
by Choraimy
15s
created

DeterminesGeoAccess   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Test Coverage

Coverage 85.71%

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 23
ccs 6
cts 7
cp 0.8571
rs 10
c 0
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A shouldHaveAccess() 0 13 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
     * Determine if the request should be allowed through.
12
     *
13
     * @param \Illuminate\Http\Request $request
14
     * @param array $countries
15
     * @param string $strategy
16
     * @return bool
17
     */
18 15
    protected function shouldHaveAccess(Request $request, array $countries, string $strategy)
19
    {
20 15
        if (!$countries) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $countries of type array 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...
21
            return $strategy !== 'allow';
22
        }
23
24 15
        $requestCountry = Location::get($request->ip())->countryCode;
25
26 15
        if ($strategy === 'allow') {
27 6
            return in_array($requestCountry, $countries);
28
        }
29
30 9
        return !in_array($requestCountry, $countries);
31
    }
32
}
33