Passed
Pull Request — master (#18)
by Choraimy
07:22
created

GeoRoutesServiceProvider   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 43.48%

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 64
ccs 10
cts 23
cp 0.4348
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

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