Completed
Push — master ( 1bd981...d065a3 )
by Choraimy
15s
created

GeoRoutesServiceProvider::boot()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 11
ccs 6
cts 7
cp 0.8571
crap 2.0116
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace LaraCrafts\GeoRoutes;
4
5
use Illuminate\Contracts\Http\Kernel;
6
use Illuminate\Routing\Route;
7
use Illuminate\Support\ServiceProvider;
8
use LaraCrafts\GeoRoutes\Http\Middleware\GeoMiddleware;
9
use LaraCrafts\GeoRoutes\Http\Middleware\GeoRoutesMiddleware;
10
11
class GeoRoutesServiceProvider extends ServiceProvider
12
{
13
    /**
14
     * Bootstrap any package services.
15
     *
16
     * @return void
17
     */
18 21
    public function boot()
19
    {
20 21
        $this->mergeConfigFrom(__DIR__ . '/../config/global.php', 'geo-routes.global');
21 21
        $this->mergeConfigFrom(__DIR__ . '/../config/routes.php', 'geo-routes.routes');
22 21
        $this->publishes([__DIR__ . '/../config' => config_path('geo-routes')], 'config');
23
24 21
        if (version_compare($this->app->version(), '5.5.0', '>=')) {
25
            $this->registerMacros();
26
        }
27
28 21
        $this->registerGlobalMiddleware();
29 21
    }
30
31
    /**
32
     * Register any package services.
33
     *
34
     * @return void
35
     */
36 21
    public function register()
37
    {
38 21
        $router = $this->app->make('router');
39
40 21
        if (method_exists($router, 'aliasMiddleware')) {
41
            $router->aliasMiddleware('geo', GeoRoutesMiddleware::class);
42
        } else {
43 21
            $router->middleware('geo', GeoRoutesMiddleware::class);
44
        }
45 21
    }
46
47
    /**
48
     * Register the global middleware.
49
     *
50
     * @return void
51
     */
52 21
    protected function registerGlobalMiddleware()
53
    {
54 21
        if (!$this->app['config']['geo-routes.global.enabled']) {
55
            return;
56
        }
57
58 21
        $this->app->make(Kernel::class)->pushMiddleware(GeoMiddleware::class);
59 21
    }
60
61
    /**
62
     * Register the route macros.
63
     *
64
     * @return void
65
     */
66
    protected function registerMacros()
67
    {
68
        Route::macro('allowFrom', function (string ...$countries) {
69
            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

69
            return new GeoRoute(/** @scrutinizer ignore-type */ $this, $countries, 'allow');
Loading history...
70
        });
71
72
        Route::macro('denyFrom', function (string ...$countries) {
73
            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

73
            return new GeoRoute(/** @scrutinizer ignore-type */ $this, $countries, 'deny');
Loading history...
74
        });
75
76
        Route::macro('from', function (string ...$countries) {
77
            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

77
            return new GeoRoute(/** @scrutinizer ignore-type */ $this, $countries, 'allow');
Loading history...
78
        });
79
    }
80
}
81