CrawlLogController::clearAllLogs()   A
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 9
c 1
b 0
f 0
nc 8
nop 1
dl 0
loc 17
rs 9.6111
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($is_job = false)
13
    {
14
        $enabled = false;
15
        $config = config('laravel-job-handler.clear_log_after_seconds', null);
16
        $as_job = config('laravel-job-handler.clear_log_via_job', false);
17
18
        if ($as_job && !$is_job) {
19
            //dispatch(new )
20
        }
21
22
        if (!is_null($config)) {
23
            $enabled = true;
24
        }
25
26
        if ($enabled) {
27
            $this->clearAllLogsBefore(Carbon::now()->subSeconds($config));
28
        } else {
29
            //deleting of old logs is disabled
30
        }
31
    }
32
    protected function clearAllLogsBefore(Carbon $carbon)
33
    {
34
        $date_string = $carbon->copy()->format('Y-m-d H:i:s');
35
36
        CrawlerStatus::where('created_at', '<=', $date_string)
37
            ->orWhereNull('created_at')
38
            ->delete();
39
        CrawlerStatusLogs::where('created_at', '<=', $date_string)
40
            ->orWhereNull('created_at')
41
            ->delete();
42
    }
43
}
44