Completed
Pull Request — master (#40)
by Matthew
17:20
created

StaticJobManager   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 3
Bugs 1 Features 2
Metric Value
wmc 17
lcom 1
cbo 2
dl 0
loc 77
rs 10
c 3
b 1
f 2

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getStatus() 0 4 1
A deleteJob() 0 4 1
B getJob() 0 12 5
A save() 0 14 3
A compareJobPriority() 0 4 1
A saveHistory() 0 4 1
A getWaitingJobCount() 0 15 4
1
<?php
2
3
namespace Dtc\QueueBundle\Tests;
4
5
use Dtc\QueueBundle\Manager\AbstractJobManager;
6
use Dtc\QueueBundle\Model\Job;
7
use Dtc\QueueBundle\Manager\JobTimingManager;
8
use Dtc\QueueBundle\Manager\RunManager;
9
10
/**
11
 * @author David Tee
12
 *
13
 *	Created for Unitesting purposes.
14
 */
15
class StaticJobManager extends AbstractJobManager
16
{
17
    private $jobs;
18
    private $uniqeId;
19
    public $enableSorting = true;
20
21
    public function __construct(RunManager $runManager, JobTimingManager $jobTimingManager, $jobClass)
22
    {
23
        $this->jobs = array();
24
        $this->uniqeId = 1;
25
        parent::__construct($runManager, $jobTimingManager, $jobClass);
26
    }
27
28
    public function getWaitingJobCount($workerName = null, $methodName = null)
29
    {
30
        if ($workerName && isset($this->jobs[$workerName])) {
31
            return count($this->jobs[$workerName]);
32
        }
33
34
        $total = 0;
35
        foreach ($this->jobs as $jobWorkerName => $jobs) {
36
            $total += count($this->jobs[$jobWorkerName]);
37
        }
38
39
        return array_sum(array_map(function ($jobs) {
40
            return count($jobs);
41
        }, $this->jobs));
42
    }
43
44
    public function getStatus()
45
    {
46
        return null;
47
    }
48
49
    public function deleteJob(Job $job)
50
    {
51
        unset($this->jobs[$job->getWorkerName()][$job->getId()]);
52
    }
53
54
    public function getJob($workerName = null, $methodName = null, $prioritize = true, $runId = null)
55
    {
56
        if ($workerName && isset($this->jobs[$workerName])) {
57
            return array_pop($this->jobs[$workerName]);
58
        }
59
60
        foreach ($this->jobs as $jobWorkerName => &$jobs) {
61
            if ($jobs) {
62
                return array_pop($jobs);
63
            }
64
        }
65
    }
66
67
    public function save(Job $job)
68
    {
69
        if (!$job->getId()) {
70
            $job->setId($this->uniqeId);
71
            $this->jobs[$job->getWorkerName()][$this->uniqeId] = $job;
72
            if ($this->enableSorting) {
73
                uasort($this->jobs[$job->getWorkerName()], array($this, 'compareJobPriority'));
74
            }
75
76
            ++$this->uniqeId;
77
        }
78
79
        return $job;
80
    }
81
82
    public function compareJobPriority(Job $a, Job $b)
83
    {
84
        return $b->getPriority() - $a->getPriority();
85
    }
86
87
    public function saveHistory(Job $job)
88
    {
89
        $this->save($job);
90
    }
91
}
92