Completed
Push — master ( 4a2452...7d97fb )
by Mike
09:50
created

Service::middlewareGroupExists()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 16
rs 9.9666
c 0
b 0
f 0
cc 4
nc 1
nop 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A Service::shouldRegisterRouteMiddleware() 0 3 1
1
<?php namespace GeneaLabs\LaravelCaffeine\Providers;
2
3
use GeneaLabs\LaravelCaffeine\Console\Commands\Publish;
4
use GeneaLabs\LaravelCaffeine\Http\Middleware\LaravelCaffeineDripMiddleware;
5
use Illuminate\Contracts\Http\Kernel;
6
use Illuminate\Routing\Route;
7
use Illuminate\Support\ServiceProvider;
8
9
class Service extends ServiceProvider
10
{
11
    public function boot()
12
    {
13
        app('router')->group(app("router")->hasMiddlewareGroup('web')
14
            ? ['middleware' => 'web']
15
            : [], function () {
16
                require __DIR__ . '/../../routes/web.php';
17
18
                if (config("app.env") === 'internaltesting') {
19
                    require __DIR__ . '/../../tests/routes/web.php';
20
                }
21
            });
22
23
        $configPath = __DIR__ . '/../../config/genealabs-laravel-caffeine.php';
24
        $this->mergeConfigFrom($configPath, 'genealabs-laravel-caffeine');
25
        $this->loadViewsFrom(
26
            __DIR__ . '/../../resources/views',
27
            'genealabs-laravel-caffeine'
28
        );
29
30
        if (config("app.env") === 'internaltesting') {
31
            $this->loadViewsFrom(
32
                __DIR__ . '/../../tests/resources/views',
33
                'genealabs-laravel-caffeine'
34
            );
35
        }
36
37
        $this->publishes([
38
            $configPath => config_path('genealabs-laravel-caffeine.php')
39
        ], 'config');
40
41
        $this->commands(Publish::class);
42
        $this->mergeConfigFrom(__DIR__ . '/../../config/genealabs-laravel-caffeine.php', 'genealabs-laravel-caffeine');
43
44
        if ($this->shouldRegisterGlobalMiddleware()) {
45
            app(Kernel::class)->pushMiddleware('\\' . LaravelCaffeineDripMiddleware::class);
46
        }
47
48
        if ($this->shouldRegisterRouteMiddleware()) {
49
            app('router')->aliasMiddleware(
50
                'caffeinated',
51
                '\\' . LaravelCaffeineDripMiddleware::class
52
            );
53
        }
54
    }
55
56
    protected function shouldRegisterGlobalMiddleware() : bool
57
    {
58
        return (! request()->ajax()
59
            && ! $this->shouldRegisterRouteMiddleware()
60
            && (php_sapi_name() === 'fpm-fcgi'
61
                || php_sapi_name() === 'cgi-fcgi'
62
                || php_sapi_name() === 'apache2handler'
63
                || config("app.env") === 'internaltesting'));
64
    }
65
66
    protected function shouldRegisterRouteMiddleware() : bool
67
    {
68
        return (bool) config('genealabs-laravel-caffeine.use-route-middleware');
69
    }
70
}
71