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

GeoGroup   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
eloc 21
dl 0
loc 91
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A denyFrom() 0 6 1
A __destruct() 0 10 1
A allowFrom() 0 6 1
A __construct() 0 8 1
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
    /**
38
     * Create a new GeoGroup instance
39
     *
40
     * @param array $attributes
41
     * @param callable $routes
42
     */
43 50
    public function __construct(array $attributes, callable $routes)
44
    {
45 50
        $this->attributes = $attributes;
46 50
        $this->routes = $routes;
47
48 50
        $this->constraint = new GeoConstraint(false, []);
49
50 50
        CallbacksRegistrar::init();
51 50
    }
52
53
    /**
54
     * Destruct the GeoGroup instance
55
     */
56 50
    public function __destruct()
57
    {
58 50
        $this->constraint->setCountries(...$this->countries ?? []);
59
60 50
        $this->attributes = array_merge($this->attributes, [
61 50
                                            'GeoConstraint' => $this->constraint,
62 50
                                            'middleware' => 'geo',
63
                                        ]);
64
65 50
        app('router')->group($this->attributes, $this->routes);
66 50
    }
67
68
    /**
69
     * Allow access from given countries
70
     *
71
     * @param string ...$countries
72
     *
73
     * @return $this
74
     */
75 20
    public function allowFrom(string ...$countries)
76
    {
77 20
        $this->countries = $countries;
78 20
        $this->constraint->setAccess(true);
79
80 20
        return $this;
81
    }
82
83
    /**
84
     * Deny access from given countries
85
     *
86
     * @param string ...$countries
87
     *
88
     * @return $this
89
     */
90 30
    public function denyFrom(string ...$countries)
91
    {
92 30
        $this->countries = $countries;
93 30
        $this->constraint->setAccess(false);
94
95 30
        return $this;
96
    }
97
}
98