Conditions | 6 |
Paths | 18 |
Total Lines | 110 |
Code Lines | 87 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
195 | ->innerJoin('resource.resourceNode', 'node') |
||
196 | ->innerJoin('node.resourceLinks', 'links') |
||
197 | ->andWhere('links.course = :course') |
||
198 | ->setParameter('course', $this->courseRef()); |
||
199 | |||
200 | if ($ids && \count($ids) > 0) { |
||
201 | $qb->andWhere('resource.iid IN (:ids)')->setParameter('ids', $ids); |
||
202 | } |
||
203 | |||
204 | return $qb->getQuery()->getResult(); |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * Hard-deletes a list of resources. If repository doesn't provide hardDelete(), |
||
209 | * falls back to EM->remove() and a final flush (expect proper cascade mappings). |
||
210 | */ |
||
211 | private function hardDeleteMany(string $entityClass, array $resources): void |
||
212 | { |
||
213 | $repo = $this->em->getRepository($entityClass); |
||
214 | |||
215 | $usedFallback = false; |
||
216 | foreach ($resources as $res) { |
||
217 | if (method_exists($repo, 'hardDelete')) { |
||
218 | // hardDelete takes care of Resource, ResourceNode, Links and Files (Flysystem) |
||
219 | $repo->hardDelete($res); |
||
220 | } else { |
||
221 | // Fallback: standard remove. Ensure your mappings cascade what you need. |
||
222 | $this->em->remove($res); |
||
223 | $usedFallback = true; |
||
224 | } |
||
225 | } |
||
226 | |||
227 | // One flush at the end. If hardDelete() already flushed internally, this is harmless. |
||
228 | if ($usedFallback) { |
||
229 | $this->em->flush(); |
||
230 | } |
||
231 | } |
||
232 | |||
233 | /** Deletes all resources of a type in the course */ |
||
234 | private function deleteAllOfTypeForCourse(string $entityClass): void |
||
235 | { |
||
236 | $resources = $this->fetchResourcesForCourse($entityClass, null); |
||
237 | if ($resources) { |
||
238 | $this->hardDeleteMany($entityClass, $resources); |
||
239 | } |
||
240 | } |
||
241 | |||
242 | /** Deletes selected resources (by iid) of a type in the course */ |
||
243 | private function deleteSelectedOfTypeForCourse(string $entityClass, array $ids): void |
||
244 | { |
||
245 | if (!$ids) { |
||
246 | return; |
||
247 | } |
||
248 | $resources = $this->fetchResourcesForCourse($entityClass, $ids); |
||
249 | if ($resources) { |
||
250 | $this->hardDeleteMany($entityClass, $resources); |
||
251 | } |
||
252 | } |
||
253 | |||
254 | /** Optional post-clean for empty categories if repository supports it */ |
||
255 | private function autoCleanIfSupported(string $entityClass): void |
||
256 | { |
||
257 | $repo = $this->em->getRepository($entityClass); |
||
258 | if (method_exists($repo, 'deleteEmptyByCourse')) { |
||
259 | $repo->deleteEmptyByCourse($this->courseId); |
||
260 | } |
||
261 | } |
||
262 | |||
263 | /** Detach categories from ALL LPs in course (repo-level bulk method preferred if available) */ |
||
264 | private function clearLpCategoriesForCourse(): void |
||
265 | { |
||
266 | $lps = $this->fetchResourcesForCourse(CLp::class, null); |
||
267 | $changed = false; |
||
268 | foreach ($lps as $lp) { |
||
269 | if (method_exists($lp, 'getCategory') && method_exists($lp, 'setCategory')) { |
||
270 | if ($lp->getCategory()) { |
||
271 | $lp->setCategory(null); |
||
272 | $this->em->persist($lp); |
||
273 | $changed = true; |
||
274 | } |
||
275 | } |
||
276 | } |
||
277 | if ($changed) { |
||
278 | $this->em->flush(); |
||
279 | } |
||
280 | } |
||
281 | |||
282 | /** Detach categories only for LPs that are linked to given category ids */ |
||
283 | private function clearLpCategoriesForIds(array $catIds): void |
||
284 | { |
||
285 | $lps = $this->fetchResourcesForCourse(CLp::class, null); |
||
286 | $changed = false; |
||
287 | foreach ($lps as $lp) { |
||
288 | $cat = method_exists($lp, 'getCategory') ? $lp->getCategory() : null; |
||
289 | $catId = $cat?->getId(); |
||
290 | if ($catId !== null && \in_array($catId, $catIds, true) && method_exists($lp, 'setCategory')) { |
||
291 | $lp->setCategory(null); |
||
292 | $this->em->persist($lp); |
||
293 | $changed = true; |
||
294 | } |
||
295 | } |
||
296 | if ($changed) { |
||
297 | $this->em->flush(); |
||
298 | } |
||
299 | } |
||
300 | |||
301 | /** SCORM directory cleanup for ALL LPs (hook your storage service here if needed) */ |
||
302 | private function cleanupScormDirsForAllLp(): void |
||
303 | { |
||
304 | // If you have a storage/scorm service, invoke it here. |
||
305 | // By default, nothing: hardDelete already deletes files linked to ResourceNode. |
||
314 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.