Passed
Push — master ( b2198e...e8e6de )
by Robbie
02:50 queued 10s
created

src/Jobs/LDAPAllSyncJob.php (1 issue)

Severity
1
<?php
2
3
namespace SilverStripe\LDAP\Jobs;
4
5
use Exception;
6
use SilverStripe\LDAP\Tasks\LDAPGroupSyncTask;
7
use SilverStripe\Core\Config\Config;
8
use SilverStripe\Core\Injector\Injector;
9
use SilverStripe\LDAP\Tasks\LDAPMemberSyncTask;
10
use Symbiote\QueuedJobs\Services\AbstractQueuedJob;
11
use Symbiote\QueuedJobs\Services\QueuedJob;
12
use Symbiote\QueuedJobs\Services\QueuedJobService;
13
14
/**
15
 * Class LDAPAllSyncJob
16
 *
17
 * A {@link QueuedJob} job to sync all groups and members to the site using LDAP.
18
 * This doesn't do the actual sync work, but rather just triggers {@link LDAPGroupSyncTask} and
19
 * {@link LDAPMemberSyncTask}
20
 */
21
class LDAPAllSyncJob extends AbstractQueuedJob
22
{
23
    /**
24
     * If you specify this value in seconds, it tells the completed job to queue another of itself
25
     * x seconds ahead of time.
26
     *
27
     * @var mixed
28
     * @config
29
     */
30
    private static $regenerate_time = null;
0 ignored issues
show
The private property $regenerate_time is not used, and could be removed.
Loading history...
31
32
    public function __construct()
33
    {
34
        // noop, but needed for QueuedJobsAdmin::createjob() to work
35
    }
36
37
    /**
38
     * @return string
39
     */
40
    public function getJobType()
41
    {
42
        return QueuedJob::QUEUED;
43
    }
44
45
    /**
46
     * @return string
47
     */
48
    public function getTitle()
49
    {
50
        return _t(__CLASS__ . '.SYNCTITLE', 'Sync all groups and users from Active Directory, and set mappings up.');
51
    }
52
53
    /**
54
     * @return string
55
     */
56
    public function getSignature()
57
    {
58
        return md5(get_class($this));
59
    }
60
61
    /**
62
     * @throws Exception
63
     */
64
    public function validateRegenerateTime()
65
    {
66
        $regenerateTime = Config::inst()->get(LDAPAllSyncJob::class, 'regenerate_time');
67
68
        // don't allow this job to run less than every 15 minutes, as it could take a while.
69
        if ($regenerateTime !== null && $regenerateTime < 900) {
70
            throw new Exception('LDAPAllSyncJob::regenerate_time must be 15 minutes or greater');
71
        }
72
    }
73
74
    /**
75
     * {@inheritDoc}
76
     */
77
    public function process()
78
    {
79
        $regenerateTime = Config::inst()->get(LDAPAllSyncJob::class, 'regenerate_time');
80
        if ($regenerateTime) {
81
            $this->validateRegenerateTime();
82
83
            $nextJob = Injector::inst()->create(LDAPAllSyncJob::class);
84
            singleton(QueuedJobService::class)->queueJob($nextJob, date('Y-m-d H:i:s', time() + $regenerateTime));
85
        }
86
87
        $task = Injector::inst()->create(LDAPGroupSyncTask::class);
88
        $task->run(null);
89
90
        $task = Injector::inst()->create(LDAPMemberSyncTask::class);
91
        $task->run(null);
92
93
        $this->isComplete = true;
94
    }
95
}
96