CommonTrait::restoreIdGenerator()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 1
dl 0
loc 10
ccs 8
cts 8
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Dtc\QueueBundle\ORM;
4
5
use Doctrine\Bundle\DoctrineBundle\Registry;
0 ignored issues
show
Bug introduced by
The type Doctrine\Bundle\DoctrineBundle\Registry 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\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...
7
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...
8
use 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...
9
use Dtc\QueueBundle\Entity\Job;
10
use Dtc\QueueBundle\Entity\JobTiming;
11
use Dtc\QueueBundle\Entity\Run;
12
use Dtc\QueueBundle\Util\Util;
13
14
trait CommonTrait
15
{
16
    protected $formerIdGenerators;
17
18
    /** @var string */
19
    protected $entityManagerName;
20
21
    /** @var Registry */
22
    protected $registry;
23
24
    protected $entityManagerReset = false;
25
26 2
    public function setEntityManagerName($name = 'default')
27
    {
28 2
        $this->entityManagerName = $name;
29 2
    }
30
31 2
    public function setRegistry(Registry $registry)
32
    {
33 2
        $this->registry = $registry;
34 2
    }
35
36
    /**
37
     * @param $currentObjectManager
38
     *
39
     * @return EntityManager
40
     */
41 34
    public function getObjectManagerReset()
42
    {
43 34
        $currentObjectManager = parent::getObjectManager();
44 34
        if (!$currentObjectManager->isOpen() && $this->registry) {
45 2
            $this->entityManagerReset = true;
46 2
            if (($currentObjectManager = $this->registry->getManager($this->entityManagerName)) && $currentObjectManager->isOpen()) {
47 1
                return $this->objectManager = $currentObjectManager;
0 ignored issues
show
Bug Best Practice introduced by
The property objectManager does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
48
            }
49
50 1
            return $this->objectManager = $this->registry->resetManager($this->entityManagerName);
51
        }
52
53 34
        return $currentObjectManager;
54
    }
55
56
    /**
57
     * @param string $objectName
58
     * @param string $field
59
     *
60
     * @return int
61
     */
62 1
    protected function removeOlderThan($objectName, $field, \DateTime $olderThan)
63
    {
64
        /** @var EntityManager $objectManager */
65 1
        $objectManager = $this->getObjectManager();
66 1
        $qb = $objectManager->createQueryBuilder()->delete($objectName, 'j');
67
        $qb = $qb
68 1
            ->where('j.'.$field.' < :'.$field)
69 1
            ->setParameter(':'.$field, $olderThan);
70
71 1
        $query = $qb->getQuery();
72
73 1
        return $query->execute();
74
    }
75
76 1
    public function stopIdGenerator($objectName)
77
    {
78
        /** @var EntityManager $objectManager */
79 1
        $objectManager = $this->getObjectManager();
80 1
        $repository = $objectManager->getRepository($objectName);
81
        /** @var ClassMetadata $metadata */
82 1
        $metadata = $objectManager->getClassMetadata($repository->getClassName());
83 1
        $this->formerIdGenerators[$objectName]['generator'] = $metadata->idGenerator;
84 1
        $this->formerIdGenerators[$objectName]['type'] = $metadata->generatorType;
85 1
        $metadata->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_NONE);
86 1
        $metadata->setIdGenerator(new AssignedGenerator());
87 1
    }
88
89 1
    public function restoreIdGenerator($objectName)
90
    {
91 1
        $objectManager = $this->getObjectManager();
92 1
        $repository = $objectManager->getRepository($objectName);
93
        /** @var ClassMetadata $metadata */
94 1
        $metadata = $objectManager->getClassMetadata($repository->getClassName());
95 1
        $generator = $this->formerIdGenerators[$objectName]['generator'];
96 1
        $type = $this->formerIdGenerators[$objectName]['type'];
97 1
        $metadata->setIdGeneratorType($type);
98 1
        $metadata->setIdGenerator($generator);
99 1
    }
100
101
    /**
102
     * @param Run|Job|JobTiming $object
103
     * @param string            $action
104
     */
105 23
    protected function persist($object, $action = 'persist')
106
    {
107
        /** @var EntityManager $objectManager */
108 23
        $objectManager = $this->getObjectManager();
109
110
        // If the entityManager gets reset somewhere midway, we may have to try to refetch the object we're persisting
111 23
        if ($this->entityManagerReset) {
112 2
            if ($object->getId() &&
113 2
                !$objectManager->getUnitOfWork()->tryGetById(['id' => $object->getId()], get_class($object))) {
114 2
                $newObject = $objectManager->find(get_class($object), $object->getId());
115 2
                if ($newObject) {
116 2
                    Util::copy($object, $newObject);
117 2
                    $object = $newObject;
118
                }
119
            }
120
        }
121
122 23
        $objectManager->$action($object);
123 23
        $objectManager->flush();
124 23
    }
125
126
    /**
127
     * @return ObjectManager
0 ignored issues
show
Bug introduced by
The type Dtc\QueueBundle\ORM\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...
128
     */
129
    abstract public function getObjectManager();
130
}
131