Passed
Push — master ( 3c190c...2857c2 )
by Robbie
03:26
created

LDAPAllSyncJob   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 75
Duplicated Lines 20 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 15
loc 75
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getSignature() 0 3 1
A getTitle() 0 3 1
A getJobType() 0 3 1
A process() 6 17 2
A validateRegenerateTime() 7 7 3
A __construct() 0 2 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 Symbiote\QueuedJobs\Services\AbstractQueuedJob;
10
use Symbiote\QueuedJobs\Services\QueuedJob;
11
use Symbiote\QueuedJobs\Services\QueuedJobService;
12
13
/**
14
 * Class LDAPAllSyncJob
15
 *
16
 * A {@link QueuedJob} job to sync all groups and members to the site using LDAP.
17
 * This doesn't do the actual sync work, but rather just triggers {@link LDAPGroupSyncTask} and
18
 * {@link LDAPMemberSyncTask}
19
 */
20
class LDAPAllSyncJob extends AbstractQueuedJob
21
{
22
    /**
23
     * If you specify this value in seconds, it tells the completed job to queue another of itself
24
     * x seconds ahead of time.
25
     *
26
     * @var mixed
27
     * @config
28
     */
29
    private static $regenerate_time = null;
0 ignored issues
show
Unused Code introduced by
The property $regenerate_time is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
30
31
    public function __construct()
32
    {
33
        // noop, but needed for QueuedJobsAdmin::createjob() to work
34
    }
35
36
    /**
37
     * @return string
38
     */
39
    public function getJobType()
40
    {
41
        return QueuedJob::QUEUED;
42
    }
43
44
    /**
45
     * @return string
46
     */
47
    public function getTitle()
48
    {
49
        return _t(__CLASS__ . '.SYNCTITLE', 'Sync all groups and users from Active Directory, and set mappings up.');
50
    }
51
52
    /**
53
     * @return string
54
     */
55
    public function getSignature()
56
    {
57
        return md5(get_class($this));
58
    }
59
60
    /**
61
     * @throws Exception
62
     */
63 View Code Duplication
    public function validateRegenerateTime()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
    {
65
        $regenerateTime = Config::inst()->get(LDAPAllSyncJob::class, 'regenerate_time');
66
67
        // don't allow this job to run less than every 15 minutes, as it could take a while.
68
        if ($regenerateTime !== null && $regenerateTime < 900) {
69
            throw new Exception('LDAPAllSyncJob::regenerate_time must be 15 minutes or greater');
70
        }
71
    }
72
73
    /**
74
     * {@inheritDoc}
75
     */
76
    public function process()
77
    {
78
        $regenerateTime = Config::inst()->get(LDAPAllSyncJob::class, 'regenerate_time');
79 View Code Duplication
        if ($regenerateTime) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
80
            $this->validateRegenerateTime();
81
82
            $nextJob = Injector::inst()->create(LDAPAllSyncJob::class);
83
            singleton(QueuedJobService::class)->queueJob($nextJob, date('Y-m-d H:i:s', time() + $regenerateTime));
84
        }
85
86
        $task = Injector::inst()->create(LDAPGroupSyncTask::class);
87
        $task->run(null);
88
89
        $task = Injector::inst()->create(LDAPGroupSyncTask::class);
90
        $task->run(null);
91
92
        $this->isComplete = true;
93
    }
94
}
95