Issues (47)

src/SimpleJobsDailyTask.php (2 issues)

1
<?php
2
3
namespace LeKoala\SimpleJobs;
4
5
use SilverStripe\ORM\DataList;
6
use SilverStripe\Core\Config\Configurable;
7
use SilverStripe\ORM\FieldType\DBDatetime;
8
use SilverStripe\CronTask\Interfaces\CronTask;
9
use SilverStripe\SessionManager\Models\LoginSession;
10
11
/**
12
 */
13
class SimpleJobsDailyTask implements CronTask, SimpleJobsDescription
14
{
15
    use Configurable;
16
17
    public static function getJobTitle(): string
18
    {
19
        return 'Session collection service';
20
    }
21
22
    public static function getJobCategory(): string
23
    {
24
        return SimpleJobsDescription::SYSTEM;
25
    }
26
27
    public static function getJobDescription(): string
28
    {
29
        return 'Collects expired session in the database';
30
    }
31
32
    /**
33
     * Logic extracted roughly from GarbageCollectionService that doesn't expose what it collects
34
     * so we need to run our own query
35
     */
36
    protected static function getExpiredSessions(): DataList
37
    {
38
        $maxAge = LoginSession::getMaxAge();
39
        $nowTs = DBDatetime::now()->getTimestamp();
40
        $now = date('Y-m-d H:i:s', $nowTs);
41
42
        $case1 = "(LastAccessed < '$maxAge' AND Persistent = 0)";
43
        $case2 = "(Persistent = 1 AND RememberLoginHash.ExpiryDate < '$now')";
44
        $case3 = "(LastAccessed < '$maxAge' AND Persistent = 1 AND RememberLoginHash.ExpiryDate IS NULL)";
45
46
        $list = LoginSession::get()->leftJoin('RememberLoginHash', 'RememberLoginHash.LoginSessionID = LoginSession.ID');
47
        $list = $list->where("$case1 OR $case2 OR $case3");
48
        return $list;
49
    }
50
51
    public static function getJobRecords(): ?DataList
52
    {
53
        return null;
54
55
        //@link https://github.com/silverstripe/silverstripe-session-manager/issues/178
56
        // return self::getExpiredSessions();
57
    }
58
59
    /**
60
     * Return a string for a CRON expression. If a "falsy" value is returned, the CronTaskController will assume the
61
     * CronTask is disabled.
62
     *
63
     * @return string
64
     */
65
    public function getSchedule()
66
    {
67
        return SimpleJobsSchedules::EVERY_DAY;
68
    }
69
70
    public static function IsDisabled(): bool
71
    {
72
        $config = static::config();
73
        $sessionGarbageCollector = \SilverStripe\SessionManager\Services\GarbageCollectionService::class;
74
        if (class_exists($sessionGarbageCollector) && $config->clean_sessions) {
75
            return false;
76
        }
77
        return true;
78
    }
79
80
    /**
81
     * When this script is supposed to run the CronTaskController will execute
82
     * process().
83
     *
84
     * @return mixed
85
     */
86
    public function process()
87
    {
88
        if (self::IsDisabled()) {
89
            return 'disabled';
0 ignored issues
show
Bug Best Practice introduced by
The expression return 'disabled' returns the type string 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...
90
        }
91
92
        // Hard to know what this will be doing
93
        // @link https://github.com/silverstripe/silverstripe-session-manager/issues/160
94
        \SilverStripe\SessionManager\Services\GarbageCollectionService::singleton()->collect();
95
        return 'done';
0 ignored issues
show
Bug Best Practice introduced by
The expression return 'done' returns the type string 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...
96
    }
97
}
98