| Total Complexity | 4 |
| Total Lines | 85 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 0 | ||
| 1 | <?php |
||
| 5 | class GeoGroup |
||
| 6 | { |
||
| 7 | use CallbacksHandler; |
||
|
1 ignored issue
–
show
|
|||
| 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) |
|
| 90 | } |
||
| 91 | } |
||
| 92 |