Completed
Push — master ( 7fae80...8ffdf8 )
by Matthew
07:25
created

BaseJobManager   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 88
Duplicated Lines 15.91 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 92.11%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 7
dl 14
loc 88
ccs 35
cts 38
cp 0.9211
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A setRedis() 0 4 1
A getJobCacheKey() 0 4 1
A getJobCrcHashKey() 0 4 1
A getPriorityQueueCacheKey() 0 4 1
A getWhenQueueCacheKey() 0 4 1
A getStatusCacheKey() 0 4 1
saveJob() 0 1 ?
A resetJob() 14 14 2
A deleteJob() 0 11 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
2
3
namespace Dtc\QueueBundle\Redis;
4
5
use Dtc\QueueBundle\Manager\JobIdTrait;
6
use Dtc\QueueBundle\Manager\JobTimingManager;
7
use Dtc\QueueBundle\Manager\PriorityJobManager;
8
use Dtc\QueueBundle\Manager\RunManager;
9
use Dtc\QueueBundle\Manager\VerifyTrait;
10
use Dtc\QueueBundle\Model\BaseJob;
11
use Dtc\QueueBundle\Model\RetryableJob;
12
use Dtc\QueueBundle\Util\Util;
13
14
/**
15
 * For future implementation.
16
 */
17
abstract class BaseJobManager extends PriorityJobManager
18
{
19
    use JobIdTrait;
20
    use VerifyTrait;
21
22
    /** @var RedisInterface */
23
    protected $redis;
24
25
    /** @var string */
26
    protected $cacheKeyPrefix;
27
28
    protected $hostname;
29
    protected $pid;
30
31
    /**
32
     * @param string $cacheKeyPrefix
33
     */
34 3
    public function __construct(RunManager $runManager, JobTimingManager $jobTimingManager, $jobClass, $cacheKeyPrefix)
35
    {
36 3
        $this->cacheKeyPrefix = $cacheKeyPrefix;
37 3
        $this->hostname = gethostname() ?: '';
38 3
        $this->pid = getmypid();
39
40 3
        parent::__construct($runManager, $jobTimingManager, $jobClass);
41 3
    }
42
43
    public function setRedis(RedisInterface $redis)
44
    {
45
        $this->redis = $redis;
46
    }
47
48 28
    protected function getJobCacheKey($jobId)
49
    {
50 28
        return $this->cacheKeyPrefix.'_job_'.$jobId;
51
    }
52
53
    /**
54
     * @param string $jobCrc
55
     */
56 28
    protected function getJobCrcHashKey($jobCrc)
57
    {
58 28
        return $this->cacheKeyPrefix.'_job_crc_'.$jobCrc;
59
    }
60
61 14
    protected function getPriorityQueueCacheKey()
62
    {
63 14
        return $this->cacheKeyPrefix.'_priority';
64
    }
65
66 28
    protected function getWhenQueueCacheKey()
67
    {
68 28
        return $this->cacheKeyPrefix.'_when';
69
    }
70
71 18
    protected function getStatusCacheKey()
72
    {
73 18
        return $this->cacheKeyPrefix.'_status';
74
    }
75
76
    abstract protected function saveJob(Job $job);
77
78 9 View Code Duplication
    public function resetJob(RetryableJob $job)
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...
79
    {
80 9
        if (!$job instanceof Job) {
81 3
            throw new \InvalidArgumentException('$job must be instance of '.Job::class);
82
        }
83 6
        $job->setStatus(BaseJob::STATUS_NEW);
84 6
        $job->setMessage(null);
85 6
        $job->setStartedAt(null);
86 6
        $job->setRetries($job->getRetries() + 1);
87 6
        $job->setUpdatedAt(Util::getMicrotimeDateTime());
88 6
        $this->saveJob($job);
89
90 6
        return true;
91
    }
92
93 3
    public function deleteJob(\Dtc\QueueBundle\Model\Job $job)
94
    {
95 3
        $jobId = $job->getId();
96 3
        $priorityQueue = $this->getPriorityQueueCacheKey();
97 3
        $whenQueue = $this->getWhenQueueCacheKey();
98
99 3
        $this->redis->zRem($priorityQueue, $jobId);
100 3
        $this->redis->zRem($whenQueue, $jobId);
101 3
        $this->redis->del([$this->getJobCacheKey($jobId)]);
102 3
        $this->redis->lRem($this->getJobCrcHashKey($job->getCrcHash()), 1, $jobId);
103 3
    }
104
}
105