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

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

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

76
            return new GeoRoute(/** @scrutinizer ignore-type */ $this, $countries, 'allow');
Loading history...
77 204
        });
78
79 51
        Route::macro('geo', function (array $attributes, Closure $routes) {
80
            return new GeoGroup($attributes, $routes);
81 204
        });
82
83 51
        Router::macro('geo', function (array $attributes, Closure $routes) {
84 12
            return new GeoGroup($attributes, $routes);
85 204
        });
86 204
    }
87
}
88