Completed
Push — 2.0 ( dbbe01...d43752 )
by Marco
11:55
created

QueueWorker::loop()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 40
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 40
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 22
nc 2
nop 0
1
<?php namespace Comodojo\Extender\Workers;
2
3
use \Comodojo\Daemon\Worker\AbstractWorker;
4
use \Comodojo\Foundation\Logging\LoggerTrait;
5
use \Comodojo\Foundation\Events\EventsTrait;
6
use \Comodojo\Extender\Queue\Manager as QueueManager;
7
use \Comodojo\Extender\Task\Locker;
8
use \Comodojo\Extender\Task\Manager as TaskManager;
9
use \Comodojo\Extender\Traits\TasksTableTrait;
10
use \Comodojo\Foundation\Base\ConfigurationTrait;
11
use \Comodojo\Extender\Traits\WorkerTrait;
12
13
class QueueWorker extends AbstractWorker {
14
15
    use ConfigurationTrait;
16
    use LoggerTrait;
17
    use EventsTrait;
18
    use TasksTableTrait;
19
    use WorkerTrait;
20
21
    protected $locker;
22
23 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...
24
25
        $configuration = $this->getConfiguration();
26
27
        $base_path = $configuration->get('base-path');
28
        $lock_path = $configuration->get('run-path');
29
        $lock_file = "$base_path/$lock_path/queue.worker.lock";
30
31
        $this->locker = new Locker($lock_file);
32
        $this->locker->lock([]);
33
34
    }
35
36
    public function loop() {
37
38
        $task_manager = new TaskManager(
39
            $this->locker,
40
            $this->getConfiguration(),
41
            $this->getLogger(),
42
            $this->getTasksTable(),
43
            $this->getEvents()
44
        );
45
46
        $queue_manager = new QueueManager(
47
            $this->getConfiguration(),
48
            $this->getLogger(),
49
            $this->getEvents()
50
        );
51
52
        $queue = $queue_manager->get();
53
54
        if ( !empty($queue) ) {
55
56
            $requests = $this->jobsToRequests($queue);
57
58
            $queue_manager->remove($queue);
59
            unset($queue_manager);
60
61
            $result = $task_manager->addBulk($requests)->run();
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
62
            unset($task_manager);
63
64
        } else {
65
66
            unset($queue_manager);
67
            unset($task_manager);
68
69
        }
70
71
72
73
        $this->locker->lock([]);
74
75
    }
76
77
    public function spindown() {
78
79
        $this->locker->release();
80
81
    }
82
83
}
84