objectmanager   A
last analyzed

Complexity

Total Complexity 29

Size/Duplication

Total Lines 224
Duplicated Lines 0 %

Test Coverage

Coverage 96.21%

Importance

Changes 11
Bugs 0 Features 0
Metric Value
eloc 123
c 11
b 0
f 0
dl 0
loc 224
ccs 127
cts 132
cp 0.9621
rs 10
wmc 29

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A update() 0 14 1
A delete() 0 23 2
A copy_metadata() 0 20 5
A approve() 0 13 1
A unapprove() 0 13 1
A copy_associations() 0 4 2
A unlock() 0 10 1
A new_instance() 0 16 3
A create() 0 20 4
A undelete() 0 8 1
A lock() 0 13 1
A kill_potential_proxies() 0 16 5
A purge() 0 12 1
1
<?php
2
/**
3
 * @author CONTENT CONTROL http://www.contentcontrol-berlin.de/
4
 * @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/
5
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
6
 */
7
8
namespace midgard\portable\storage;
9
10
use midgard\portable\api\dbobject;
11
use midgard\portable\api\error\exception;
12
use midgard\portable\storage\interfaces\metadata;
13
use Doctrine\ORM\EntityManager;
14
use Doctrine\ORM\Proxy\Proxy;
15
use Doctrine\ORM\UnitOfWork;
16
use Doctrine\Common\Util\ClassUtils;
17
use midgard_datetime;
18
19
class objectmanager
20
{
21
    private readonly EntityManager $em;
22
23 118
    public function __construct(EntityManager $em)
24
    {
25 118
        $this->em = $em;
0 ignored issues
show
Bug introduced by
The property em is declared read-only in midgard\portable\storage\objectmanager.
Loading history...
26
    }
27
28 118
    public function create(dbobject $entity)
29
    {
30 118
        foreach ($this->em->getClassMetadata(get_class($entity))->getAssociationNames() as $name) {
31 104
            if (!empty($entity->$name)) {
32
                //This makes sure that we don't have stale references
33 32
                $entity->$name = $entity->$name;
34
            }
35
        }
36
37
        //workaround for possible oid collisions in UnitOfWork
38
        //see https://github.com/doctrine/orm/issues/3037
39 118
        if ($this->em->getUnitOfWork()->getEntityState($entity) != UnitOfWork::STATE_NEW) {
40
            connection::log()->warning('oid collision during create detected, detaching ' . spl_object_hash($entity));
41
            $this->em->detach($entity);
42
        }
43
44 118
        $this->em->persist($entity);
45 118
        $this->em->flush($entity);
46 118
        $this->em->detach($entity);
47 118
        exception::ok();
48
    }
49
50 15
    public function update(dbobject $entity)
51
    {
52
        // if entities are loaded by querybuilder, they are managed at this point already,
53
        // which can in very rare (and very unreproducable) circumstances lead to the update
54
        // getting lost silently (because originalEntityData somehow is empty), so we detach
55
        // before doing anything else
56 15
        $this->em->detach($entity);
57 15
        $merged = $this->em->merge($entity);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\ORM\EntityManager::merge() has been deprecated: 2.7 This method is being removed from the ORM and won't have any replacement ( Ignorable by Annotation )

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

57
        $merged = /** @scrutinizer ignore-deprecated */ $this->em->merge($entity);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
58 15
        $this->copy_associations($entity, $merged);
59 15
        $this->em->persist($merged);
60 15
        $this->em->flush($merged);
61 15
        $this->em->detach($merged);
62 15
        $this->copy_metadata($merged, $entity);
63 15
        exception::ok();
64
    }
65
66
    /**
67
     * This is basically a workaround for some quirks when merging detached entities with changed associations
68
     *
69
     * @todo: This may or may not be a bug in Doctrine
70
     */
71 15
    private function copy_associations(dbobject $source, dbobject $target)
72
    {
73 15
        foreach ($this->em->getClassMetadata(get_class($source))->getAssociationNames() as $name) {
74 13
            $target->$name = $source->$name;
75
        }
76
    }
77
78 27
    private function kill_potential_proxies(dbobject $entity)
79
    {
80 27
        $classname = ClassUtils::getClass($entity);
81 27
        $cm = $this->em->getClassMetadata($classname);
82 27
        $changed_associations = $entity->__get_changed_associations();
83
84 27
        foreach ($cm->getAssociationNames() as $name) {
85 24
            if ($entity->$name === 0) {
86
                //This is necessary to kill potential proxy objects pointing to purged entities
87 21
                $entity->$name = 0;
88 11
            } elseif (!isset($changed_associations[$name])) {
89 11
                $value = $cm->getReflectionProperty($name)->getValue($entity);
0 ignored issues
show
Bug introduced by
The method getReflectionProperty() does not exist on Doctrine\Persistence\Mapping\ClassMetadata. Did you maybe mean getReflectionClass()? ( Ignorable by Annotation )

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

89
                $value = $cm->/** @scrutinizer ignore-call */ getReflectionProperty($name)->getValue($entity);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
90 11
                if ($value instanceof Proxy) {
91
                    //This makes sure that the associated entity doesn't end up in the changeset calculation
92 11
                    $value->__isInitialized__ = false;
0 ignored issues
show
Bug introduced by
Accessing __isInitialized__ on the interface Doctrine\ORM\Proxy\Proxy suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
93 11
                    continue;
94
                }
95
            }
96
        }
97
    }
98
99 27
    public function delete(dbobject $entity)
100
    {
101
        //we might deal with a proxy here, so we translate the classname
102 27
        $classname = ClassUtils::getClass($entity);
103 27
        $copy = new $classname($entity->id);
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist on midgard\portable\api\dbobject. Since you implemented __get, consider adding a @property annotation.
Loading history...
104
105
        //workaround for possible oid collisions in UnitOfWork
106
        //see https://github.com/doctrine/orm/issues/3037
107 27
        if ($this->em->getUnitOfWork()->getEntityState($copy) != UnitOfWork::STATE_DETACHED) {
108
            connection::log()->warning('oid collision during delete detected, detaching ' . spl_object_hash($copy));
109
            $this->em->detach($copy);
110
        }
111
112 27
        $copy = $this->em->merge($copy);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\ORM\EntityManager::merge() has been deprecated: 2.7 This method is being removed from the ORM and won't have any replacement ( Ignorable by Annotation )

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

112
        $copy = /** @scrutinizer ignore-deprecated */ $this->em->merge($copy);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
113 27
        $this->kill_potential_proxies($copy);
114 27
        $copy->metadata_deleted = true;
115
116 27
        $this->em->persist($copy);
117 27
        $this->em->flush($copy);
118 27
        $this->em->detach($copy);
119 27
        $this->em->detach($entity);
120 27
        $this->copy_metadata($copy, $entity, 'delete');
121 27
        exception::ok();
122
    }
123
124 3
    public function undelete(dbobject $entity)
125
    {
126 3
        $entity->metadata_deleted = false;
0 ignored issues
show
Bug Best Practice introduced by
The property metadata_deleted does not exist on midgard\portable\api\dbobject. Since you implemented __set, consider adding a @property annotation.
Loading history...
127 3
        $this->kill_potential_proxies($entity);
128
129 3
        $this->em->persist($entity);
130 3
        $this->em->flush($entity);
131 3
        $this->em->detach($entity);
132
    }
133
134 18
    public function purge(dbobject $entity)
135
    {
136 18
        $this->em->getFilters()->disable('softdelete');
137
        try {
138 18
            $entity = $this->em->merge($entity);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\ORM\EntityManager::merge() has been deprecated: 2.7 This method is being removed from the ORM and won't have any replacement ( Ignorable by Annotation )

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

138
            $entity = /** @scrutinizer ignore-deprecated */ $this->em->merge($entity);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
139
        } finally {
140 18
            $this->em->getFilters()->enable('softdelete');
141
        }
142 18
        $this->em->remove($entity);
143 18
        $this->em->flush($entity);
144 18
        $this->em->detach($entity);
145 18
        exception::ok();
146
    }
147
148 2
    public function approve(dbobject $entity)
149
    {
150 2
        $user = connection::get_user();
151 2
        $ref = $this->em->getReference(get_class($entity), $entity->id);
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist on midgard\portable\api\dbobject. Since you implemented __get, consider adding a @property annotation.
Loading history...
152 2
        $ref->metadata_isapproved = true;
153 2
        $ref->metadata_approver = $user->person;
154 2
        $ref->metadata_approved = new midgard_datetime;
155
156 2
        $this->em->persist($ref);
157 2
        $this->em->flush($ref);
158 2
        $this->em->detach($entity);
159 2
        $this->copy_metadata($ref, $entity, 'approve');
160 2
        exception::ok();
161
    }
162
163 1
    public function unapprove(dbobject $entity)
164
    {
165 1
        $user = connection::get_user();
166 1
        $ref = $this->em->getReference(get_class($entity), $entity->id);
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist on midgard\portable\api\dbobject. Since you implemented __get, consider adding a @property annotation.
Loading history...
167 1
        $ref->metadata_isapproved = false;
168 1
        $ref->metadata_approver = $user->person;
169 1
        $ref->metadata_approved = new midgard_datetime;
170
171 1
        $this->em->persist($ref);
172 1
        $this->em->flush($ref);
173 1
        $this->em->detach($entity);
174 1
        $this->copy_metadata($ref, $entity, 'approve');
175 1
        exception::ok();
176
    }
177
178 2
    public function lock(dbobject $entity)
179
    {
180 2
        $user = connection::get_user();
181 2
        $ref = $this->em->getReference(get_class($entity), $entity->id);
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist on midgard\portable\api\dbobject. Since you implemented __get, consider adding a @property annotation.
Loading history...
182 2
        $ref->metadata_islocked = true;
183 2
        $ref->metadata_locker = $user->person;
184 2
        $ref->metadata_locked = new midgard_datetime;
185
186 2
        $this->em->persist($ref);
187 2
        $this->em->flush($ref);
188 2
        $this->em->detach($entity);
189 2
        $this->copy_metadata($ref, $entity, 'lock');
190 2
        exception::ok();
191
    }
192
193 1
    public function unlock(dbobject $entity)
194
    {
195 1
        $ref = $this->em->getReference(get_class($entity), $entity->id);
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist on midgard\portable\api\dbobject. Since you implemented __get, consider adding a @property annotation.
Loading history...
196 1
        $ref->metadata_islocked = false;
197
198 1
        $this->em->persist($ref);
199 1
        $this->em->flush($ref);
200 1
        $this->em->detach($entity);
201 1
        $this->copy_metadata($ref, $entity, 'lock');
202 1
        exception::ok();
203
    }
204
205 118
    public function new_instance(string $classname) : dbobject
206
    {
207
        //workaround for possible oid collisions in UnitOfWork
208
        //see https://github.com/doctrine/orm/issues/3037
209 118
        $counter = 0;
210 118
        $candidates = [];
211
        do {
212 118
            $entity = new $classname;
213 118
            if ($counter++ > 100) {
214
                throw new exception('Failed to create fresh ' . $classname . ' instance (all tried oids are already known to UoW)');
215
            }
216
            //we keep the entity in memory to make sure we get a different oid during the next iteration
217 118
            $candidates[] = $entity;
218 118
        } while ($this->em->getUnitOfWork()->getEntityState($entity) !== UnitOfWork::STATE_NEW);
219
        // TODO: Calling $em->getUnitOfWork()->isInIdentityMap($entity) returns false in the same situation. Why?
220 118
        return $entity;
221
    }
222
223 40
    private function copy_metadata(dbobject $source, dbobject $target, string $action = 'update')
224
    {
225 40
        if (!$source instanceof metadata) {
226 1
            return;
227
        }
228
229 39
        $target->metadata_revised = $source->metadata_revised;
0 ignored issues
show
Bug Best Practice introduced by
The property metadata_revised does not exist on midgard\portable\api\dbobject. Since you implemented __set, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property metadata_revised does not exist on midgard\portable\api\dbobject. Since you implemented __get, consider adding a @property annotation.
Loading history...
230 39
        $target->metadata_revisor = $source->metadata_revisor;
0 ignored issues
show
Bug Best Practice introduced by
The property metadata_revisor does not exist on midgard\portable\api\dbobject. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property metadata_revisor does not exist on midgard\portable\api\dbobject. Since you implemented __set, consider adding a @property annotation.
Loading history...
231 39
        $target->metadata_revision = $source->metadata_revision;
0 ignored issues
show
Bug Best Practice introduced by
The property metadata_revision does not exist on midgard\portable\api\dbobject. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property metadata_revision does not exist on midgard\portable\api\dbobject. Since you implemented __set, consider adding a @property annotation.
Loading history...
232
233 39
        if ($action == 'lock') {
234 2
            $target->metadata_islocked = $source->metadata_islocked;
0 ignored issues
show
Bug Best Practice introduced by
The property metadata_islocked does not exist on midgard\portable\api\dbobject. Since you implemented __set, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property metadata_islocked does not exist on midgard\portable\api\dbobject. Since you implemented __get, consider adding a @property annotation.
Loading history...
235 2
            $target->metadata_locker = $source->metadata_locker;
0 ignored issues
show
Bug Best Practice introduced by
The property metadata_locker does not exist on midgard\portable\api\dbobject. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property metadata_locker does not exist on midgard\portable\api\dbobject. Since you implemented __set, consider adding a @property annotation.
Loading history...
236 2
            $target->metadata_locked = $source->metadata_locked;
0 ignored issues
show
Bug Best Practice introduced by
The property metadata_locked does not exist on midgard\portable\api\dbobject. Since you implemented __set, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property metadata_locked does not exist on midgard\portable\api\dbobject. Since you implemented __get, consider adding a @property annotation.
Loading history...
237 37
        } elseif ($action == 'approve') {
238 2
            $target->metadata_isapproved = $source->metadata_isapproved;
0 ignored issues
show
Bug Best Practice introduced by
The property metadata_isapproved does not exist on midgard\portable\api\dbobject. Since you implemented __set, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property metadata_isapproved does not exist on midgard\portable\api\dbobject. Since you implemented __get, consider adding a @property annotation.
Loading history...
239 2
            $target->metadata_approver = $source->metadata_approver;
0 ignored issues
show
Bug Best Practice introduced by
The property metadata_approver does not exist on midgard\portable\api\dbobject. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property metadata_approver does not exist on midgard\portable\api\dbobject. Since you implemented __set, consider adding a @property annotation.
Loading history...
240 2
            $target->metadata_approved = $source->metadata_approved;
0 ignored issues
show
Bug Best Practice introduced by
The property metadata_approved does not exist on midgard\portable\api\dbobject. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property metadata_approved does not exist on midgard\portable\api\dbobject. Since you implemented __set, consider adding a @property annotation.
Loading history...
241 35
        } elseif ($action == 'delete') {
242 27
            $target->metadata_deleted = $source->metadata_deleted;
0 ignored issues
show
Bug Best Practice introduced by
The property metadata_deleted does not exist on midgard\portable\api\dbobject. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property metadata_deleted does not exist on midgard\portable\api\dbobject. Since you implemented __set, consider adding a @property annotation.
Loading history...
243
        }
244
    }
245
}
246