Passed
Push — master ( f67228...ea1bad )
by Matthew
08:44
created

CommonTrait::persist()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Dtc\QueueBundle\ODM;
4
5
use Doctrine\Common\Persistence\ObjectManager;
6
use Doctrine\ODM\MongoDB\DocumentManager;
7
use Dtc\QueueBundle\Document\Job;
8
use Dtc\QueueBundle\Document\JobTiming;
9
use Dtc\QueueBundle\Document\Run;
10
11
trait CommonTrait
12
{
13
    /**
14
     * @param string    $objectName
15
     * @param string    $field
16
     * @param \DateTime $olderThan
17
     *
18
     * @return int
19
     */
20 2
    protected function removeOlderThan($objectName, $field, \DateTime $olderThan)
21
    {
22
        /** @var DocumentManager $objectManager */
23 2
        $objectManager = $this->getObjectManager();
24 2
        $qb = $objectManager->createQueryBuilder($objectName);
25
        $qb
26 2
            ->remove()
27 2
            ->field($field)->lt($olderThan);
28
29 2
        $query = $qb->getQuery();
30 2
        $result = $query->execute();
31 2
        if (isset($result['n'])) {
32 2
            return $result['n'];
33
        }
34
35
        return 0;
36
    }
37
38
    /**
39
     * @param Run|Job|JobTiming $object
40
     * @param string            $action
41
     */
42 22
    protected function persist($object, $action = 'persist')
43
    {
44 22
        $objectManager = $this->getObjectManager();
45 22
        $objectManager->$action($object);
46 22
        $objectManager->flush();
47 22
    }
48
49
    /**
50
     * @return ObjectManager
51
     */
52
    abstract public function getObjectManager();
53
54
    /**
55
     * @param string $objectName
56
     */
57 1
    public function stopIdGenerator($objectName)
58
    {
59
        // Not needed for ODM
60 1
    }
61
62 1
    public function restoreIdGenerator($objectName)
63
    {
64
        // Not needed for ODM
65 1
    }
66
}
67