entityManager = $entityManager; $this->container = $container; // $this->trashBoxManager = $this->container->get('net15.trash.box.manager'); } /** * @param UserBox $element * @return UserBox|null */ public function quickUpdate(UserBox $element): ?UserBox { if (!$this->entityManager->contains($element)) { try { $this->entityManager->persist($element); } catch (ORMException $e) { return null; } } try { $this->entityManager->flush(); } catch (OptimisticLockException $e) { return null; } catch (ORMException $e) { return null; } return $element; } public function getByUser(CustomUserInterface $user){ $userBox = $this->entityManager->getRepository(UserBox::class)->findOneBy(['user'=>$user]); if($userBox === null){ $userBox = $this->create($user); } return $userBox; } private function create(CustomUserInterface $user){ if (!$user instanceof CustomUserInterface) return null; $userBox = new UserBox(); $name = $user->getId().'-user-box'; $userBox->setDefault($name, $user); if (!$this->quickUpdate($userBox)) return null; $trashBoxName = $name = ''; $trash = $this->container->get('net15.trash.box.manager')->create($trashBoxName, $user, $userBox); // $trash = $this->trashBoxManager->create($trashBoxName, $user, $userBox); $userBox->setTrashBox($trash); if (!$this->update($userBox)) return null; return $userBox; } /* * directories is children */ public function addDirectories(UserBox $userBox, Directory $directory){ if (!$userBox instanceof UserBox || !$directory instanceof Directory) return null; $userBox->addDirectory($directory); return $this->update($userBox); } public function addFile(UserBox $userBox, FileItem $fileItem){ if (!$userBox instanceof UserBox || !$fileItem instanceof FileItem) return null; $userBox->addFile($fileItem); return $this->update($userBox); } public function addFolderOwner(UserBox $userBox, FolderOwner $folderOwner){ if (!$userBox instanceof UserBox || !$folderOwner instanceof FolderOwner) return null; $userBox->addFolderOwner($folderOwner); return $this->update($userBox); } public function addFolderGroupOwner(UserBox $userBox, FolderGroupPermission $folderGroupPermission){ if (!$userBox instanceof UserBox || !$folderGroupPermission instanceof FolderGroupPermission) return null; $userBox->addFolderGroupPermission($folderGroupPermission); return $this->update($userBox); } public function update(UserBox $folder){ //lever de certains events return $this->quickUpdate($folder); } public function remove(UserBox $userBox){ try { $this->entityManager->remove($userBox); $this->entityManager->flush($userBox); } catch (ORMException $e) { return false; } return true; } }