DtcQueueListener::processJob()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 5

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 5
eloc 16
c 1
b 1
f 0
nc 5
nop 1
dl 0
loc 25
ccs 17
cts 17
cp 1
crap 5
rs 9.4222
1
<?php
2
3
namespace Dtc\QueueBundle\Doctrine;
4
5
use Doctrine\ODM\MongoDB\DocumentManager;
0 ignored issues
show
Bug introduced by
The type Doctrine\ODM\MongoDB\DocumentManager 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...
6
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
0 ignored issues
show
Bug introduced by
The type Doctrine\ODM\MongoDB\Mapping\ClassMetadata 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...
7
use Doctrine\ORM\EntityManager;
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\EntityManager 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...
8
use Doctrine\ORM\Id\AssignedGenerator;
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\Id\AssignedGenerator 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...
9
use Dtc\QueueBundle\Model\Run;
10
use Dtc\QueueBundle\Model\StallableJob;
11
use Dtc\QueueBundle\Util\Util;
12
13
class DtcQueueListener
14
{
15
    private $jobArchiveClass;
16
    private $runArchiveClass;
17
    private $entityManagerName;
18
    private $objectManager;
19
    private $registry;
20
    private $localeFix;
21
22 18
    public function __construct($jobArchiveClass, $runArchiveClass, $localeFix)
23
    {
24 18
        $this->jobArchiveClass = $jobArchiveClass;
25 18
        $this->runArchiveClass = $runArchiveClass;
26 18
        $this->localeFix = $localeFix;
27 18
    }
28
29
    public function setRegistry($registry)
30
    {
31
        $this->registry = $registry;
32
    }
33
34
    public function setEntityManagerName($entityManagerName)
35
    {
36
        $this->entityManagerName = $entityManagerName;
37
    }
38
39
    /**
40
     * @param \Doctrine\Persistence\Event\LifecycleEventArgs $eventArgs
0 ignored issues
show
Bug introduced by
The type Doctrine\Persistence\Event\LifecycleEventArgs 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...
41
     */
42 38
    public function preRemove($eventArgs)
43
    {
44 38
        $object = $eventArgs->getObject();
45 38
        $objectManager = $eventArgs->getObjectManager();
46 38
        $this->objectManager = $objectManager;
47
48 38
        if ($object instanceof \Dtc\QueueBundle\Model\Job) {
49 35
            $this->processJob($object);
50 9
        } elseif ($object instanceof Run) {
51 9
            $this->processRun($object);
52
        }
53 38
    }
54
55 38
    protected function getObjectManager()
56
    {
57 38
        if (!$this->registry) {
58 38
            return $this->objectManager;
59
        }
60
61
        if ($this->objectManager instanceof EntityManager && !$this->objectManager->isOpen()) {
62
            $this->objectManager = $this->registry->getManager($this->entityManagerName);
63
            if (!$this->objectManager->isOpen()) {
64
                $this->objectManager = $this->registry->resetManager($this->entityManagerName);
65
            }
66
        }
67
68
        return $this->objectManager;
69
    }
70
71 9
    public function processRun(Run $object)
72
    {
73 9
        $runArchiveClass = $this->runArchiveClass;
74 9
        if ($object instanceof $runArchiveClass) {
75
            return;
76
        }
77
78 9
        $objectManager = $this->getObjectManager();
79
80 9
        $repository = $objectManager->getRepository($runArchiveClass);
81 9
        $newArchive = false;
82
83 9
        if (!$runArchive = $repository->find($object->getId())) {
84 9
            $runArchive = new $runArchiveClass();
85 9
            $newArchive = true;
86
        }
87
88 9
        Util::copy($object, $runArchive);
89 9
        if ($newArchive) {
90 9
            $metadata = $objectManager->getClassMetadata($runArchiveClass);
91 9
            $this->adjustIdGenerator($metadata);
92
        }
93
94 9
        $objectManager->persist($runArchive);
95 9
    }
96
97
    /**
98
     * @param $metadata
99
     */
100 38
    protected function adjustIdGenerator($metadata)
101
    {
102 38
        $objectManager = $this->getObjectManager();
103 38
        if ($objectManager instanceof EntityManager && $metadata instanceof \Doctrine\ORM\Mapping\ClassMetadata) {
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\Mapping\ClassMetadata 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...
104 18
            $metadata->setIdGeneratorType(\Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_NONE);
105 18
            $metadata->setIdGenerator(new AssignedGenerator());
106 20
        } elseif ($objectManager instanceof DocumentManager && $metadata instanceof ClassMetadata) {
107 20
            $metadata->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_NONE);
108
        }
109 38
    }
