Completed
Push — master ( 2813e7...1616b2 )
by Choraimy
14s
created

GeoRoutesServiceProvider::boot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 1
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 100
    public function boot()
19
    {
20 100
        $this->mergeConfigFrom(__DIR__ . '/../config/global.php', 'geo-routes.global');
21 100
        $this->mergeConfigFrom(__DIR__ . '/../config/routes.php', 'geo-routes.routes');
22 100
        $this->publishes([__DIR__ . '/../config' => config_path('geo-routes')], 'config');
23
24 100
        $this->registerMacros();
25 100
        $this->registerGlobalMiddleware();
26 100
    }
27
28
    /**
29
     * Register any package services.
30
     *
31
     * @return void
32
     */
33 100
    public function register()
34
    {
35 100
        $router = $this->app->make('router');
36
37 100
        if (method_exists($router, 'aliasMiddleware')) {
38 100
            $router->aliasMiddleware('geo', GeoRoutesMiddleware::class);
39
        } else {
40
            $router->middleware('geo', GeoRoutesMiddleware::class);
41
        }
42 100
    }
43
44
    /**
45
     * Register the global middleware.
46
     *
47
     * @return void
48
     */
49 100
    protected function registerGlobalMiddleware()
50
    {
51 100
        if (!$this->app['config']['geo-routes.global.enabled']) {
52
            return;
53
        }
54
55 100
        $this->app->make(Kernel::class)->pushMiddleware(GeoMiddleware::class);
56 100
    }
57
58
    /**
59
     * Register the route macros.
60
     *
61
     * @return void
62
     */
63 60
    protected function registerMacros()
64
    {
65 40
        Route::macro('allowFrom', function (string ...$countries) {
66 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

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

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

74
            return new GeoRoute(/** @scrutinizer ignore-type */ $this, $countries, 'allow');
Loading history...
75 100
        });
76 100
    }
77
}
78