1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dtc\QueueBundle\Doctrine; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
6
|
|
|
use Doctrine\Common\Persistence\ObjectRepository; |
7
|
|
|
use Dtc\QueueBundle\Manager\ArchivableJobManager; |
8
|
|
|
use Dtc\QueueBundle\Manager\JobTimingManager; |
9
|
|
|
use Dtc\QueueBundle\Manager\RunManager; |
10
|
|
|
|
11
|
|
|
abstract class BaseDoctrineJobManager extends ArchivableJobManager |
12
|
|
|
{ |
13
|
|
|
/** Number of jobs to prune / reset / gather at a time */ |
14
|
|
|
const FETCH_COUNT_MIN = 100; |
15
|
|
|
const FETCH_COUNT_MAX = 500; |
16
|
|
|
const SAVE_COUNT_MIN = 10; |
17
|
|
|
const SAVE_COUNT_MAX = 100; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var ObjectManager |
21
|
|
|
*/ |
22
|
|
|
protected $objectManager; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* DoctrineJobManager constructor. |
26
|
|
|
* |
27
|
20 |
|
* @param RunManager $runManager |
28
|
|
|
* @param JobTimingManager $jobTimingManager |
29
|
|
|
* @param ObjectManager $objectManager |
30
|
|
|
* @param $jobClass |
31
|
|
|
* @param $jobArchiveClass |
32
|
|
|
*/ |
33
|
|
|
public function __construct( |
34
|
20 |
|
RunManager $runManager, |
35
|
20 |
|
JobTimingManager $jobTimingManager, |
36
|
20 |
|
ObjectManager $objectManager, |
37
|
|
|
$jobClass, |
38
|
|
|
$jobArchiveClass |
39
|
|
|
) { |
40
|
|
|
$this->objectManager = $objectManager; |
41
|
57 |
|
parent::__construct($runManager, $jobTimingManager, $jobClass, $jobArchiveClass); |
42
|
|
|
} |
43
|
57 |
|
|
44
|
|
View Code Duplication |
protected function getFetchCount($totalCount) |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
$fetchCount = intval($totalCount / 10); |
47
|
|
|
if ($fetchCount < self::FETCH_COUNT_MIN) { |
48
|
|
|
$fetchCount = self::FETCH_COUNT_MIN; |
49
|
27 |
|
} |
50
|
|
|
if ($fetchCount > self::FETCH_COUNT_MAX) { |
51
|
27 |
|
$fetchCount = self::FETCH_COUNT_MAX; |
52
|
|
|
} |
53
|
|
|
|
54
|
15 |
|
return $fetchCount; |
55
|
|
|
} |
56
|
15 |
|
|
57
|
15 |
View Code Duplication |
protected function getSaveCount($totalCount) |
|
|
|
|
58
|
|
|
{ |
59
|
20 |
|
$saveCount = intval($totalCount / 10); |
60
|
|
|
if ($saveCount < self::SAVE_COUNT_MIN) { |
61
|
20 |
|
$saveCount = self::SAVE_COUNT_MIN; |
62
|
20 |
|
} |
63
|
20 |
|
if ($saveCount > self::SAVE_COUNT_MAX) { |
64
|
20 |
|
$saveCount = self::SAVE_COUNT_MAX; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $saveCount; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return ObjectManager |
72
|
|
|
*/ |
73
|
|
|
public function getObjectManager() |
74
|
|
|
{ |
75
|
|
|
return $this->objectManager; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return ObjectRepository |
80
|
|
|
*/ |
81
|
|
|
public function getRepository() |
82
|
|
|
{ |
83
|
|
|
return $this->getObjectManager()->getRepository($this->getJobClass()); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
protected function flush() |
87
|
|
|
{ |
88
|
|
|
$this->getObjectManager()->flush(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function deleteJob(\Dtc\QueueBundle\Model\Job $job) |
92
|
|
|
{ |
93
|
|
|
$objectManager = $this->getObjectManager(); |
94
|
|
|
$objectManager->remove($job); |
95
|
|
|
$objectManager->flush(); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
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.