CheckSite   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 33
rs 10
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 31 5
1
<?php
2
3
namespace App\Http\Middleware;
4
5
use Closure;
6
use DB;
7
use Illuminate\Support\Facades\Cache;
8
9
class CheckSite
10
{
11
    public function handle($request, Closure $next)
12
    {
13
        //check for database connection every 5 minutes
14
        $time = now()->addMinutes(5);
15
        $error = null;
16
17
        //dev
18
        // cache()->forget('check');
19
20
        if (!Cache::has('check')) {
21
            Cache::remember('check', $time, function () use ($error) {
22
                try {
23
                    DB::connection()->table(DB::raw('settings'))->first([DB::raw(1)]);
24
                    if (!DB::connection()->table(DB::raw('settings'))->count()) {
25
                        $error = __('api.db_data');
26
                    }
27
                } catch (\PDOException $e) {
28
                    $error = __('api.db_connect');
29
                }
30
31
                //show error
32
                if (isset($error)) {
33
                    cache()->forget('check');
34
                    dd($error);
35
                }
36
37
                return true;
38
            });
39
        }
40
41
        return $next($request);
42
    }
43
}
44