Passed
Pull Request — master (#57)
by Matthew
08:25
created

CommonTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 94.44%

Importance

Changes 0
Metric Value
wmc 5
dl 0
loc 53
ccs 17
cts 18
cp 0.9444
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A removeOlderThan() 0 16 2
A persist() 0 5 1
A restoreIdGenerator() 0 2 1
A stopIdGenerator() 0 2 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