Completed
Pull Request — master (#53)
by Matthew
12:05
created

BaseDoctrineJobManager::getObjectManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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
    use CommonTrait;
20
21
    /**
22
     * @var ObjectManager
23
     */
24
    protected $objectManager;
25
26
    /**
27
     * DoctrineJobManager constructor.
28
     *
29
     * @param RunManager       $runManager
30
     * @param JobTimingManager $jobTimingManager
31
     * @param ObjectManager    $objectManager
32
     * @param $jobClass
33
     * @param $jobArchiveClass
34
     */
35 19
    public function __construct(
36
        RunManager $runManager,
37
        JobTimingManager $jobTimingManager,
38
        ObjectManager $objectManager,
39
        $jobClass,
40
        $jobArchiveClass
41
    ) {
42 19
        $this->objectManager = $objectManager;
43 19
        parent::__construct($runManager, $jobTimingManager, $jobClass, $jobArchiveClass);
44 19
    }
45
46 7
    protected function getFetchCount($totalCount)
47
    {
48 7
        $fetchCount = intval($totalCount / 10);
49 7
        if ($fetchCount < self::FETCH_COUNT_MIN) {
50 7
            $fetchCount = self::FETCH_COUNT_MIN;
51
        }
52 7
        if ($fetchCount > self::FETCH_COUNT_MAX) {
53
            $fetchCount = self::FETCH_COUNT_MAX;
54
        }
55
56 7
        return $fetchCount;
57
    }
58
59 6
    protected function getSaveCount($totalCount)
60
    {
61 6
        $saveCount = intval($totalCount / 10);
62 6
        if ($saveCount < self::SAVE_COUNT_MIN) {
63 6
            $saveCount = self::SAVE_COUNT_MIN;
64
        }
65 6
        if ($saveCount > self::SAVE_COUNT_MAX) {
66
            $saveCount = self::SAVE_COUNT_MAX;
67
        }
68
69 6
        return $saveCount;
70
    }
71
72
    /**
73
     * @return ObjectManager
74
     */
75 56
    public function getObjectManager()
76
    {
77 56
        return $this->objectManager;
78
    }
79
80
    /**
81
     * @return ObjectRepository
82
     */
83 28
    public function getRepository()
84
    {
85 28
        return $this->getObjectManager()->getRepository($this->getJobClass());
86
    }
87
88 11
    protected function flush()
89
    {
90 11
        $this->getObjectManager()->flush();
91 11
    }
92
93 20
    public function deleteJob(\Dtc\QueueBundle\Model\Job $job)
94
    {
95 20
        $this->persist($job, 'remove');
96 20
    }
97
}
98