Completed
Push — dev5 ( b58b46...cf4e4a )
by Ron
09:08
created

CleanLogs::handle()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 11
nc 3
nop 0
dl 0
loc 15
rs 9.9
c 1
b 0
f 0
1
<?php
2
3
namespace App\Console\Commands;
4
5
use Carbon\Carbon;
6
use Illuminate\Console\Command;
7
8
class CleanLogs extends Command
9
{
10
    /**
11
     * The name and signature of the console command.
12
     *
13
     * @var string
14
     */
15
    protected $signature = 'log:clean';
16
17
    /**
18
     * The console command description.
19
     *
20
     * @var string
21
     */
22
    protected $description = 'This command will empty the current active log';
23
24
    /**
25
     * Create a new command instance.
26
     *
27
     * @return void
28
     */
29
    public function __construct()
30
    {
31
        parent::__construct();
32
    }
33
34
    /**
35
     * Execute the console command.
36
     *
37
     * @return mixed
38
     */
39
    public function handle()
40
    {
41
        $channel = config('logging.default');
42
        $this->line('');
43
44
        //  Determine if the daily or single log channel is being used
45
        switch ($channel) {
46
            case 'daily':
47
                $this->dailyLog();
48
                break;
49
            case 'single':
50
                $this->singleLog();
51
                break;
52
            default:
53
                $this->line('Sorry, but this command only support Single and Daily log channels.');
54
        }
55
    }
56
57
    //  Empty the current Daily log file
58
    private function dailyLog()
59
    {
60
        $logFile = $this->getDailyPath();
61
62
        $this->cleanLogFile($logFile);
63
    }
64
65
    private function getDailyPath()
66
    {
67
        //  Break up the log file name and path to get the name of the current active log file
68
        $logPath = config('logging.channels.daily.path');
69
        $fileParts = pathinfo($logPath);
70
        $timeStamp = Carbon::now()->format('Y-m-d');
71
        $logFile = $fileParts['dirname'] . DIRECTORY_SEPARATOR . $fileParts['filename'] . '-' . $timeStamp . '.' . $fileParts['extension'];
72
73
        return $logFile;
74
    }
75
76
    //  Empty the current single log file
77
    private function singleLog()
78
    {
79
        $logFile = config('logging.channels.single.path');
80
81
        $this->cleanLogFile($logFile);
82
    }
83
84
    private function cleanLogFile($logFile)
85
    {
86
        //  Verify the file exists before trying to clear it
87
        if (file_exists($logFile)) {
88
            file_put_contents($logFile, '');
89
            $this->line('Log File Emptied');
90
        } else {
91
            $this->line('Well, this is embarrassing...');
92
            $this->line('It seems I cannot find your log file');
93
            $this->line('');
94
            $this->line('I was looking for ' . $logFile);
95
            $this->line('It either has not been created yet, or there was a problem with my algorithm');
96
        }
97
98
        $this->line('');
99
    }
100
}
101