ServiceProvider::boot()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Alkhwlani\XssMiddleware;
4
5
use Illuminate\Contracts\Http\Kernel;
6
use Illuminate\Support\Arr;
7
use Illuminate\Support\ServiceProvider as ServiceProviderAlias;
8
9
class ServiceProvider extends ServiceProviderAlias
10
{
11
    /**
12
     * Bootstrap any application services.
13
     *
14
     * @return void
15
     */
16 3
    public function boot()
17
    {
18 3
        $this->registerMiddleware();
19 3
    }
20
21
    /**
22
     * auto append middleware to router.
23
     */
24 3
    protected function registerMiddleware()
25
    {
26 3
        $middlewareClass = $this->app['config']->get('xss-middleware.middleware', XSSFilterMiddleware::class);
27
28 3
        $this->app['router']->aliasMiddleware('xss-filter', $middlewareClass);
29
30 3
        $registerType = $this->app['config']->get('xss-middleware.auto_register_middleware', false);
31
32 3
        if ($registerType === false) {
33 2
            return;
34
        }
35
36 1
        if ($registerType === true) { // Register middleware as global Middleware
37 1
            $this->app->make(Kernel::class)->pushMiddleware($middlewareClass);
38
39 1
            return;
40
        }
41
42
        // Register Middleware for route group
43
        foreach (Arr::wrap($registerType) as $group) {
44
            $this->app['router']->pushMiddlewareToGroup($group, $middlewareClass);
45
        }
46
    }
47
48
    /**
49
     * Register any application services.
50
     *
51
     * @return void
52
     */
53 3
    public function register()
54
    {
55 3
        $this->registerConfig();
56 3
    }
57
58 3
    public function registerConfig()
59
    {
60 3
        if ($this->app->runningInConsole()) {
61 3
            $this->publishes([
62 3
                __DIR__.'/../config/xss-middleware.php' => config_path('xss-middleware.php'),
63 3
            ], 'config');
64
        }
65
66 3
        $this->mergeConfigFrom(__DIR__.'/../config/xss-middleware.php', 'xss-middleware');
67 3
    }
68
}
69