Passed
Pull Request — master (#34)
by Raed
04:11
created

GeoRoute   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Test Coverage

Coverage 74.07%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 28
c 3
b 0
f 0
dl 0
loc 100
ccs 20
cts 27
cp 0.7407
rs 10
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __destruct() 0 3 1
A __call() 0 11 4
A applyConstraint() 0 17 3
A __construct() 0 8 1
1
<?php
2
3
namespace LaraCrafts\GeoRoutes;
4
5
use BadMethodCallException;
6
use Illuminate\Routing\Route;
7
8
/**
9
 * @mixin \Illuminate\Routing\Route
10
 */
11
class GeoRoute
12
{
13
    use Concerns\HasCallback;
14
    use Concerns\ControlsAccess;
15
16
    /**
17
     * Rule is applied.
18
     *
19
     * @var bool
20
     */
21
    protected $applied;
22
23
    /**
24
     * The countries to apply the rule for.
25
     *
26
     * @var array
27
     */
28
    protected $countries;
29
30
    /**
31
     * The route.
32
     *
33
     * @var \Illuminate\Routing\Route
34
     */
35
    protected $route;
36
37
    /**
38
     * The rule's strategy.
39
     *
40
     * @var string
41
     */
42
    protected $strategy;
43
44
    /**
45
     * Create a new GeoRoute instance.
46
     *
47
     * @param \Illuminate\Routing\Route $route
48
     * @param array $countries
49
     * @param string $strategy
50
     * @throws \InvalidArgumentException
51
     */
52 60
    public function __construct(Route $route, array $countries, string $strategy)
53
    {
54 60
        $this->applied = false;
55 60
        $this->countries = array_map('strtoupper', $countries);
56 60
        $this->route = $route;
57 60
        $this->strategy = $strategy;
58
59 60
        static::loadProxies();
60 60
    }
61
62
    /**
63
     * Dynamically call the underlying route.
64
     *
65
     * @param string $method
66
     * @param array $arguments
67
     *
68
     * @return mixed
69
     */
70
    public function __call(string $method, array $arguments)
71
    {
72
        if (method_exists($this->route, $method) || Route::hasMacro($method)) {
73
            return $this->route->$method(...$arguments);
74
        }
75
76
        if ($this->callbackExists($method)) {
77
            return $this->setCallback(static::$proxies[$method], $arguments);
78
        }
79
80
        throw new BadMethodCallException("Undefined method '$method'");
81
    }
82
83
    /**
84
     * Destruct the GeoRoute instance and apply the middleware.
85
     */
86 60
    public function __destruct()
87
    {
88 60
        $this->applyConstraint();
89 60
    }
90
91
    /**
92
     * Apply the geo-constraint to the route.
93
     */
94 60
    protected function applyConstraint()
95
    {
96 60
        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...
97
            return;
98
        }
99
100 60
        $action = $this->route->getAction();
101 60
        $action['middleware'][] = 'geo';
102 60
        $action['geo'] = [
103 60
            'strategy' => $this->strategy,
104 60
            'countries' => (array)$this->countries,
105 60
            'callback' => $this->callback,
106
        ];
107
108 60
        $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

108
        $this->route->setAction(/** @scrutinizer ignore-type */ $action);
Loading history...
109
110 60
        $this->applied = true;
111 60
    }
112
}
113