Completed
Pull Request — master (#17)
by Raed
15:29
created

GeoRoutesServiceProvider   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
eloc 16
dl 0
loc 50
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 8 2
A boot() 0 7 2
A registerMacros() 0 12 1
1
<?php
2
3
namespace LaraCrafts\GeoRoutes;
4
5
use Illuminate\Routing\Route;
6
use Illuminate\Support\ServiceProvider;
7
use LaraCrafts\GeoRoutes\Http\Middleware\GeoRoutesMiddleware;
8
9
class GeoRoutesServiceProvider extends ServiceProvider
10
{
11
    /**
12
     * Bootstrap any package services.
13
     *
14
     * @return void
15
     */
16 223
    public function boot()
17
    {
18 223
        $this->mergeConfigFrom(__DIR__ . '/../config/geo-routes.php', 'geo-routes');
19 223
        $this->publishes([__DIR__ . '/../config/geo-routes.php' => config_path('geo-routes.php')], 'config');
20
21 223
        if (version_compare($this->app->version(), '5.5.0', '>=')) {
22 90
            $this->registerMacros();
23
        }
24 223
    }
25
26
    /**
27
     * Register any package services.
28
     *
29
     * @return void
30
     */
31 223
    public function register()
32
    {
33 223
        $router = $this->app->make('router');
34
35 223
        if (method_exists($router, 'aliasMiddleware')) {
36 122
            $router->aliasMiddleware('geo', GeoRoutesMiddleware::class);
37
        } else {
38 101
            $router->middleware('geo', GeoRoutesMiddleware::class);
39
        }
40 223
    }
41
42
    /**
43
     * Register the route macros.
44
     *
45
     * @return void
46
     */
47 54
    protected function registerMacros()
48
    {
49 36
        Route::macro('allowFrom', function (string ...$countries) {
50 10
            return new GeoRoute($this, $countries, 'allow');
0 ignored issues
show
Bug introduced by
$this of type LaraCrafts\GeoRoutes\GeoRoutesServiceProvider is incompatible with the type Illuminate\Routing\Route expected by parameter $route of LaraCrafts\GeoRoutes\GeoRoute::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

50
            return new GeoRoute(/** @scrutinizer ignore-type */ $this, $countries, 'allow');
Loading history...
51 90
        });
52
53 36
        Route::macro('denyFrom', function (string ...$countries) {
54 10
            return new GeoRoute($this, $countries, 'deny');
0 ignored issues
show
Bug introduced by
$this of type LaraCrafts\GeoRoutes\GeoRoutesServiceProvider is incompatible with the type Illuminate\Routing\Route expected by parameter $route of LaraCrafts\GeoRoutes\GeoRoute::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

54
            return new GeoRoute(/** @scrutinizer ignore-type */ $this, $countries, 'deny');
Loading history...
55 90
        });
56
57 36
        Route::macro('from', function (string ...$countries) {
58 10
            return new GeoRoute($this, $countries, 'allow');
0 ignored issues
show
Bug introduced by
$this of type LaraCrafts\GeoRoutes\GeoRoutesServiceProvider is incompatible with the type Illuminate\Routing\Route expected by parameter $route of LaraCrafts\GeoRoutes\GeoRoute::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

58
            return new GeoRoute(/** @scrutinizer ignore-type */ $this, $countries, 'allow');
Loading history...
59 90
        });
60 90
    }
61
}
62