CloseSite::handle()   C
last analyzed

Complexity

Conditions 12
Paths 16

Size

Total Lines 33
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 16
c 1
b 0
f 0
nc 16
nop 2
dl 0
loc 33
rs 6.9666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Http\Middleware;
4
5
use App\Repositories\SettingRepository;
6
use Auth;
7
use Closure;
8
use Gate;
9
use Request;
10
11
class CloseSite
12
{
13
    public function handle($request, Closure $next)
14
    {
15
        if (Request::is('js/*.js') || Request::is('api/login')) {
16
            return $next($request);
17
        }
18
19
        $set_data = new SettingRepository();
20
        $settings = $set_data->getCacheSetting();
21
22
        // сайт отключен на обновление / диагностику
23
        if (isset($settings['disable_site']) && $settings['disable_site']) {
24
            if (Gate::allows('admin-only', auth()->user())) {
25
                return $next($request);
26
            }
27
            abort(521);
28
        }
29
30
        // Доступ к сайту только для зарегистрированных пользователей
31
        if (isset($settings['registered_allow']) && $settings['registered_allow']) {
32
            if (Auth::check()) {
33
                return $next($request);
34
            }
35
            abort(403);
36
        }
37
38
        // Запрет всех роутов регистрации;
39
        if (isset($settings['enable_register']) && !$settings['enable_register']) {
40
            if (Request::is('api/register')) {
41
                abort(403);
42
            }
43
        }
44
45
        return $next($request);
46
    }
47
}
48