Completed
Push — master ( b55014...d2ffcb )
by Robin
03:16
created

CrawlLogController::clearAllLogs()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 4
nop 0
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Famdirksen\LaravelJobHandler\Http\Controllers;
4
5
use Carbon\Carbon;
6
use Famdirksen\LaravelJobHandler\Exceptions\ClearAllLogsException;
7
use Famdirksen\LaravelJobHandler\Models\CrawlerStatus;
8
use Famdirksen\LaravelJobHandler\Models\CrawlerStatusLogs;
9
10
class CrawlLogController
11
{
12
    public function clearAllLogs() {
13
        $enabled = false;
14
        $config = config('laravel-job-handler.clear-log-after-seconds', null);
15
16
        if(!is_null($config)) {
17
            $enabled = true;
18
        }
19
20
        if($enabled) {
21
            $this->clearAllLogsBefore(Carbon::now()->subSeconds($config));
22
        } else {
23
            //deleting of old logs is disabled
24
        }
25
    }
26
    protected function clearAllLogsBefore(Carbon $carbon) {
27
        $date_string = $carbon->copy()->format('Y-m-d H:i:s');
28
29
        CrawlerStatus::where('created_at', '<=', $date_string)
30
            ->orWhereNull('created_at')
31
            ->delete();
32
        CrawlerStatusLogs::where('created_at', '<=', $date_string)
33
            ->orWhereNull('created_at')
34
            ->delete();
35
    }
36
}
37