Passed
Push — master ( 4d8734...991786 )
by Matthew
09:35 queued 06:44
created

DtcQueueListener::processRun()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4.0047

Importance

Changes 0
Metric Value
cc 4
eloc 14
nc 5
nop 1
dl 0
loc 24
ccs 14
cts 15
cp 0.9333
crap 4.0047
rs 9.7998
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
23 18
    public function __construct($jobArchiveClass, $runArchiveClass)
24
    {
25 18
        $this->jobArchiveClass = $jobArchiveClass;
26 18
        $this->runArchiveClass = $runArchiveClass;
27 18
    }
28
29
    public function setRegistry(RegistryInterface $registry)
30
    {
31
        $this->registry = $registry;
32
    }
33
34
    public function setEntityManagerName($entityManagerName)
35
    {
36
        $this->entityManagerName = $entityManagerName;
37
    }
38
39 36
    public function preRemove(LifecycleEventArgs $eventArgs)
40
    {
41 36
        $object = $eventArgs->getObject();
42 36
        $objectManager = $eventArgs->getObjectManager();
43 36
        $this->objectManager = $objectManager;
44
45 36
        if ($object instanceof \Dtc\QueueBundle\Model\Job) {
46 33
            $this->processJob($object);
47 9
        } elseif ($object instanceof Run) {
48 9
            $this->processRun($object);
49
        }
50 36
    }
51
52 36
    protected function getObjectManager()
53
    {
54 36
        if (!$this->registry) {
55 36
            return $this->objectManager;
56
        }
57
58
        if ($this->objectManager instanceof EntityManager && !$this->objectManager->isOpen()) {
59
            $this->objectManager = $this->registry->getManager($this->entityManagerName);
60
            if (!$this->objectManager->isOpen()) {
61
                $this->objectManager = $this->registry->resetManager($this->entityManagerName);
62
            }
63
        }
64
65
        return $this->objectManager;
66
    }
67
68 9
    public function processRun(Run $object)
69
    {
70 9
        $runArchiveClass = $this->runArchiveClass;
71 9
        if ($object instanceof $runArchiveClass) {
72
            return;
73
        }
74
75 9
        $objectManager = $this->getObjectManager();
76
77 9
        $repository = $objectManager->getRepository($runArchiveClass);
78 9
        $newArchive = false;
79
80 9
        if (!$runArchive = $repository->find($object->getId())) {
81 9
            $runArchive = new $runArchiveClass();
82 9
            $newArchive = true;
83
        }
84
85 9
        Util::copy($object, $runArchive);
86 9
        if ($newArchive) {
87 9
            $metadata = $objectManager->getClassMetadata($runArchiveClass);
88 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

88
            $this->/** @scrutinizer ignore-call */ 
89
                   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...
89
        }
90
91 9
        $objectManager->persist($runArchive);
92 9
    }
93
94
    /**
95
     * @param $metadata
96
     */
97 36
    protected function adjustIdGenerator(\Doctrine\Common\Persistence\Mapping\ClassMetadata $metadata)
98
    {
99 36
        $objectManager = $this->getObjectManager();
100 36
        if ($objectManager instanceof EntityManager && $metadata instanceof \Doctrine\ORM\Mapping\ClassMetadata) {
101 17
            $metadata->setIdGeneratorType(\Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_NONE);
102 17
            $metadata->setIdGenerator(new AssignedGenerator());
103 19
        } elseif ($objectManager instanceof DocumentManager && $metadata instanceof ClassMetadata) {
104 19
            $metadata->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_NONE);
105
        }
106 36
    }
107
108 33
    public function processJob(\Dtc\QueueBundle\Model\Job $object)
109
    {
110 33
        if ($object instanceof \Dtc\QueueBundle\Document\Job ||
111 33
            $object instanceof \Dtc\QueueBundle\Entity\Job) {
112
            /** @var JobManager $jobManager */
113 33
            $archiveObjectName = $this->jobArchiveClass;
114 33
            $objectManager = $this->getObjectManager();
115 33
            $repository = $objectManager->getRepository($archiveObjectName);
116 33
            $className = $repository->getClassName();
117
118
            /** @var StallableJob $jobArchive */
119 33
            $newArchive = false;
120 33
            if (!$jobArchive = $repository->find($object->getId())) {
121 33
                $jobArchive = new $className();
122 33
                $newArchive = true;
123
            }
124
125 33
            if ($newArchive) {
126 33
                $metadata = $objectManager->getClassMetadata($className);
127 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

127
                $this->/** @scrutinizer ignore-call */ 
128
                       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...
128
            }
129
130 33
            Util::copy($object, $jobArchive);
131 33
            $jobArchive->setUpdatedAt(Util::getMicrotimeDateTime());
132 33
            $objectManager->persist($jobArchive);
133
        }
134 33
    }
135
136 20
    public function preUpdate(LifecycleEventArgs $eventArgs)
137
    {
138 20
        $object = $eventArgs->getObject();
139 20
        if ($object instanceof \Dtc\QueueBundle\Model\StallableJob) {
140 20
            $dateTime = \Dtc\QueueBundle\Util\Util::getMicrotimeDateTime();
141 20
            $object->setUpdatedAt($dateTime);
142
        }
143 20
    }
144
145 48
    public function prePersist(LifecycleEventArgs $eventArgs)
146
    {
147 48
        $object = $eventArgs->getObject();
148
149 48
        if ($object instanceof \Dtc\QueueBundle\Model\StallableJob) {
150 43
            $dateTime = \Dtc\QueueBundle\Util\Util::getMicrotimeDateTime();
151 43
            if (!$object->getCreatedAt()) {
152
                $object->setCreatedAt($dateTime);
153
            }
154 43
            $object->setUpdatedAt($dateTime);
155
        }
156 48
    }
157
}
158