Completed
Push — master ( 7fae80...8ffdf8 )
by Matthew
07:25
created

CommonTrait::persist()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 12
cts 12
cp 1
rs 8.8571
c 0
b 0
f 0
nc 4
cc 5
eloc 11
nop 2
crap 5
1
<?php
2
3
namespace Dtc\QueueBundle\ORM;
4
5
use Doctrine\Bundle\DoctrineBundle\Registry;
6
use Doctrine\Common\Persistence\ObjectManager;
7
use Doctrine\ORM\EntityManager;
8
use Doctrine\ORM\Id\AssignedGenerator;
9
use Doctrine\ORM\Mapping\ClassMetadata;
10
use Dtc\QueueBundle\Entity\Job;
11
use Dtc\QueueBundle\Entity\JobTiming;
12
use Dtc\QueueBundle\Entity\Run;
13
use Dtc\QueueBundle\Util\Util;
14
15
trait CommonTrait
16
{
17
    protected $formerIdGenerators;
18
19
    /** @var string */
20
    protected $entityManagerName;
21
22
    /** @var Registry */
23
    protected $registry;
24
25
    protected $entityManagerReset = false;
26
27 2
    public function setEntityManagerName($name = 'default')
28
    {
29 2
        $this->entityManagerName = $name;
30 2
    }
31
32 2
    public function setRegistry(Registry $registry)
33
    {
34 2
        $this->registry = $registry;
35 2
    }
36
37
    /**
38
     * @param $currentObjectManager
39
     *
40
     * @return EntityManager
41
     */
42 34
    public function getObjectManagerReset()
43
    {
44 34
        $currentObjectManager = parent::getObjectManager();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getObjectManager() instead of getObjectManagerReset()). Are you sure this is correct? If so, you might want to change this to $this->getObjectManager().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
45 34
        if (!$currentObjectManager->isOpen() && $this->registry) {
46 2
            $this->entityManagerReset = true;
47 2
            if (($currentObjectManager = $this->registry->getManager($this->entityManagerName)) && $currentObjectManager->isOpen()) {
48 1
                return $this->objectManager = $currentObjectManager;
0 ignored issues
show
Bug introduced by
The property objectManager does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
49
            }
50
51 1
            return $this->objectManager = $this->registry->resetManager($this->entityManagerName);
52
        }
53
54 34
        return $currentObjectManager;
55
    }
56
57
    /**
58
     * @param string    $objectName
59
     * @param string    $field
60
     * @param \DateTime $olderThan
61
     *
62
     * @return int
63
     */
64 1
    protected function removeOlderThan($objectName, $field, \DateTime $olderThan)
65
    {
66
        /** @var EntityManager $objectManager */
67 1
        $objectManager = $this->getObjectManager();
68 1
        $qb = $objectManager->createQueryBuilder()->delete($objectName, 'j');
69
        $qb = $qb
70 1
            ->where('j.'.$field.' < :'.$field)
71 1
            ->setParameter(':'.$field, $olderThan);
72
73 1
        $query = $qb->getQuery();
74
75 1
        return $query->execute();
76
    }
77
78 1
    public function stopIdGenerator($objectName)
79
    {
80
        /** @var EntityManager $objectManager */
81 1
        $objectManager = $this->getObjectManager();
82 1
        $repository = $objectManager->getRepository($objectName);
83
        /** @var ClassMetadata $metadata */
84 1
        $metadata = $objectManager->getClassMetadata($repository->getClassName());
85 1
        $this->formerIdGenerators[$objectName]['generator'] = $metadata->idGenerator;
86 1
        $this->formerIdGenerators[$objectName]['type'] = $metadata->generatorType;
87 1
        $metadata->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_NONE);
88 1
        $metadata->setIdGenerator(new AssignedGenerator());
89 1
    }
90
91 1
    public function restoreIdGenerator($objectName)
92
    {
93 1
        $objectManager = $this->getObjectManager();
94 1
        $repository = $objectManager->getRepository($objectName);
95
        /** @var ClassMetadata $metadata */
96 1
        $metadata = $objectManager->getClassMetadata($repository->getClassName());
97 1
        $generator = $this->formerIdGenerators[$objectName]['generator'];
98 1
        $type = $this->formerIdGenerators[$objectName]['type'];
99 1
        $metadata->setIdGeneratorType($type);
100 1
        $metadata->setIdGenerator($generator);
101 1
    }
102
103
    /**
104
     * @param Run|Job|JobTiming $object
105
     * @param string            $action
106
     */
107 23
    protected function persist($object, $action = 'persist')
108
    {
109
        /** @var EntityManager $objectManager */
110 23
        $objectManager = $this->getObjectManager();
111
112
        // If the entityManager gets reset somewhere midway, we may have to try to refetch the object we're persisting
113 23
        if ($this->entityManagerReset) {
114 2
            if ($object->getId() &&
115 2
                !$objectManager->getUnitOfWork()->tryGetById(['id' => $object->getId()], get_class($object))) {
116 2
                $newObject = $objectManager->find(get_class($object), $object->getId());
117 2
                if ($newObject) {
118 2
                    Util::copy($object, $newObject);
119 2
                    $object = $newObject;
120
                }
121
            }
122
        }
123
124 23
        $objectManager->$action($object);
125 23
        $objectManager->flush();
126 23
    }
127
128
    /**
129
     * @return ObjectManager
130
     */
131
    abstract public function getObjectManager();
132
}
133