entityManager = $entityManager; $this->fileManagerMainService = $fileManagerMainService; $this->systemFileManager = $systemFileManager; $this->fileManagerPermissionService = $fileManagerPermissionService; $this->systemBoxManager = $systemBoxManager; $this->systemBox = $this->systemBoxManager->getByName("avatar_file_system"); } /** * @param Avatar $avatar * @param FileItem $fileItem * @param bool $autoUpdate * @return bool */ public function setOrigineFile(Avatar $avatar, FileItem $fileItem, $autoUpdate = true){ if(!$this->systemBox instanceof SystemBox) return false; $r = $this->fileManagerMainService->moveFileInFolder($fileItem,$this->systemBox); if(\is_array($r)) return false; unset($r); $avatar->setFori($fileItem); if($autoUpdate) return $this->quickUpdate($avatar); return true; } /** * @param FileItem $fileItem * @param UserFileOwnerInterface $user * @return Avatar|null */ public function createAvatarFromFileItem(FileItem $fileItem, UserFileOwnerInterface $user): ?Avatar { $avatar = new Avatar(); $avatar->setUserFileOwnerInterface($user); $r = $this->setOrigineFile($avatar,$fileItem); if(!$r) return null; return $avatar; } /** * @param UserFileOwnerInterface $user * @param String $guid * @param bool $applyPermission * @return Avatar|null */ public function setUserAvatarFromFileGuid(UserFileOwnerInterface $user, String $guid, $applyPermission = true): ?Avatar { $file = $this->fileManagerMainService->getFileFromGuid($guid,true); if(!$file instanceof FileItem) return null; return $this->setUserAvatarFromFileItem($user,$file, $applyPermission); } /** * @param UserFileOwnerInterface $user * @param FileItem $file * @param bool $applyPermission * @return Avatar|null */ public function setUserAvatarFromFileItem(UserFileOwnerInterface $user, FileItem $file, $applyPermission = true): ?Avatar { if($applyPermission){ //in case it was previously in use $oldAlias = $this->fileManagerPermissionService->getAliasedUser(); $this->fileManagerPermissionService->setAliasedUser($user); if(!$this->fileManagerPermissionService->canRead($file)){ $this->fileManagerPermissionService->setAliasedUser($oldAlias); return null; } $this->fileManagerPermissionService->setAliasedUser($oldAlias); } //get old avatar if any $oldAvatar = $user->getAvatar(); //set new avatar $newAvatar = $this->createAvatarFromFileItem($file, $user); if(!$newAvatar instanceof Avatar) return null; $user->setAvatar($newAvatar); $r = $this->quickUpdate($newAvatar); if(!$r) return null; //if there's an old avatar if($oldAvatar instanceof Avatar){ //delete old avatar $this->deleteAvatar($oldAvatar); } //return new avatar entity return $newAvatar; } public function deleteAvatar(Avatar $avatar){ return $this->delete($avatar); } /** * @param Avatar $element * @return bool */ public function update(Avatar $element) { //do stuff... return $this->quickUpdate($element); } /** * @param Avatar $element * @return bool */ public function quickUpdate(Avatar $element) { if (!$this->entityManager->contains($element)) { try { $this->entityManager->persist($element); } catch (ORMException $e) { return false; } } try { $this->entityManager->flush(); } catch (OptimisticLockException $e) { return false; } catch (ORMException $e) { return false; } return true; } /** * @param Avatar $element * @return bool */ public function delete(Avatar $element) { try { $this->entityManager->remove($element); } catch (ORMException $e) { return false; } try { $this->entityManager->flush(); } catch (OptimisticLockException $e) { return false; } catch (ORMException $e) { return false; } return true; } }