Completed
Push — master ( 984bc1...bcdfa3 )
by Raed
12s queued 10s
created

GeoRoutesServiceProvider   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 93.75%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 6
eloc 24
dl 0
loc 71
ccs 30
cts 32
cp 0.9375
rs 10
c 5
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 8 1
A registerGlobalMiddleware() 0 7 2
A register() 0 15 2
A registerMacros() 0 12 1
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 280
    public function boot()
19
    {
20 280
        $this->mergeConfigFrom(__DIR__ . '/../config/global.php', 'geo-routes.global');
21 280
        $this->mergeConfigFrom(__DIR__ . '/../config/routes.php', 'geo-routes.routes');
22 280
        $this->publishes([__DIR__ . '/../config' => config_path('geo-routes')], 'config');
23
24 280
        $this->registerMacros();
25 280
        $this->registerGlobalMiddleware();
26 280
    }
27
28
    /**
29
     * Register any package services.
30
     *
31
     * @return void
32
     */
33 280
    public function register()
34
    {
35 280
        $router = $this->app->make('router');
36
37 100
        $this->app->singleton('georoutes.callbacks', function () {
38 14
            $registrar = new CallbackRegistrar();
39 14
            $registrar->loadCallbacks(config('geo-routes.routes.callbacks'));
40
41 14
            return $registrar;
42 280
        });
43
44 280
        if (method_exists($router, 'aliasMiddleware')) {
45 280
            $router->aliasMiddleware('geo', GeoRoutesMiddleware::class);
46
        } else {
47
            $router->middleware('geo', GeoRoutesMiddleware::class);
48
        }
49 280
    }
50
51
    /**
52
     * Register the global middleware.
53
     *
54
     * @return void
55
     */
56 280
    protected function registerGlobalMiddleware()
57
    {
58 280
        if (!$this->app['config']['geo-routes.global.enabled']) {
59
            return;
60
        }
61
62 280
        $this->app->make(Kernel::class)->pushMiddleware(GeoMiddleware::class);
63 280
    }
64
65
    /**
66
     * Register the route macros.
67
     *
68
     * @return void
69
     */
70 180
    protected function registerMacros()
71
    {
72 100
        Route::macro('allowFrom', function (string ...$countries) {
73 14
            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

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

77
            return new GeoRoute(/** @scrutinizer ignore-type */ $this, $countries, 'deny');
Loading history...
78 280
        });
79
80 100
        Route::macro('from', function (string ...$countries) {
81 14
            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

81
            return new GeoRoute(/** @scrutinizer ignore-type */ $this, $countries, 'allow');
Loading history...
82 280
        });
83 280
    }
84
}
85