RemoveOldLogEntriesTask::run()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverLeague\LogViewer\Task;
4
5
use SilverLeague\LogViewer\Model\LogEntry;
6
use SilverStripe\Dev\BuildTask;
7
use SilverStripe\Core\Config\Config;
8
use SilverStripe\CronTask\Interfaces\CronTask;
9
10
/**
11
 * Remove old LogEntry records from the database
12
 *
13
 * @package silverstripe-logviewer
14
 * @author  Robbie Averill <[email protected]>
15
 */
16
class RemoveOldLogEntriesTask extends BuildTask implements CronTask
17
{
18
    /**
19
     * {@inheritDoc}
20
     */
21
    private static $segment = 'RemoveOldLogEntriesTask';
0 ignored issues
show
introduced by
The private property $segment is not used, and could be removed.
Loading history...
22
23
    /**
24
     * {@inheritDoc}
25
     */
26
    protected $title = 'Remove LogEntry records older than a "n" days';
27
28
    /**
29
     * {@inheritDoc}
30
     */
31
    protected $description = 'Removes LogEntry records that are older than the configured `LogViewer.max_log_age`.';
32
33
    /**
34
     * BuildTask implementation
35
     *
36
     * {@inheritDoc}
37
     *
38
     * @return bool Whether anything was removed
39
     */
40
    public function run($request)
41
    {
42
        return $this->removeOldLogs();
43
    }
44
45
    /**
46
     * CronTask implementation - can be disabled with YAML configuration
47
     *
48
     * {@inheritDoc}
49
     *
50
     * @return bool Whether anything was removed
51
     */
52
    public function process()
53
    {
54
        if (!$this->getCronEnabled()) {
55
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the return type mandated by SilverStripe\CronTask\In...ces\CronTask::process() of void.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
56
        }
57
        return $this->removeOldLogs();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->removeOldLogs() returns the type boolean which is incompatible with the return type mandated by SilverStripe\CronTask\In...ces\CronTask::process() of void.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
58
    }
59
60
    /**
61
     * {@inheritDoc}
62
     */
63
    public function getSchedule()
64
    {
65
        return Config::inst()->get(LogEntry::class, 'cron_schedule');
66
    }
67
68
    /**
69
     * Get the maximum age allowed for a LogEntry from configuration
70
     *
71
     * @return int
72
     */
73
    public function getMaxAge()
74
    {
75
        return (int) Config::inst()->get(LogEntry::class, 'max_log_age');
76
    }
77
78
    /**
79
     * Return whether the cron functionality is enabled from configuration
80
     *
81
     * @return bool
82
     */
83
    public function getCronEnabled()
84
    {
85
        return (bool) Config::inst()->get(LogEntry::class, 'cron_enabled');
86
    }
87
88
    /**
89
     * Remove LogEntry records older than the LogViewer.max_log_age days
90
     *
91
     * @return bool Whether anything was deleted or not
92
     */
93
    protected function removeOldLogs()
94
    {
95
        $tableName = LogEntry::singleton()->baseTable();
96
        $maxAge = $this->getMaxAge();
97
98
        $logs = LogEntry::get()
99
            ->where(sprintf('"%s"."Created" <= NOW() - INTERVAL \'%d\' DAY', $tableName, $maxAge));
100
101
        $count = $logs->count();
102
        if ($count > 0) {
103
            $logs->removeAll();
104
        }
105
106
        echo sprintf('Finished. Removed %d log(s) older than %d day(s).', $count, $maxAge) . PHP_EOL;
107
108
        return $count > 0;
109
    }
110
}
111