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

GeoGroup::allowFrom()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
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 1
dl 0
loc 6
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 GeoGroup
6
{
7
    use CallbacksHandler;
1 ignored issue
show
Bug introduced by
The trait LaraCrafts\GeoRoutes\CallbacksHandler requires the property $route which is not provided by LaraCrafts\GeoRoutes\GeoGroup.
Loading history...
8
9
    /**
10
     * The route group attributes
11
     *
12
     * @var array
13
     */
14
    protected $attributes;
15
16
    /**
17
     * The raw routes
18
     *
19
     * @var callable
20
     */
21
    protected $routes;
22
23
    /**
24
     * The geo-constraint
25
     *
26
     * @var GeoConstraint
27
     */
28
    protected $constraint;
29
30
    /**
31
     * The countries covered by the constraint
32
     *
33
     * @var array
34
     */
35
    protected $countries;
36
37 50
    public function __construct(array $attributes, callable $routes)
38
    {
39 50
        $this->attributes = $attributes;
40 50
        $this->routes = $routes;
41
42 50
        $this->constraint = new GeoConstraint(false, []);
43
44 50
        CallbacksRegistrar::init();
45 50
    }
46
47
    /**
48
     * Destruct the GeoGroup instance
49
     */
50 50
    public function __destruct()
51
    {
52 50
        $this->constraint->setCountries(...$this->countries ?? []);
53
54 50
        $this->attributes = array_merge($this->attributes, [
55 50
                                            'GeoConstraint' => $this->constraint,
56 50
                                            'middleware' => 'geo',
57
                                        ]);
58
59 50
        app('router')->group($this->attributes, $this->routes);
60 50
    }
61
62
    /**
63
     * Allow access from given countries
64
     *
65
     * @param string ...$countries
66
     *
67
     * @return $this
68
     */
69 20
    public function allowFrom(string ...$countries)
70
    {
71 20
        $this->countries = $countries;
72 20
        $this->constraint->setAccess(true);
73
74 20
        return $this;
75
    }
76
77
    /**
78
     * Deny access from given countries
79
     *
80
     * @param string ...$countries
81
     *
82
     * @return $this
83
     */
84 30
    public function denyFrom(string ...$countries)
85
    {
86 30
        $this->countries = $countries;
87 30
        $this->constraint->setAccess(false);
88
89 30
        return $this;
90
    }
91
}
92