CheckForEarlyAccessMode   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 2
dl 0
loc 81
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 18 5
A inExceptArray() 0 26 5
1
<?php
2
3
namespace Neo\EarlyAccess\Http\Middleware;
4
5
use Closure;
6
use Illuminate\Http\Request;
7
use Neo\EarlyAccess\Facades\EarlyAccess;
8
use Symfony\Component\HttpFoundation\IpUtils;
9
10
class CheckForEarlyAccessMode
11
{
12
    /**
13
     * The URIs that should be accessible while maintenance mode is enabled.
14
     *
15
     * @var array
16
     */
17
    protected $except = [];
18
19
    /**
20
     * @var string
21
     */
22
    private $baseUrl;
23
24
    /**
25
     * CheckForEarlyAccessMode constructor.
26
     */
27
    public function __construct()
28
    {
29
        $this->baseUrl = config('early-access.url');
30
    }
31
32
    /**
33
     * Handle the incoming request.
34
     *
35
     * @param  \Illuminate\Http\Request $request
36
     * @param  \Closure $next
37
     * @return mixed
38
     */
39
    public function handle(Request $request, Closure $next)
40
    {
41
        if (EarlyAccess::isEnabled()) {
42
            $data = EarlyAccess::getBeaconDetails();
43
44
            if (isset($data['allowed']) && IpUtils::checkIp($request->ip(), (array) $data['allowed'])) {
45
                return $next($request);
46
            }
47
48
            if ($this->inExceptArray($request)) {
49
                return $next($request);
50
            }
51
52
            return redirect(config('early-access.url'));
53
        }
54
55
        return $next($request);
56
    }
57
58
    /**
59
     * Determine if the request has a URI that should be accessible in maintenance mode.
60
     *
61
     * @param  \Illuminate\Http\Request $request
62
     * @return bool
63
     */
64
    protected function inExceptArray($request)
65
    {
66
        $defaultExceptions = [
67
            $this->baseUrl,
68
            $this->baseUrl . '/*',
69
            config('early-access.login_url'),
70
        ];
71
72
        $defaultExceptions = array_filter($defaultExceptions, function ($item) {
73
            return trim($item, '/') !== '*';
74
        });
75
76
        array_push($this->except, ...$defaultExceptions);
77
78
        foreach (array_unique($this->except) as $except) {
79
            if ($except !== '/') {
80
                $except = trim($except, '/');
81
            }
82
83
            if ($request->fullUrlIs($except) || $request->is($except)) {
84
                return true;
85
            }
86
        }
87
88
        return false;
89
    }
90
}
91