These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Kunstmaan\AdminListBundle\Service; |
||
4 | |||
5 | use Doctrine\Common\Persistence\ObjectManager; |
||
6 | use FOS\UserBundle\Model\User; |
||
7 | use Kunstmaan\AdminListBundle\Entity\LockableEntity; |
||
8 | use Kunstmaan\AdminListBundle\Entity\EntityVersionLock; |
||
9 | use Kunstmaan\AdminListBundle\Entity\LockableEntityInterface; |
||
10 | use Kunstmaan\AdminListBundle\Repository\EntityVersionLockRepository; |
||
11 | |||
12 | class EntityVersionLockService |
||
13 | { |
||
14 | /** |
||
15 | * @var ObjectManager |
||
16 | */ |
||
17 | private $objectManager; |
||
18 | |||
19 | /** |
||
20 | * @var int |
||
21 | */ |
||
22 | private $threshold; |
||
23 | |||
24 | /** |
||
25 | * @var bool |
||
26 | */ |
||
27 | private $lockEnabled; |
||
28 | |||
29 | public function __construct(ObjectManager $em, $threshold, $lockEnabled) |
||
30 | { |
||
31 | $this->setObjectManager($em); |
||
32 | $this->setThreshold($threshold); |
||
33 | $this->setLockEnabled($lockEnabled); |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * @param LockableEntityInterface $entity |
||
38 | * |
||
39 | * @return bool |
||
40 | */ |
||
41 | public function isEntityBelowThreshold(LockableEntityInterface $entity) |
||
42 | { |
||
43 | /** @var LockableEntity $lockable */ |
||
44 | $lockable = $this->getLockableEntity($entity, false); |
||
45 | |||
46 | if ($this->lockEnabled && $lockable->getId() !== null) { |
||
47 | $now = new \DateTime(); |
||
48 | $thresholdDate = clone $lockable->getUpdated(); |
||
49 | $thresholdDate->add(new \DateInterval('PT'.$this->threshold.'S')); |
||
50 | |||
51 | return $thresholdDate > $now; |
||
52 | } |
||
53 | |||
54 | return false; |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * @param User $user |
||
59 | * @param LockableEntityInterface $entity |
||
60 | * |
||
61 | * @return bool |
||
62 | */ |
||
63 | public function isEntityLocked(User $user, LockableEntityInterface $entity) |
||
64 | { |
||
65 | /** @var LockableEntity $lockable */ |
||
66 | $lockable = $this->getLockableEntity($entity); |
||
67 | |||
68 | if ($this->lockEnabled) { |
||
69 | $this->removeExpiredLocks($lockable); |
||
70 | $locks = $this->getEntityVersionLocksByLockableEntity($lockable, $user); |
||
71 | |||
72 | if ($locks === null || !count($locks)) { |
||
73 | $this->createEntityVersionLock($user, $lockable); |
||
74 | |||
75 | $lockable->setUpdated(new \DateTime()); |
||
76 | $this->objectManager->flush(); |
||
77 | |||
78 | return false; |
||
79 | } |
||
80 | |||
81 | return true; |
||
82 | } |
||
83 | |||
84 | return false; |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * When editing the entity, create a new entity translation lock. |
||
89 | * |
||
90 | * @param User $user |
||
91 | * @param LockableEntity $entity |
||
92 | */ |
||
93 | protected function createEntityVersionLock(User $user, LockableEntity $entity) |
||
94 | { |
||
95 | /** @var EntityVersionLock $lock */ |
||
96 | $lock = $this->objectManager->getRepository('KunstmaanAdminListBundle:EntityVersionLock')->findOneBy([ |
||
97 | 'owner' => $user->getUsername(), |
||
98 | 'lockableEntity' => $entity, |
||
99 | ]); |
||
100 | if (!$lock) { |
||
101 | $lock = new EntityVersionLock(); |
||
102 | } |
||
103 | $lock->setOwner($user->getUsername()); |
||
104 | $lock->setLockableEntity($entity); |
||
105 | $lock->setCreatedAt(new \DateTime()); |
||
106 | $this->objectManager->persist($lock); |
||
107 | $this->objectManager->flush(); |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * @param LockableEntityInterface $entity |
||
112 | * @param User $userToExclude |
||
113 | * |
||
114 | * @return array |
||
115 | */ |
||
116 | public function getUsersWithEntityVersionLock(LockableEntityInterface $entity, User $userToExclude = null) |
||
117 | { |
||
118 | /** @var LockableEntity $lockable */ |
||
119 | $lockable = $this->getLockableEntity($entity); |
||
120 | |||
121 | return array_reduce( |
||
122 | $this->getEntityVersionLocksByLockableEntity($lockable, $userToExclude), |
||
123 | function ($return, EntityVersionLock $item) { |
||
124 | $return[] = $item->getOwner(); |
||
125 | |||
126 | return $return; |
||
127 | }, |
||
128 | [] |
||
129 | ); |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * @param LockableEntity $entity |
||
134 | */ |
||
135 | protected function removeExpiredLocks(LockableEntity $entity) |
||
136 | { |
||
137 | $locks = $this->objectManager->getRepository('KunstmaanAdminListBundle:EntityVersionLock')->getExpiredLocks($entity, $this->threshold); |
||
138 | foreach ($locks as $lock) { |
||
139 | $this->objectManager->remove($lock); |
||
140 | } |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * When editing an entity, check if there is a lock for this entity. |
||
145 | * |
||
146 | * @param LockableEntity $entity |
||
147 | * @param User $userToExclude |
||
0 ignored issues
–
show
|
|||
148 | * |
||
149 | * @return EntityVersionLock[] |
||
150 | */ |
||
151 | protected function getEntityVersionLocksByLockableEntity(LockableEntity $entity, User $userToExclude = null) |
||
152 | { |
||
153 | /** @var EntityVersionLockRepository $objectRepository */ |
||
154 | $objectRepository = $this->objectManager->getRepository('KunstmaanAdminListBundle:EntityVersionLock'); |
||
155 | |||
156 | return $objectRepository->getLocksForLockableEntity($entity, $this->threshold, $userToExclude); |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * Get or create a LockableEntity for an entity with LockableEntityInterface |
||
161 | * |
||
162 | * @param LockableEntityInterface $entity |
||
163 | * |
||
164 | * @return LockableEntity |
||
165 | */ |
||
166 | protected function getLockableEntity(LockableEntityInterface $entity, $create = true) |
||
167 | { |
||
168 | /** @var LockableEntity $lockable */ |
||
169 | $lockable = $this->objectManager->getRepository('KunstmaanAdminListBundle:LockableEntity')->getOrCreate($entity->getId(), get_class($entity)); |
||
170 | |||
171 | if ($create === true && $lockable->getId() === null) { |
||
172 | $this->objectManager->persist($lockable); |
||
173 | $this->objectManager->flush(); |
||
174 | } |
||
175 | |||
176 | return $lockable; |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * @param ObjectManager $objectManager |
||
181 | */ |
||
182 | public function setObjectManager($objectManager) |
||
183 | { |
||
184 | $this->objectManager = $objectManager; |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * @param int $threshold |
||
189 | */ |
||
190 | public function setThreshold($threshold) |
||
191 | { |
||
192 | $this->threshold = $threshold; |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * @param bool lockEnabled |
||
197 | */ |
||
198 | public function setLockEnabled($lockEnabled) |
||
199 | { |
||
200 | $this->lockEnabled = $lockEnabled; |
||
201 | } |
||
202 | } |
||
203 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.