em = $em; $this->fmm = $fmm; $this->sysfm = $sysfm; $this->fps = $fps; } /** * @param Avatar $avatar * @param FileItem $fileItem * @param bool $autoUpdate * @return bool */ public function setOrigineFile(Avatar $avatar, FileItem $fileItem, $autoUpdate = true){ $sysbox = $this->sysfm->getSystemBox(); if(!$sysbox instanceof FolderItem) return false; $r = $this->fmm->moveFileInFolder($fileItem,$sysbox); if(\is_array($r)) return false; unset($r); $avatar->setFori($fileItem); if($autoUpdate) return $this->quickUpdate($avatar); return true; } /** * @param FileItem $fileItem * @return Avatar|null */ public function createAvatarFromFileItem(FileItem $fileItem): ?Avatar { $avatar = new Avatar(); $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->fmm->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->fps->getAliasedUser(); $this->fps->setAliasedUser($user); if(!$this->fps->canRead($file)){ $this->fps->setAliasedUser($oldAlias); return null; } $this->fps->setAliasedUser($oldAlias); } //get old avatar if any $oldAvatar = $user->getAvatar(); //set new avatar $newAvatar = $this->createAvatarFromFileItem($file); 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->em->contains($element)) { try { $this->em->persist($element); } catch (ORMException $e) { return false; } } try { $this->em->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->em->remove($element); } catch (ORMException $e) { return false; } try { $this->em->flush(); } catch (OptimisticLockException $e) { return false; } catch (ORMException $e) { return false; } return true; } }