Completed
Push — master ( 31d53b...10a888 )
by Matthew
75:43 queued 71:47
created

DoctrineRunManager   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Test Coverage

Coverage 93.18%

Importance

Changes 0
Metric Value
eloc 35
c 0
b 0
f 0
dl 0
loc 112
ccs 41
cts 44
cp 0.9318
rs 10
wmc 15

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getRunArchiveClass() 0 3 1
A getRepository() 0 3 1
A getObjectManager() 0 3 1
A __construct() 0 5 1
A persistRun() 0 4 1
A pruneStalledRuns() 0 22 5
A flush() 0 3 1
A pruneArchivedRuns() 0 3 1
A deleteOldRuns() 0 13 3
1
<?php
2
3
namespace Dtc\QueueBundle\Doctrine;
4
5
use Doctrine\Common\Persistence\ObjectManager;
6
use Doctrine\Common\Persistence\ObjectRepository;
7
use Dtc\QueueBundle\Manager\RunManager;
8
use Dtc\QueueBundle\Model\Run;
9
10
abstract class DoctrineRunManager extends RunManager
11
{
12
    /** @var ObjectManager */
13
    protected $objectManager;
14
15
    /** @var string|null */
16
    protected $runArchiveClass;
17
18 18
    public function __construct(ObjectManager $objectManager, $runClass, $runArchiveClass)
19
    {
20 18
        $this->runArchiveClass = $runArchiveClass;
21 18
        $this->objectManager = $objectManager;
22 18
        parent::__construct($runClass);
23 18
    }
24
25
    /**
26
     * @return ObjectManager
27
     */
28 20
    public function getObjectManager()
29
    {
30 20
        return $this->objectManager;
31
    }
32
33
    /**
34
     * @return ObjectRepository
35
     */
36
    public function getRepository()
37
    {
38
        return $this->getObjectManager()->getRepository($this->getRunClass());
39
    }
40
41
    /**
42
     * @return string|null
43
     */
44 21
    public function getRunArchiveClass()
45
    {
46 21
        return $this->runArchiveClass;
47
    }
48
49
    /**
50
     * @return int
51
     *
52
     * @throws \Exception
53
     */
54 2
    public function pruneStalledRuns()
55
    {
56 2
        $runs = $this->getOldLiveRuns();
57
        /** @var Run $run */
58 2
        $delete = [];
59
60 2
        foreach ($runs as $run) {
61 2
            $lastHeartbeat = $run->getLastHeartbeatAt();
62 2
            $time = time() - 3600;
63 2
            $processTimeout = $run->getProcessTimeout();
64 2
            $time -= $processTimeout;
65 2
            $oldDate = \DateTime::createFromFormat('U', strval($time));
66 2
            if (false === $oldDate) {
67
                throw new \Exception("Could not create DateTime object from $time");
68
            }
69 2
            $oldDate->setTimezone(new \DateTimeZone(date_default_timezone_get()));
70 2
            if (null === $lastHeartbeat || $oldDate > $lastHeartbeat) {
71 2
                $delete[] = $run;
72
            }
73
        }
74
75 2
        return $this->deleteOldRuns($delete);
76
    }
77
78
    /**
79
     * @return int
80
     */
81 2
    protected function deleteOldRuns(array $delete)
82
    {
83 2
        $count = count($delete);
84 2
        $objectManager = $this->getObjectManager();
85 2
        for ($i = 0; $i < $count; $i += 100) {
86 2
            $deleteList = array_slice($delete, $i, 100);
87 2
            foreach ($deleteList as $object) {
88 2
                $objectManager->remove($object);
89
            }
90 2
            $this->flush();
91
        }
92
93 2
        return $count;
94
    }
95
96 2
    protected function flush()
97
    {
98 2
        $this->getObjectManager()->flush();
99 2
    }
100
101
    /**
102
     * @param string $action
103
     */
104 4
    protected function persistRun(Run $run, $action = 'persist')
105
    {
106 4
        parent::persistRun($run, $action);
107 4
        $this->persist($run, $action);
108 4
    }
109
110
    /**
111
     * @return array
112
     */
113
    abstract protected function getOldLiveRuns();
114
115
    abstract protected function persist($object, $action = 'persist');
116
117
    abstract protected function removeOlderThan($objectName, $field, \DateTime $olderThan);
118
119 1
    public function pruneArchivedRuns(\DateTime $olderThan)
120
    {
121 1
        return $this->removeOlderThan($this->getRunArchiveClass(), 'endedAt', $olderThan);
122
    }
123
}
124