Passed
Push — master ( 534a05...213efd )
by Andreas
03:03
created

objectmanager::approve()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
c 3
b 0
f 0
nc 1
nop 1
dl 0
loc 13
ccs 11
cts 11
cp 1
crap 1
rs 9.9332
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 $em;
22
23 115
    public function __construct(EntityManager $em)
24
    {
25 115
        $this->em = $em;
26
    }
27
28 115
    public function create(dbobject $entity)
29
    {
30 115
        foreach ($this->em->getClassMetadata(get_class($entity))->getAssociationNames() as $name) {
31 102
            if (!empty($entity->$name)) {
32
                //This makes sure that we don't have stale references
33 31
                $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 115
        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 115
        $this->em->persist($entity);
45 115
        $this->em->flush($entity);
46 115
        $this->em->detach($entity);
47 115
        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 26
    private function kill_potential_proxies(dbobject $entity)
79
    {
80 26
        $classname = ClassUtils::getRealClass(get_class($entity));
81 26
        $cm = $this->em->getClassMetadata($classname);
82 26
        $changed_associations = $entity->__get_changed_associations();
83
84 26
        foreach ($cm->getAssociationNames() as $name) {
85 23
            if ($entity->$name === 0) {
86
                //This is necessary to kill potential proxy objects pointing to purged entities
87 20
                $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 26
    public function delete(dbobject $entity)
100
    {
101
        //we might deal with a proxy here, so we translate the classname
102 26
        $classname = ClassUtils::getRealClass(get_class($entity));
103 26
        $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 26
        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 26
        $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 26
        $this->kill_potential_proxies($copy);
114 26
        $copy->metadata_deleted = true;
115
116 26
        $this->em->persist($copy);
117 26
        $this->em->flush($copy);
118 26
        $this->em->detach($copy);
119 26
        $this->em->detach($entity);
120 26
        $this->copy_metadata($copy, $entity, 'delete');
121 26
        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 17
    public function purge(dbobject $entity)
135
    {
136 17
        $this->em->getFilters()->disable('softdelete');
137
        try {
138 17
            $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 17
        } finally {
140 17
            $this->em->getFilters()->enable('softdelete');
141
        }
142 17
        $this->em->remove($entity);
143 17
        $this->em->flush($entity);
144 17
        $this->em->detach($entity);
145 17
        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 115
    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 115
        $counter = 0;
210 115
        $candidates = [];
211
        do {
212 115
            $entity = new $classname;
213 115
            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 115
            $candidates[] = $entity;
218 115
        } while ($this->em->getUnitOfWork()->getEntityState($entity) !== UnitOfWork::STATE_NEW);
219
        // TODO: Calling $em->getUnitOfWork()->isInIdentityMap($entity) returns false in the same situation. Why?
220 115
        return $entity;
221
    }
222
223 39
    private function copy_metadata(dbobject $source, dbobject $target, string $action = 'update')
224
    {
225 39
        if (!$source instanceof metadata) {
226 1
            return;
227
        }
228
229 38
        $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 38
        $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 38
        $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 38
        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 36
        } 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 34
        } elseif ($action == 'delete') {
242 26
            $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