Completed
Push — master ( 38310f...b83e6e )
by Matthew
08:14
created

DoctrineJobTimingManager::performRecording()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Dtc\QueueBundle\Doctrine;
4
5
use Doctrine\Common\Persistence\ObjectManager;
6
use Dtc\QueueBundle\Model\JobTiming;
7
use Dtc\QueueBundle\Manager\JobTimingManager;
8
9
abstract class DoctrineJobTimingManager extends JobTimingManager
10
{
11
    /** @var ObjectManager */
12
    protected $objectManager;
13
14 18
    public function __construct(ObjectManager $objectManager, $jobTimingClass, $recordTimings)
15
    {
16 18
        $this->objectManager = $objectManager;
17 18
        parent::__construct($jobTimingClass, $recordTimings);
18 18
    }
19
20
    /**
21
     * @return ObjectManager
22
     */
23 44
    public function getObjectManager()
24
    {
25 44
        return $this->objectManager;
26
    }
27
28 44
    public function performRecording($status, \DateTime $finishedAt = null)
29
    {
30 44
        if (null === $finishedAt) {
31 43
            $finishedAt = \Dtc\QueueBundle\Util\Util::getMicrotimeDateTime();
32
        }
33
34
        /** @var JobTiming $jobTiming */
35 44
        $jobTiming = new $this->jobTimingClass();
36 44
        $jobTiming->setFinishedAt($finishedAt);
37 44
        $jobTiming->setStatus($status);
38 44
        $objectManager = $this->getObjectManager();
39 44
        $objectManager->persist($jobTiming);
40 44
        $objectManager->flush();
41 44
    }
42
43
    abstract protected function removeOlderThan($objectName, $field, \DateTime $olderThan);
44
45
    public function pruneJobTimings(\DateTime $olderThan)
46
    {
47
        return $this->removeOlderThan($this->getJobTimingClass(), 'createdAt', $olderThan);
48
    }
49
}
50