}
}
- if ($this->user->avatarID) {
+ if ($this->user->avatarFileID !== null || $this->user->avatarID !== null) {
$data['avatarURL'] = $this->user->getAvatar()->getURL();
}
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
-use wcf\data\file\FileAction;
use wcf\data\IStorableObject;
-use wcf\data\user\UserEditor;
use wcf\data\user\UserProfile;
use wcf\http\Helper;
use wcf\system\cache\runtime\UserProfileRuntimeCache;
use wcf\system\form\builder\field\RadioButtonFormField;
use wcf\system\form\builder\IFormDocument;
use wcf\system\form\builder\Psr15DialogForm;
-use wcf\system\user\storage\UserStorageHandler;
+use wcf\system\user\command\SetAvatar;
use wcf\system\user\UserProfileHandler;
use wcf\system\WCF;
// However, if the user wants to delete their avatar and use a standard avatar,
// this must be saved and the cache reset
if ($data['avatarType'] === 'none') {
- if ($user->avatarFileID !== null) {
- (new FileAction([$user->avatarFileID], 'delete'))->executeAction();
- }
-
- (new UserEditor($user->getDecoratedObject()))->update([
- 'avatarFileID' => null,
- ]);
-
- UserStorageHandler::getInstance()->reset([$user->userID], 'avatar');
+ (new SetAvatar($user->getDecoratedObject()))();
}
return new JsonResponse([
use wcf\data\file\File;
use wcf\data\user\avatar\UserAvatar;
-use wcf\data\user\UserEditor;
use wcf\data\user\UserProfile;
use wcf\system\cache\runtime\UserProfileRuntimeCache;
use wcf\system\database\util\PreparedStatementConditionBuilder;
use wcf\system\exception\UserInputException;
-use wcf\system\user\storage\UserStorageHandler;
+use wcf\system\user\command\SetAvatar;
use wcf\system\WCF;
use wcf\util\FileUtil;
WCF::getSession()->update();
}
- (new UserEditor($user->getDecoratedObject()))->update([
- 'avatarFileID' => $file->fileID,
- ]);
- // reset user storage
- UserStorageHandler::getInstance()->reset([$user->userID], 'avatar');
+ (new SetAvatar($user->getDecoratedObject(), $file))();
}
#[\Override]
--- /dev/null
+<?php
+
+namespace wcf\system\user\command;
+
+use wcf\data\file\File;
+use wcf\data\file\FileAction;
+use wcf\data\user\avatar\UserAvatarAction;
+use wcf\data\user\User;
+use wcf\data\user\UserEditor;
+use wcf\system\user\storage\UserStorageHandler;
+
+final class SetAvatar
+{
+ public function __construct(
+ private readonly User $user,
+ private readonly ?File $file = null
+ ) {
+ }
+
+ public function __invoke()
+ {
+ if ($this->file === null) {
+ (new FileAction([$this->user->avatarFileID], 'delete'))->executeAction();
+ }
+
+ // Delete old `UserAvatar` object if provided
+ if ($this->user->avatarID) {
+ (new UserAvatarAction([$this->user->avatarID], 'delete'))->executeAction();
+ }
+
+ (new UserEditor($this->user))->update([
+ 'avatarFileID' => $this->file?->fileID,
+ 'avatarID' => null,
+ ]);
+
+ UserStorageHandler::getInstance()->reset([$this->user->userID], 'avatar');
+ }
+}