110
111 35
    public function processJob(\Dtc\QueueBundle\Model\Job $object)
112
    {
113 35
        if ($object instanceof \Dtc\QueueBundle\Document\Job ||
114 35
            $object instanceof \Dtc\QueueBundle\Entity\Job) {
115
            /** @var JobManager $jobManager */
116 35
            $archiveObjectName = $this->jobArchiveClass;
117 35
            $objectManager = $this->getObjectManager();
118 35
            $repository = $objectManager->getRepository($archiveObjectName);
119 35
            $className = $repository->getClassName();
120
121
            /** @var StallableJob $jobArchive */
122 35
            $newArchive = false;
123 35
            if (!$jobArchive = $repository->find($object->getId())) {
124 35
                $jobArchive = new $className();
125 35
                $newArchive = true;
126
            }
127
128 35
            if ($newArchive) {
129 35
                $metadata = $objectManager->getClassMetadata($className);
130 35
                $this->adjustIdGenerator($metadata);
131
            }
132
133 35
            Util::copy($object, $jobArchive);
134 35
            $jobArchive->setUpdatedAt(Util::getMicrotimeDateTime());
135 35
            $objectManager->persist($jobArchive);
136
        }
137 35
    }
138
139
    /**
140
     * @param \Doctrine\Persistence\Event\LifecycleEventArgs $eventArgs
141
     */
142 20
    public function preUpdate($eventArgs)
143
    {
144 20
        $object = $eventArgs->getObject();
145 20
        if ($object instanceof \Dtc\QueueBundle\Model\StallableJob) {
146 20
            $dateTime = \Dtc\QueueBundle\Util\Util::getMicrotimeDateTime();
147 20
            $object->setUpdatedAt($dateTime);
148
        }
149 20
        if ($this->localeFix && method_exists($object, 'getElapsed') && $object->getElapsed()) {
150
            $localeinfo = localeconv();
151
            if (isset($localeinfo['decimal_point']) && $localeinfo['decimal_point'] && '.' !== $localeinfo['decimal_point']) {
152
                $object->setElapsed(number_format($object->getElapsed(), 16));
153
            }
154
        }
155 20
    }
156
157
    /**
158
     * @param \Doctrine\Persistence\Event\LifecycleEventArgs $eventArgs
159
     */
160 48
    public function prePersist($eventArgs)
161
    {
162 48
        $object = $eventArgs->getObject();
163
164 48
        if ($object instanceof \Dtc\QueueBundle\Model\StallableJob) {
165 43
            $dateTime = \Dtc\QueueBundle\Util\Util::getMicrotimeDateTime();
166 43
            if (!$object->getCreatedAt()) {
167
                $object->setCreatedAt($dateTime);
168
            }
169 43
            $object->setUpdatedAt($dateTime);
170
        }
171 48
        if ($this->localeFix && method_exists($object, 'getElapsed') && $object->getElapsed()) {
172
            $localeinfo = localeconv();
173
            if (isset($localeinfo['decimal_point']) && $localeinfo['decimal_point'] && '.' !== $localeinfo['decimal_point']) {
174
                $object->setElapsed(number_format($object->getElapsed(), 16));
175
            }
176
        }
177 48
    }
178
}
179