Test Setup Failed
Pull Request — master (#7)
by Matthew
14:04
created

JobManager::getJobCount()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 5

Duplication

Lines 9
Ratio 75 %

Importance

Changes 0
Metric Value
dl 9
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 2
1
<?php
2
3
namespace Dtc\QueueBundle\Beanstalkd;
4
5
use Dtc\QueueBundle\Model\Job as BaseJob;
6
use Dtc\QueueBundle\Model\JobManagerInterface;
7
use Pheanstalk\Pheanstalk;
8
9
class JobManager implements JobManagerInterface
10
{
11
    const DEFAULT_RESERVE_TIMEOUT = 5; // seconds
12
13
    /** @var Pheanstalk */
14
    protected $beanstalkd;
15
16
    protected $tube;
17
18
    protected $reserveTimeout = self::DEFAULT_RESERVE_TIMEOUT;
19
20
    public function setBeanstalkd(Pheanstalk $beanstalkd)
21
    {
22
        $this->beanstalkd = $beanstalkd;
23
    }
24
25
    public function setTube($tube)
26
    {
27
        $this->tube = $tube;
28
    }
29
30
    public function setReserveTimeout($timeout)
31
    {
32
        $this->reserveTimeout = $timeout;
33
    }
34
35
    public function save(\Dtc\QueueBundle\Model\Job $job)
36
    {
37
        /** @var Job $job */
38
        $arguments = [$job->toMessage(), $job->getPriority(), $job->getDelay(), $job->getTtr()];
39
        $method = 'put';
40
        if ($this->tube) {
41
            array_unshift($arguments, $this->tube);
42
            $method .= 'InTube';
43
        }
44
        var_dump(get_class($this->beanstalkd));
0 ignored issues
show
Security Debugging Code introduced by
var_dump(get_class($this->beanstalkd)); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
45
        var_dump($method);
46
        $jobId = call_user_func_array([$this->beanstalkd, $method], $arguments);
47
        $job->setId($jobId);
48
49
        return $job;
50
    }
51
52
    public function getJob($workerName = null, $methodName = null, $prioritize = true)
53
    {
54
        if ($methodName) {
55
            throw new \Exception('Unsupported');
56
        }
57
58
        $beanJob = $this->beanstalkd;
59
        if ($this->tube) {
60
            $beanJob = $beanJob->watch($this->tube);
61
        }
62
63
        $beanJob = $beanJob->reserve($this->reserveTimeout);
64
        if ($beanJob) {
65
            $job = new Job();
66
            $job->fromMessage($beanJob->getData());
67
            $job->setId($beanJob->getId());
68
69
            return $job;
70
        }
71
    }
72
73
    public function deleteJob(\Dtc\QueueBundle\Model\Job $job)
74
    {
75
        $this->beanstalkd
76
            ->delete($job);
77
    }
78
79
    // Save History get called upon completion of the job
80
    public function saveHistory(\Dtc\QueueBundle\Model\Job $job)
81
    {
82
        if ($job->getStatus() === BaseJob::STATUS_SUCCESS) {
83
            $this->beanstalkd
84
                ->delete($job);
85
        }
86
87
        // @Todo Need a strategy for buried jobs, if any?
0 ignored issues
show
Unused Code Comprehensibility introduced by
36% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
88
//        else {
89
//            $this->beanstalkd
90
//                ->bury($job);
91
//        }
92
    }
93
94 View Code Duplication
    public function getJobCount($workerName = null, $methodName = null)
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...
95
    {
96
        if ($methodName) {
97
            throw new \Exception('Unsupported');
98
        }
99
100
        if ($workerName) {
101
            throw new \Exception('Unsupported');
102
        }
103
104
        // @Todo - use statistics
105
    }
106
107
    public function getStats()
108
    {
109
        return $this->beanstalkd->stats();
110
    }
111
112
    public function resetErroneousJobs($workerName = null, $methodName = null)
113
    {
114
        throw new \Exception('Unsupported');
115
    }
116
117
    public function pruneErroneousJobs($workerName = null, $methodName = null)
118
    {
119
        throw new \Exception('Unsupported');
120
    }
121
122
    public function getStatus()
123
    {
124
        throw new \Exception('Unsupported');
125
    }
126
}
127