Passed
Push — master ( fa4594...c101ff )
by Matthew
07:38
created

DtcQueueListener::prePersist()   B

Complexity

Conditions 9
Paths 9

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 12.8936

Importance

Changes 0
Metric Value
cc 9
eloc 10
nc 9
nop 1
dl 0
loc 15
ccs 7
cts 11
cp 0.6364
crap 12.8936
rs 8.0555
c 0
b 0
f 0
1
<?php
2
3
namespace Dtc\QueueBundle\Doctrine;
4
5
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
6
use Doctrine\ODM\MongoDB\DocumentManager;
7
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
8
use Doctrine\ORM\EntityManager;
9
use Doctrine\ORM\Id\AssignedGenerator;
10
use Dtc\QueueBundle\Model\StallableJob;
11
use Dtc\QueueBundle\Model\Run;
12
use Dtc\QueueBundle\Util\Util;
13
use Symfony\Bridge\Doctrine\RegistryInterface;
14
15
class DtcQueueListener
16
{
17
    private $jobArchiveClass;
18
    private $runArchiveClass;
19
    private $entityManagerName;
20
    private $objectManager;
21
    private $registry;
22
    private $localeFix;
23
24 18
    public function __construct($jobArchiveClass, $runArchiveClass, $localeFix)
25
    {
26 18
        $this->jobArchiveClass = $jobArchiveClass;
27 18
        $this->runArchiveClass = $runArchiveClass;
28 18
        $this->localeFix = $localeFix;
29 18
    }
30
31
    public function setRegistry(RegistryInterface $registry)
32
    {
33
        $this->registry = $registry;
34
    }
35
36
    public function setEntityManagerName($entityManagerName)
37
    {
38
        $this->entityManagerName = $entityManagerName;
39
    }
40
41 36
    public function preRemove(LifecycleEventArgs $eventArgs)
42
    {
43 36
        $object = $eventArgs->getObject();
44 36
        $objectManager = $eventArgs->getObjectManager();
45 36
        $this->objectManager = $objectManager;
46
47 36
        if ($object instanceof \Dtc\QueueBundle\Model\Job) {
48 33
            $this->processJob($object);
49 9
        } elseif ($object instanceof Run) {
50 9
            $this->processRun($object);
51
        }
52 36
    }
53
54 36
    protected function getObjectManager()
55
    {
56 36
        if (!$this->registry) {
57 36
            return $this->objectManager;
58
        }
59
60
        if ($this->objectManager instanceof EntityManager && !$this->objectManager->isOpen()) {
61
            $this->objectManager = $this->registry->getManager($this->entityManagerName);
62
            if (!$this->objectManager->isOpen()) {
63
                $this->objectManager = $this->registry->resetManager($this->entityManagerName);
64
            }
65
        }
66
67
        return $this->objectManager;
68
    }
69
70 9
    public function processRun(Run $object)
71
    {
72 9
        $runArchiveClass = $this->runArchiveClass;
73 9
        if ($object instanceof $runArchiveClass) {
74
            return;
75
        }
76
77 9
        $objectManager = $this->getObjectManager();
78
79 9
        $repository = $objectManager->getRepository($runArchiveClass);
80 9
        $newArchive = false;
81
82 9
        if (!$runArchive = $repository->find($object->getId())) {
83 9
            $runArchive = new $runArchiveClass();
84 9
            $newArchive = true;
85
        }
86
87 9
        Util::copy($object, $runArchive);
88 9
        if ($newArchive) {
89 9
            $metadata = $objectManager->getClassMetadata($runArchiveClass);
90 9
            $this->adjustIdGenerator($metadata, $objectManager);
0 ignored issues
show
Unused Code introduced by
The call to Dtc\QueueBundle\Doctrine...er::adjustIdGenerator() has too many arguments starting with $objectManager. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

90
            $this->/** @scrutinizer ignore-call */ 
91
                   adjustIdGenerator($metadata, $objectManager);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
91
        }
92
93 9
        $objectManager->persist($runArchive);
94 9
    }
95
96
    /**
97
     * @param $metadata
98
     */
99 36
    protected function adjustIdGenerator(\Doctrine\Common\Persistence\Mapping\ClassMetadata $metadata)
100
    {
101 36
        $objectManager = $this->getObjectManager();
102 36
        if ($objectManager instanceof EntityManager && $metadata instanceof \Doctrine\ORM\Mapping\ClassMetadata) {
103 17
            $metadata->setIdGeneratorType(\Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_NONE);
104 17
            $metadata->setIdGenerator(new AssignedGenerator());
105 19
        } elseif ($objectManager instanceof DocumentManager && $metadata instanceof ClassMetadata) {
106 19
            $metadata->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_NONE);
107
        }
108 36
    }
