Passed
Pull Request — master (#24)
by Raed
04:43
created

GeoConstraint::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace LaraCrafts\GeoRoutes;
4
5
class GeoConstraint
6
{
7
    /**
8
     * Determines if the constraint is allowing
9
     * requests from the specified countries
10
     *
11
     * @var boolean
12
     */
13
    protected $allowed;
14
15
    /**
16
     * The countries to apply the constraint on
17
     *
18
     * @var array
19
     */
20
    protected $countries;
21
22
    /**
23
     * The callback to execute if the country
24
     * is not allowed
25
     *
26
     * @var callable
27
     */
28
    protected $callback;
29
30
    /**
31
     * Determine if the constraint is
32
     * bound to a callback
33
     *
34
     * @var null|string
35
     */
36
    protected $binding = null;
37
38
    /**
39
     * Create a new GeoConstraint instance
40
     *
41
     * @param boolean $allowed
42
     * @param array $countries
43
     * @param null|\LaraCrafts\GeoRoutes\GeoCallback $callback
44
     *
45
     * @return void
46
     */
47 120
    public function __construct(bool $allowed, array $countries, GeoCallback $callback = null)
48
    {
49 120
        $this->allowed = $allowed;
50 120
        $this->countries = $countries;
51 120
        $this->callback = $callback;
52 120
    }
53
54
    /**
55
     * Get the constraint's callback
56
     *
57
     * @return \LaraCrafts\GeoRoutes\GeoCallback
58
     */
59 110
    public function getCallback()
60
    {
61 110
        if (!is_null($this->binding)) {
62 40
            return CallbacksRegistrar::resolve($this->binding);
63
        }
64
65 70
        return $this->callback;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->callback returns the type callable which is incompatible with the documented return type LaraCrafts\GeoRoutes\GeoCallback.
Loading history...
66
    }
67
68
    /**
69
     * Set the contraint's callback
70
     *
71
     * @param \LaraCrafts\GeoRoutes\GeoCallback $callback
72
     *
73
     * @return \LaraCrafts\GeoRoutes\GeoCallback
74
     */
75 30
    public function setCallback(GeoCallback $callback)
76
    {
77 30
        $this->callback = $callback;
78
79 30
        return $this->callback;
80
    }
81
82
    /**
83
     * Bind the constraint with a callback
84
     *
85
     * @param string $callback
86
     * @param array|null $arguments
87
     *
88
     * @return void
89
     */
90 40
    public function bind(string $callback, array $arguments = null)
91
    {
92 40
        $this->binding = CallbacksRegistrar::bind($callback, $arguments);
93 40
    }
94
95
    /**
96
     * Get the countries covered by
97
     * the constraint
98
     *
99
     * @return array
100
     */
101 110
    public function getCountries()
102
    {
103 110
        return $this->countries;
104
    }
105
106
    /**
107
     * Get the countries covered by
108
     * the constraint
109
     *
110
     * @param string ...$countries
111
     *
112
     * @return $this
113
     */
114 50
    public function setCountries(string ...$countries)
115
    {
116 50
        $this->countries = $countries;
117
118 50
        return $this;
119
    }
120
121
    /**
122
     * Add a country to the list of countries
123
     * covered by the constraint
124
     *
125
     * @param string $country
126
     *
127
     * @return $this
128
     */
129
    public function addCountry(string $country)
130
    {
131
        array_push($this->countries, $country);
132
133
        return $this;
134
    }
135
136
    /**
137
     * Set the access status
138
     *
139
     * @param boolean $allowed
140
     *
141
     * @return $this
142
     */
143 120
    public function setAccess(bool $allowed)
144
    {
145 120
        $this->allowed = $allowed;
146
147 120
        return $this;
148
    }
149
150
    /**
151
     * Determine if the constraint allows
152
     * access
153
     *
154
     * @return boolean
155
     */
156 110
    public function isAllowed()
157
    {
158 110
        return $this->allowed;
159
    }
160
}
161