Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
25.00% covered (danger)
25.00%
2 / 8
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
CheckAllowedVisibilityMaintenance
25.00% covered (danger)
25.00%
2 / 8
0.00% covered (danger)
0.00%
0 / 2
15.55
0.00% covered (danger)
0.00%
0 / 1
 handle
33.33% covered (danger)
33.33%
2 / 6
0.00% covered (danger)
0.00%
0 / 1
5.67
 isAllowed
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace App\Http\Middleware;
4
5use Closure;
6use Illuminate\Http\Request;
7use Illuminate\Support\Facades\Cookie;
8
9class CheckAllowedVisibilityMaintenance
10{
11    public function handle(Request $request, Closure $next)
12    {
13        // Check if the application is in maintenance mode
14        if (app()->isDownForMaintenance())
15        {
16            // Checks if allowed
17            if ($this->isAllowed())
18            {
19                return $next($request);
20            }
21
22            // Returns a custom response to indicate that the application is under maintenance
23            $message = 'The application is undergoing maintenance!';
24            return response()->view('maintenance', ['message' => $message], 503);
25        }
26
27        return $next($request);
28    }
29
30    private function isAllowed(): bool
31    {
32        // Check if is allowed
33        $conditionalFlag = env('APP_ROUTE_COOKIE_FLAG');
34        return ($conditionalFlag && Cookie::has($conditionalFlag));
35    }
36}