DoctrineRunManager::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Dtc\QueueBundle\Doctrine;
4
5
use Dtc\QueueBundle\Manager\RunManager;
6
use Dtc\QueueBundle\Model\Run;
7
8
abstract class DoctrineRunManager extends RunManager
9
{
10
    /** @var \Doctrine\Persistence\ObjectManager */
0 ignored issues
show
Bug introduced by
The type Doctrine\Persistence\ObjectManager was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
    protected $objectManager;
12
13
    /** @var string|null */
14
    protected $runArchiveClass;
15
16 18
    public function __construct($objectManager, $runClass, $runArchiveClass)
17
    {
18 18
        $this->runArchiveClass = $runArchiveClass;
19 18
        $this->objectManager = $objectManager;
20 18
        parent::__construct($runClass);
21 18
    }
22
23
    /**
24
     * @return \Doctrine\Persistence\ObjectManager
25
     */
26 20
    public function getObjectManager()
27
    {
28 20
        return $this->objectManager;
29
    }
30
31
    /**
32
     * @return \Doctrine\Persistence\ObjectRepository
0 ignored issues
show
Bug introduced by
The type Doctrine\Persistence\ObjectRepository was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
33
     */
34
    public function getRepository()
35
    {
36
        return $this->getObjectManager()->getRepository($this->getRunClass());
37
    }
38
39
    /**
40
     * @return string|null
41
     */
42 21
    public function getRunArchiveClass()
43
    {
44 21
        return $this->runArchiveClass;
45
    }
46
47
    /**
48
     * @return int
49
     *
50
     * @throws \Exception
51
     */
52 2
    public function pruneStalledRuns()
53
    {
54 2
        $runs = $this->getOldLiveRuns();
55
        /** @var Run $run */
56 2
        $delete = [];
57
58 2
        foreach ($runs as $run) {
59 2
            $lastHeartbeat = $run->getLastHeartbeatAt();
60 2
            $time = time() - 3600;
61 2
            $processTimeout = $run->getProcessTimeout();
62 2
            $time -= $processTimeout;
63 2
            $oldDate = \DateTime::createFromFormat('U', strval($time));
64 2
            if (false === $oldDate) {
65
                throw new \Exception("Could not create DateTime object from $time");
66
            }
67 2
            $oldDate->setTimezone(new \DateTimeZone(date_default_timezone_get()));
68 2
            if (null === $lastHeartbeat || $oldDate > $lastHeartbeat) {
69 2
                $delete[] = $run;
70
            }
71
        }
72
73 2
        return $this->deleteOldRuns($delete);
74
    }
75
76
    /**
77
     * @return int
78
     */
79 2
    protected function deleteOldRuns(array $delete)
80
    {
81 2
        $count = count($delete);
82 2
        $objectManager = $this->getObjectManager();
83 2
        for ($i = 0; $i < $count; $i += 100) {
84 2
            $deleteList = array_slice($delete, $i, 100);
85 2
            foreach ($deleteList as $object) {
86 2
                $objectManager->remove($object);
87
            }
88 2
            $this->flush();
89
        }
90
91 2
        return $count;
92
    }
93
94 2
    protected function flush()
95
    {
96 2
        $this->getObjectManager()->flush();
97 2
    }
98
99
    /**
100
     * @param string $action
101
     */
102 4
    protected function persistRun(Run $run, $action = 'persist')
103
    {
104 4
        parent::persistRun($run, $action);
105 4
        $this->persist($run, $action);
106 4
    }
107
108
    /**
109
     * @return array
110
     */
111
    abstract protected function getOldLiveRuns();
112
113
    abstract protected function persist($object, $action = 'persist');
114
115
    abstract protected function removeOlderThan($objectName, $field, \DateTime $olderThan);
116
117 1
    public function pruneArchivedRuns(\DateTime $olderThan)
118
    {
119 1
        return $this->removeOlderThan($this->getRunArchiveClass(), 'endedAt', $olderThan);
120
    }
121
}
122