LaravelScheduleLogger::end()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace PendoNL\LaravelScheduleLogger;
4
5
class LaravelScheduleLogger
6
{
7
    /**
8
     * Log the start of an event.
9
     *
10
     * @param $command_name
11
     *
12
     * @return mixed
13
     */
14
    public function start($command_name)
15
    {
16
        return $this->log($command_name);
17
    }
18
19
    /**
20
     * Log the end of an event.
21
     *
22
     * @param $command_name
23
     *
24
     * @return mixed
25
     */
26
    public function end($command_name)
27
    {
28
        return $this->log($command_name);
29
    }
30
31
    /**
32
     * Save the event to the database.
33
     *
34
     * @param $command_name
35
     *
36
     * @return mixed
37
     */
38
    public function log($command_name)
39
    {
40
        $log = Schedulelog::where('end', null)->latest('id', 'DESC')->first();
41
42
        if (count($log) == 0) {
43
            return Schedulelog::create([
44
                'command_name' => $command_name,
45
                'start'        => microtime(true),
46
            ]);
47
        } else {
48
            $log->end = microtime(true);
49
            $log->save();
50
51
            return $log;
52
        }
53
    }
54
55
    /**
56
     * Returns the execution time in ms.
57
     *
58
     * @param Schedulelog $log
59
     *
60
     * @return mixed
61
     */
62
    public function getExecutionTime(Schedulelog $log)
63
    {
64
        return $log->end - $log->start;
65
    }
66
}
67