Passed
Push — master ( 1afe5c...e9653b )
by Matthew
11:56 queued 05:32
created

BaseDoctrineJobManager   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Test Coverage

Coverage 89.47%

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 94
ccs 34
cts 38
cp 0.8947
rs 10
c 0
b 0
f 0
wmc 14

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getObjectManager() 0 3 1
A deleteJob() 0 3 1
A getRepository() 0 3 1
A flush() 0 3 1
A __construct() 0 9 1
A getSaveCount() 0 11 3
A getFetchCount() 0 11 3
A addWorkerNameMethod() 0 7 3
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
     * @param RunManager       $runManager
28
     * @param JobTimingManager $jobTimingManager
29
     * @param ObjectManager    $objectManager
30
     * @param $jobClass
31
     * @param $jobArchiveClass
32
     */
33 19
    public function __construct(
34
        RunManager $runManager,
35
        JobTimingManager $jobTimingManager,
36
        ObjectManager $objectManager,
37
        $jobClass,
38
        $jobArchiveClass
39
    ) {
40 19
        $this->objectManager = $objectManager;
41 19
        parent::__construct($runManager, $jobTimingManager, $jobClass, $jobArchiveClass);
42 19
    }
43
44 7
    protected function getFetchCount($totalCount)
45
    {
46 7
        $fetchCount = intval($totalCount / 10);
47 7
        if ($fetchCount < self::FETCH_COUNT_MIN) {
48 7
            $fetchCount = self::FETCH_COUNT_MIN;
49 7
        }
50 7
        if ($fetchCount > self::FETCH_COUNT_MAX) {
51
            $fetchCount = self::FETCH_COUNT_MAX;
52
        }
53
54 7
        return $fetchCount;
55
    }
56
57 6
    protected function getSaveCount($totalCount)
58
    {
59 6
        $saveCount = intval($totalCount / 10);
60 6
        if ($saveCount < self::SAVE_COUNT_MIN) {
61 6
            $saveCount = self::SAVE_COUNT_MIN;
62 6
        }
63 6
        if ($saveCount > self::SAVE_COUNT_MAX) {
64
            $saveCount = self::SAVE_COUNT_MAX;
65
        }
66
67 6
        return $saveCount;
68
    }
69
70
    /**
71
     * @return ObjectManager
72
     */
73 56
    public function getObjectManager()
74
    {
75 56
        return $this->objectManager;
76
    }
77
78
    /**
79
     * @return ObjectRepository
80
     */
81 30
    public function getRepository()
82
    {
83 30
        return $this->getObjectManager()->getRepository($this->getJobClass());
84
    }
85
86 11
    protected function flush()
87
    {
88 11
        $this->getObjectManager()->flush();
89 11
    }
90
91 20
    public function deleteJob(\Dtc\QueueBundle\Model\Job $job)
92
    {
93 20
        $this->persist($job, 'remove');
94 20
    }
95
96
    abstract protected function persist($object, $action = 'persist');
97
98 9
    protected function addWorkerNameMethod(array &$criterion, $workerName = null, $method = null)
99
    {
100 9
        if (null !== $workerName) {
101 4
            $criterion['workerName'] = $workerName;
102 4
        }
103 9
        if (null !== $method) {
104 4
            $criterion['method'] = $method;
105 4
        }
106 9
    }
107
}
108