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
|
|
|
return $result->getDeletedCount(); |
33
|
2 |
|
} elseif (isset($result['n'])) { |
34
|
2 |
|
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
|
|
|
|