109
110 33
    public function processJob(\Dtc\QueueBundle\Model\Job $object)
111
    {
112 33
        if ($object instanceof \Dtc\QueueBundle\Document\Job ||
113 33
            $object instanceof \Dtc\QueueBundle\Entity\Job) {
114
            /** @var JobManager $jobManager */
115 33
            $archiveObjectName = $this->jobArchiveClass;
116 33
            $objectManager = $this->getObjectManager();
117 33
            $repository = $objectManager->getRepository($archiveObjectName);
118 33
            $className = $repository->getClassName();
119
120
            /** @var StallableJob $jobArchive */
121 33
            $newArchive = false;
122 33
            if (!$jobArchive = $repository->find($object->getId())) {
123 33
                $jobArchive = new $className();
124 33
                $newArchive = true;
125
            }
126
127 33
            if ($newArchive) {
128 33
                $metadata = $objectManager->getClassMetadata($className);
129 33
                $this->adjustIdGenerator($metadata, $objectManager);
0 ignored issues
show
Unused Code introduced by
The call to Dtc\QueueBundle\Doctrine...er::adjustIdGenerator() has too many arguments starting with $objectManager. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

129
                $this->/** @scrutinizer ignore-call */ 
130
                       adjustIdGenerator($metadata, $objectManager);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
130
            }
131
132 33
            Util::copy($object, $jobArchive);
133 33
            $jobArchive->setUpdatedAt(Util::getMicrotimeDateTime());
134 33
            $objectManager->persist($jobArchive);
135
        }
136 33
    }
137
138 20
    public function preUpdate(LifecycleEventArgs $eventArgs)
139
    {
140 20
        $object = $eventArgs->getObject();
141 20
        if ($object instanceof \Dtc\QueueBundle\Model\StallableJob) {
142 20
            $dateTime = \Dtc\QueueBundle\Util\Util::getMicrotimeDateTime();
143 20
            $object->setUpdatedAt($dateTime);
144
        }
145 20
        if ($this->localeFix && method_exists($object, 'getElapsed') && $object->getElapsed()) {
146
            $localeinfo = localeconv();
147
            if (isset($localeinfo['decimal_point']) && $localeinfo['decimal_point'] && '.' !== $localeinfo['decimal_point']) {
148
                $object->setElapsed(number_format($object->getElapsed(), 16));
149
            }
150
        }
151 20
    }
152
153 48
    public function prePersist(LifecycleEventArgs $eventArgs)
154
    {
155 48
        $object = $eventArgs->getObject();
156
157 48
        if ($object instanceof \Dtc\QueueBundle\Model\StallableJob) {
158 43
            $dateTime = \Dtc\QueueBundle\Util\Util::getMicrotimeDateTime();
159 43
            if (!$object->getCreatedAt()) {
160
                $object->setCreatedAt($dateTime);
161
            }
162 43
            $object->setUpdatedAt($dateTime);
163
        }
164 48
        if ($this->localeFix && method_exists($object, 'getElapsed') && $object->getElapsed()) {
165
            $localeinfo = localeconv();
166
            if (isset($localeinfo['decimal_point']) && $localeinfo['decimal_point'] && '.' !== $localeinfo['decimal_point']) {
167
                $object->setElapsed(number_format($object->getElapsed(), 16));
168
            }
169
        }
170 48
    }
171
}
172