Passed
Push — master ( 26ea1a...43abdf )
by Matthew
07:12
created

CommonTrait::removeOlderThan()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.1406

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 12
c 1
b 0
f 0
nc 3
nop 3
dl 0
loc 19
ccs 9
cts 12
cp 0.75
crap 3.1406
rs 9.8666
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
     * @param \DateTime $olderThan
18
     *
19
     * @return int
20
     */
21 2
    protected function removeOlderThan($objectName, $field, \DateTime $olderThan)
22
    {
23
        /** @var DocumentManager $objectManager */
24 2
        $objectManager = $this->getObjectManager();
25 2
        $qb = $objectManager->createQueryBuilder($objectName);
26
        $qb
27 2
            ->remove()
28 2
            ->field($field)->lt($olderThan);
29
30 2
        $query = $qb->getQuery();
31 2
        $result = $query->execute();
32 2
        if ($result instanceof DeleteResult) {
33 2
            return $result->getDeletedCount();
34
        }
35
        else if (isset($result['n'])) {
36
            return $result['n'];
37
        }
38
39
        return 0;
40
    }
41
42
    /**
43
     * @param Run|Job|JobTiming $object
44
     * @param string            $action
45
     */
46 22
    protected function persist($object, $action = 'persist')
47
    {
48 22
        $objectManager = $this->getObjectManager();
49 22
        $objectManager->$action($object);
50 22
        $objectManager->flush();
51 22
    }
52
53
    /**
54
     * @return ObjectManager
55
     */
56
    abstract public function getObjectManager();
57
58
    /**
59
     * @param string $objectName
60
     */
61 1
    public function stopIdGenerator($objectName)
62
    {
63
        // Not needed for ODM
64 1
    }
65
66 1
    public function restoreIdGenerator($objectName)
67
    {
68
        // Not needed for ODM
69 1
    }
70
}
71