Passed
Pull Request — master (#24)
by Raed
04:43
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 countries to apply the rule for.
23
     *
24
     * @var array
25
     */
26
    protected $countries;
27
28
    /**
29
     * The route.
30
     *
31
     * @var \Illuminate\Routing\Route
32
     */
33
    protected $route;
34
35
    /**
36
     * Determine if the requests are
37
     * allowed from the given countries
38
     *
39
     * @var boolean
40
     */
41
    protected $allowed;
42
43
    /**
44
     * The geo-constraint
45
     *
46
     * @var \LaraCrafts\GeoRoutes\GeoConstraint
47
     */
48
    protected $constraint;
49
50
    /**
51
     * Create a new GeoRoute instance.
52
     *
53
     * @param \Illuminate\Routing\Route $route
54
     * @param array $countries
55
     * @param boolean $allowed
56
     */
57 80
    public function __construct(Route $route, array $countries, bool $allowed)
58
    {
59 80
        $this->applied = false;
60 80
        $this->countries = $countries;
61 80
        $this->route = $route;
62 80
        $this->constraint = new GeoConstraint($allowed, $countries);
63 80
        $this->constraint->setAccess($allowed);
64
65 80
        CallbacksRegistrar::init();
66 80
    }
67
68
    /**
69
     * Destruct the GeoRoute instance and apply the middleware.
70
     */
71 80
    public function __destruct()
72
    {
73 80
        $this->applyConstraint();
74 80
    }
75
76
    /**
77
     * Apply the GeoConstraint
78
     *
79
     * @return void
80
     */
81 80
    protected function applyConstraint()
82
    {
83 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...
84
            return;
85
        }
86
87 80
        $action = $this->route->getAction();
88 80
        $action['middleware'][] = 'geo';
89
90 80
        $this->applied = true;
91 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

91
        $this->route->setAction(/** @scrutinizer ignore-type */ $action);
Loading history...
92 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...
93 80
    }
94
}
95