GeoRoutesServiceProvider::boot()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1.0019

Importance

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

78
            return new GeoRoute(/** @scrutinizer ignore-type */ $this, $countries, 'allow');
Loading history...
79 480
        });
80
81 120
        Route::macro('denyFrom', function (string ...$countries) {
82 16
            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

82
            return new GeoRoute(/** @scrutinizer ignore-type */ $this, $countries, 'deny');
Loading history...
83 480
        });
84
85 120
        Route::macro('from', function (string ...$countries) {
86 16
            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

86
            return new GeoRoute(/** @scrutinizer ignore-type */ $this, $countries, 'allow');
Loading history...
87 480
        });
88
89 120
        Route::macro('geo', function (array $attributes, Closure $routes) {
90
            return new GeoGroup($attributes, $routes);
91 480
        });
92
93 120
        Router::macro('geo', function (array $attributes, Closure $routes) {
94 16
            return new GeoGroup($attributes, $routes);
95 480
        });
96 480
    }
97
}
98