LogClear::handle()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
nc 6
nop 1
dl 0
loc 26
ccs 0
cts 15
cp 0
crap 42
rs 8.8817
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Longman\LaravelLodash\Commands;
6
7
use Illuminate\Console\Command;
8
use Illuminate\Console\ConfirmableTrait;
9
use Illuminate\Filesystem\Filesystem;
10
11
class LogClear extends Command
12
{
13
    use ConfirmableTrait;
14
15
    /**
16
     * The name and signature of the console command.
17
     *
18
     * @var string
19
     */
20
    protected $signature = 'log:clear {--force : Force the operation to run when in production.}';
21
22
    /**
23
     * The console command description.
24
     *
25
     * @var string
26
     */
27
    protected $description = 'Clear log files';
28
29
    /**
30
     * Execute the console command.
31
     *
32
     * @param \Illuminate\Filesystem\Filesystem $filesystem
33
     * @return mixed
34
     */
35
    public function handle(Filesystem $filesystem): void
36
    {
37
        if (! $this->confirmToProceed('Application In Production! Will be deleted all log files from storage/log folder!')) {
38
            return;
39
        }
40
41
        $logFiles = $filesystem->allFiles(storage_path('logs'));
42
        if (empty($logFiles)) {
43
            $this->comment('Log files does not found in path ' . storage_path('logs'));
44
45
            return;
46
        }
47
48
        foreach ($logFiles as $file) {
49
            if ($file->getExtension() !== 'log') {
50
                continue;
51
            }
52
53
            $status = $filesystem->delete($file->getRealPath());
54
            if (! $status) {
55
                continue;
56
            }
57
58
            $this->info('Successfully deleted: ' . $file);
59
        }
60
    }
61
}
62