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

LDAPMemberSyncJob   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 78
Duplicated Lines 23.08 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

6 Methods

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