Completed
Push — 2.0 ( 76e968...4129d9 )
by Marco
13:01
created

ScheduleWorker   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 78
Duplicated Lines 26.92 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 11
dl 21
loc 78
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A spinup() 21 21 1
B loop() 0 30 3
A spindown() 0 6 1
A refreshPlans() 0 5 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 namespace Comodojo\Extender\Workers;
2
3
use \Comodojo\Daemon\Worker\AbstractWorker;
4
use \Comodojo\Extender\Task\Manager as TaskManager;
5
use \Comodojo\Extender\Schedule\Manager as ScheduleManager;
6
use \Comodojo\Extender\Task\Request;
7
use \Comodojo\Daemon\Traits\LoggerTrait;
8
use \Comodojo\Daemon\Traits\EventsTrait;
9
use \Comodojo\Extender\Traits\ConfigurationTrait;
10
use \Comodojo\Extender\Traits\TasksTableTrait;
11
use \Comodojo\Extender\Traits\EntityManagerTrait;
12
use \Comodojo\Extender\Traits\WorkerTrait;
13
14
class ScheduleWorker extends AbstractWorker {
15
16
    use ConfigurationTrait;
17
    use LoggerTrait;
18
    use EventsTrait;
19
    use TasksTableTrait;
20
    use EntityManagerTrait;
21
    use WorkerTrait;
22
23
    protected $wakeup_time = 0;
24
25 View Code Duplication
    public function spinup() {
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...
26
27
        $this->task_manager = new TaskManager(
28
            'schedule.worker',
29
            $this->getConfiguration(),
30
            $this->getLogger(),
31
            $this->getTasksTable(),
32
            $this->getEvents(),
33
            $this->getEntityManager()
34
        );
35
36
        $this->getEvents()->subscribe('daemon.worker.refresh', '\Comodojo\Extender\Listeners\RefreshScheduler');
37
38
        $this->job_manager = new ScheduleManager(
39
            $this->getConfiguration(),
40
            $this->getLogger(),
41
            $this->getEvents(),
42
            $this->getEntityManager()
43
        );
44
45
    }
46
47
    public function loop() {
48
49
        if ( $this->wakeup_time > time() ) {
50
            $this->logger->info('Still in sleep time, sorry');
51
            return;
52
        }
53
54
        $jobs = $this->job_manager->getJobs(true);
0 ignored issues
show
Bug introduced by
The method getJobs does only exist in Comodojo\Extender\Schedule\Manager, but not in Comodojo\Extender\Queue\Manager.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
55
56
        if ( empty($jobs) ) {
57
58
            $this->logger->debug('Nothing to do right now, sleeping... zzZZzZzZzz');
59
60
        } else {
61
62
            $this->logger->debug(count($jobs)." jobs will be executed");
63
64
            $requests = $this->jobsToRequests($jobs);
65
66
            $this->task_manager->addBulk($requests);
67
68
            $result = $this->task_manager->run();
69
70
            $this->job_manager->updateSchedules($result);
0 ignored issues
show
Bug introduced by
The method updateSchedules does only exist in Comodojo\Extender\Schedule\Manager, but not in Comodojo\Extender\Queue\Manager.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
71
72
        }
73
74
        $this->wakeup_time = $this->job_manager->getNextCycleTimestamp();
0 ignored issues
show
Bug introduced by
The method getNextCycleTimestamp does only exist in Comodojo\Extender\Schedule\Manager, but not in Comodojo\Extender\Queue\Manager.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
75
76
    }
77
78
    public function spindown() {
79
80
        unset($this->task_manager);
81
        unset($this->job_manager);
82
83
    }
84
85
    public function refreshPlans() {
86
87
        $this->wakeup_time = 0;
88
89
    }
90
91
}
92