Passed
Push — master ( 82017d...f35517 )
by Matthew
08:19
created

CommonTrait::restoreIdGenerator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 2
rs 10
c 0
b 0
f 0
ccs 1
cts 1
cp 1
cc 1
eloc 0
nc 1
nop 1
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
use MongoDB\DeleteResult;
11
12
trait CommonTrait
13
{
14
    /**
15
     * @param string $objectName
16
     * @param string $field
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 ($result instanceof DeleteResult) {
32 2
            return $result->getDeletedCount();
33
        } elseif (isset($result['n'])) {
34
            return $result['n'];
35
        }
36
37
        return 0;
38
    }
39
40
    /**
41
     * @param Run|Job|JobTiming $object
42
     * @param string            $action
43
     */
44 22
    protected function persist($object, $action = 'persist')
45
    {
46 22
        $objectManager = $this->getObjectManager();
47 22
        $objectManager->$action($object);
48 22
        $objectManager->flush();
49 22
    }
50
51
    /**
52
     * @return ObjectManager
53
     */
54
    abstract public function getObjectManager();
55
56
    /**
57
     * @param string $objectName
58
     */
59 1
    public function stopIdGenerator($objectName)
60
    {
61
        // Not needed for ODM
62 1
    }
63
64 1
    public function restoreIdGenerator($objectName)
65
    {
66
        // Not needed for ODM
67 1
    }
68
}
69