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

StaticJobManager::getJobCount()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
dl 0
loc 15
rs 9.2
c 2
b 0
f 1
cc 4
eloc 9
nc 3
nop 2
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