diff --git a/src/User/AvatarUploader.php b/src/User/AvatarUploader.php index 7070ff4b81..5bfb3d8a40 100644 --- a/src/User/AvatarUploader.php +++ b/src/User/AvatarUploader.php @@ -36,13 +36,18 @@ public function upload(User $user, Image $image) $avatarPath = Str::random().'.png'; - $this->remove($user); + $this->removeFileAfterSave($user); $user->changeAvatarPath($avatarPath); $this->uploadDir->put($avatarPath, $encodedImage); } - public function remove(User $user) + /** + * Handle the removal of the old avatar file after a successful user save + * We don't place this in remove() because otherwise we would call changeAvatarPath 2 times when uploading. + * @param User $user + */ + protected function removeFileAfterSave(User $user) { $avatarPath = $user->getOriginal('avatar_url'); @@ -51,6 +56,14 @@ public function remove(User $user) $this->uploadDir->delete($avatarPath); } }); + } + + /** + * @param User $user + */ + public function remove(User $user) + { + $this->removeFileAfterSave($user); $user->changeAvatarPath(null); } diff --git a/src/User/Command/UploadAvatarHandler.php b/src/User/Command/UploadAvatarHandler.php index 7700becf10..69a3976c1e 100644 --- a/src/User/Command/UploadAvatarHandler.php +++ b/src/User/Command/UploadAvatarHandler.php @@ -80,6 +80,8 @@ public function handle(UploadAvatar $command) $user->save(); + $this->dispatchEventsFor($user, $actor); + return $user; } }