Completed
Pull Request — master (#34)
by Raed
05:40
created

GeoRoutesServiceProvider   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Test Coverage

Coverage 92.11%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 6
eloc 28
c 6
b 0
f 0
dl 0
loc 79
ccs 35
cts 38
cp 0.9211
rs 10

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

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

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

83
            return new GeoRoute(/** @scrutinizer ignore-type */ $this, $countries, 'allow');
Loading history...
84 364
        });
85
86 78
        Route::macro('geo', function (array $attributes, Closure $routes) {
87
            return new GeoGroup($attributes, $routes);
88 364
        });
89
90 78
        Router::macro('geo', function (array $attributes, Closure $routes) {
91 14
            return new GeoGroup($attributes, $routes);
92 364
        });
93 364
    }
94
}
95