entityManager = $entityManager; $this->repository = $this->entityManager->getRepository(Directory::class); } /** * @param Directory $element * @return Directory|null */ public function quickUpdate(Directory $element): ?Directory { 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 createTrashDirectory(string $name, ?FolderItemInterface $parent, TrashBox $trashBox){ $directory = new Directory(); $directory->setDefault($name, $parent); $directory->setTrashBox($trashBox); if (!$this->quickUpdate($directory)) return null; return $directory; } public function createUserBoxDirectory(string $name, ?FolderItemInterface $parent, UserBox $userBox){ $directory = new Directory(); $directory->setDefault($name, $parent); $directory->setUserBox($userBox); if (!$this->quickUpdate($directory)) return null; return $directory; } /** * @param string $name * @param FolderItemInterface|null $parent * @param Box $systemBox * @return Directory|null */ public function createSystemBoxDirectory(string $name, ?FolderItemInterface $parent, Box $systemBox): ?Directory{ $directory = new Directory(); $directory->setDefault($name,$systemBox , $parent); $directory->setParentBox($systemBox); if (!$this->quickUpdate($directory)) return null; return $directory; } /** * @param string $name * @param Box|null $box * @param FolderItemInterface|null $parentDirectory * @return Directory|null */ public function createDirectory(string $name, ?Box $box = null, ?FolderItemInterface $parentDirectory = null): ?Directory{ $directory = new Directory(); $directory->setDefault($name,$box, $parentDirectory); if (!$this->quickUpdate($directory)) return null; return $directory; } public function updateBoxDirectory(Directory $directory, string $name, ?FolderItemInterface $parentDirectory = null, Box $box = null): ? Directory{ $directory->setDefault($name, $box, $parentDirectory); if (!$this->quickUpdate($directory)) return null; return $directory; } // public function updateUserBoxDirectory(Directory $directory, string $name, ?FolderItemInterface $parentDirectory, UserBox $userBox): ? Directory{ // $directory->setDefault($name, $parentDirectory); // $directory->setUserBox($userBox); // // if (!$this->quickUpdate($directory)) // return null; // // return $directory; // } // public function updateTrashBoxDirectory(Directory $directory, string $name, ?FolderItemInterface $parentDirectory, TrashBox $trashBox): ? Directory{ // $directory->setDefault($name, $parentDirectory); // $directory->setTrashBox($trashBox); // // if (!$this->quickUpdate($directory)) // return null; // // return $directory; // } public function getFolderByName(string $folderName){ return $this->repository->findOneBy(['name'=>$folderName]); } public function getByName(string $name, ?Box $box = null, ?FolderItemInterface $parentDirectory = null){ $directory = $this->entityManager->getRepository(Directory::class)->findOneBy(['name'=>$name, 'parentDirectory'=> $parentDirectory]); if($directory === null){ $directory = $this->createDirectory($name, $box, $parentDirectory); } return $directory; } }