Passed
Pull Request — master (#24)
by Raed
02:27
created

GeoRoute::orUnauthorized()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace LaraCrafts\GeoRoutes;
4
5
use Illuminate\Routing\Route;
6
7
/**
8
 * @mixin \Illuminate\Routing\Route
9
 */
10
class GeoRoute
11
{
12
    use CallbacksHandler;
13
14
    /**
15
     * Rule is applied.
16
     *
17
     * @var bool
18
     */
19
    protected $applied;
20
21
    /**
22
     * The callback to execute if the visitor
23
     * is not allowed.
24
     *
25
     * @var array
26
     */
27
    protected $callback;
28
29
    /**
30
     * The countries to apply the rule for.
31
     *
32
     * @var array
33
     */
34
    protected $countries;
35
36
    /**
37
     * The callbacks' proxies.
38
     *
39
     * @var array
40
     */
41
    protected static $proxies;
42
43
    /**
44
     * The route.
45
     *
46
     * @var \Illuminate\Routing\Route
47
     */
48
    protected $route;
49
50
    /**
51
     * Determine if the requests are
52
     * allowed from the given countries
53
     *
54
     * @var boolean
55
     */
56
    protected $allowed;
57
58
    /**
59
     * The geo-constraint
60
     *
61
     * @var GeoConstraint
62
     */
63
    protected $constraint;
64
65
    /**
66
     * Create a new GeoRoute instance.
67
     *
68
     * @param \Illuminate\Routing\Route $route
69
     * @param array $countries
70
     * @param string $allowed
71
     * @throws \InvalidArgumentException
72
     */
73 80
    public function __construct(Route $route, array $countries, bool $allowed)
74
    {
75 80
        $this->applied = false;
76 80
        $this->countries = $countries;
77 80
        $this->route = $route;
78 80
        $this->constraint = new GeoConstraint($allowed, $countries);
79 80
        $this->constraint->setAccess($allowed);
80
81 80
        CallbacksRegistrar::init();
82 80
    }
83
84
    /**
85
     * Destruct the GeoRoute instance and apply the middleware.
86
     */
87 80
    public function __destruct()
88
    {
89 80
        $this->applyConstraint();
90 80
    }
91
92
    /**
93
     * Apply the GeoConstraint
94
     *
95
     * @return void
96
     */
97 80
    protected function applyConstraint()
98
    {
99 80
        if ($this->applied || !$this->countries) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->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...
100
            return;
101
        }
102
103 80
        $action = $this->route->getAction();
104 80
        $action['middleware'][] = 'geo';
105
106 80
        $this->applied = true;
107 80
        $this->route->setAction($action);
0 ignored issues
show
Bug introduced by
It seems like $action can also be of type null; however, parameter $action of Illuminate\Routing\Route::setAction() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

107
        $this->route->setAction(/** @scrutinizer ignore-type */ $action);
Loading history...
108 80
        $this->route->GeoConstraint = $this->constraint;
0 ignored issues
show
Bug introduced by
The property GeoConstraint does not seem to exist on Illuminate\Routing\Route.
Loading history...
109 80
    }
110
}
111