From 4128a5403d522ef8a271a2367593c77d255d1ffb Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Sun, 9 Aug 2020 12:44:47 +0000 Subject: [PATCH] [MEDIA][EXCEPTIONS] Fix errors and deprecations --- components/Media/Controller/Avatar.php | 2 +- components/Media/Media.php | 1 + src/Core/Controller.php | 2 +- src/Entity/Avatar.php | 11 +++++++++-- src/Entity/Profile.php | 4 ++-- src/Util/Common.php | 4 ++-- src/Util/Exception/ClientException.php | 2 ++ src/Util/Exception/NicknameInvalidException.php | 2 +- src/Util/Exception/NicknameReservedException.php | 2 +- src/Util/Exception/NicknameTakenException.php | 4 +++- src/Util/Exception/NicknameTooLongException.php | 4 +++- 11 files changed, 26 insertions(+), 12 deletions(-) diff --git a/components/Media/Controller/Avatar.php b/components/Media/Controller/Avatar.php index cf6561c197..1d6766b3d2 100644 --- a/components/Media/Controller/Avatar.php +++ b/components/Media/Controller/Avatar.php @@ -49,7 +49,7 @@ class Avatar extends Controller } $res = $result[0]; - return Media::sendFile(EAvatar::getFilePath($res['file_hash']), $res['mimetype'], $res['title']); + return Media::sendFile(EAvatar::getFilePathStatic($res['file_hash']), $res['mimetype'], $res['title']); default: throw new Exception('Not implemented'); } diff --git a/components/Media/Media.php b/components/Media/Media.php index 103b1e89c7..f05b9eaa14 100644 --- a/components/Media/Media.php +++ b/components/Media/Media.php @@ -23,6 +23,7 @@ use App\Core\Log; use App\Core\Module; use App\Entity\File; use App\Util\Common; +use App\Util\Exception\ClientException; use App\Util\Nickname; use Symfony\Component\HttpFoundation\BinaryFileResponse; use Symfony\Component\HttpFoundation\File\Exception\FileException; diff --git a/src/Core/Controller.php b/src/Core/Controller.php index 48906381e1..93464bef12 100644 --- a/src/Core/Controller.php +++ b/src/Core/Controller.php @@ -62,7 +62,7 @@ class Controller extends AbstractController implements EventSubscriberInterface $controller = $event->getController(); $request = $event->getRequest(); - if (($avatar = DB::find('avatar', ['profile_id' => Common::profile()->getId()])) != null) { + if (($user = Common::user()) !== null && ($avatar = DB::find('avatar', ['profile_id' => $user->getProfile()->getId()])) != null) { $avatar_filename = $avatar->getUrl(); } else { $avatar_filename = '/public/assets/default_avatar.svg'; diff --git a/src/Entity/Avatar.php b/src/Entity/Avatar.php index 971be60fac..9adca72b88 100644 --- a/src/Entity/Avatar.php +++ b/src/Entity/Avatar.php @@ -1,6 +1,7 @@ . + // }}} namespace App\Entity; @@ -107,9 +109,14 @@ class Avatar extends Entity return $this->file; } - public function getFilePath(?string $filename = null): string + public static function getFilePathStatic(string $filename): string { - return Common::config('avatar', 'dir') . '/' . $filename ?: $this->getFile()->getFileName(); + return Common::config('avatar', 'dir') . '/' . $filename; + } + + public function getFilePath(): string + { + return Common::config('avatar', 'dir') . '/' . $this->getFile()->getFileName(); } /** diff --git a/src/Entity/Profile.php b/src/Entity/Profile.php index 45f75e6131..c879923745 100644 --- a/src/Entity/Profile.php +++ b/src/Entity/Profile.php @@ -202,12 +202,12 @@ class Profile extends Entity // }}} Autocode - public function getFromId(int $id): ?self + public static function getFromId(int $id): ?self { return DB::find('profile', ['id' => $id]); } - public function getFromNickname(string $nickname): ?self + public static function getFromNickname(string $nickname): ?self { return DB::findOneBy('profile', ['nickname' => $nickname]); } diff --git a/src/Util/Common.php b/src/Util/Common.php index ea3cf408d5..acd0fa031f 100644 --- a/src/Util/Common.php +++ b/src/Util/Common.php @@ -70,12 +70,12 @@ abstract class Common DB::flush(); } - public static function user(): LocalUser + public static function user(): ?LocalUser { return Security::getUser(); } - public static function profile(): Profile + public static function profile(): ?Profile { return self::user()->getProfile(); } diff --git a/src/Util/Exception/ClientException.php b/src/Util/Exception/ClientException.php index 5effe6d47f..31e8dde84b 100644 --- a/src/Util/Exception/ClientException.php +++ b/src/Util/Exception/ClientException.php @@ -19,6 +19,8 @@ namespace App\Util\Exception; +use Exception; + /** * Client exception. Indicates a client request contains some sort of * error. HTTP code 400 diff --git a/src/Util/Exception/NicknameInvalidException.php b/src/Util/Exception/NicknameInvalidException.php index 3c4674ea84..7058c48400 100644 --- a/src/Util/Exception/NicknameInvalidException.php +++ b/src/Util/Exception/NicknameInvalidException.php @@ -40,7 +40,7 @@ namespace App\Util\Exception; */ class NicknameInvalidException extends NicknameException { - protected function defaultMessage() + protected function defaultMessage(): string { // TRANS: Validation error in form for registration, profile and group settings, etc. return _m('Nickname must have only lowercase letters and numbers and no spaces.'); diff --git a/src/Util/Exception/NicknameReservedException.php b/src/Util/Exception/NicknameReservedException.php index e17e37e9b4..d4ac30c753 100644 --- a/src/Util/Exception/NicknameReservedException.php +++ b/src/Util/Exception/NicknameReservedException.php @@ -40,7 +40,7 @@ namespace App\Util\Exception; */ class NicknameReservedException extends NicknameException { - protected function defaultMessage() + protected function defaultMessage(): string { // TRANS: Validation error in form for registration, profile and group settings, etc. return _m('Nickname is reserved.'); diff --git a/src/Util/Exception/NicknameTakenException.php b/src/Util/Exception/NicknameTakenException.php index 2f31bcd25c..02243a3a1b 100644 --- a/src/Util/Exception/NicknameTakenException.php +++ b/src/Util/Exception/NicknameTakenException.php @@ -1,6 +1,7 @@ . + // }}} namespace App\Util\Exception; @@ -48,7 +50,7 @@ class NicknameTakenException extends NicknameException parent::__construct($msg, $code); } - protected function defaultMessage() + protected function defaultMessage(): string { // TRANS: Validation error in form for registration, profile and group settings, etc. return _m('Nickname is already in use on this server.'); diff --git a/src/Util/Exception/NicknameTooLongException.php b/src/Util/Exception/NicknameTooLongException.php index 0d4db0d0b7..8b83e4b22d 100644 --- a/src/Util/Exception/NicknameTooLongException.php +++ b/src/Util/Exception/NicknameTooLongException.php @@ -1,6 +1,7 @@ . + // }}} namespace App\Util\Exception; @@ -42,7 +44,7 @@ use App\Util\Nickname; */ class NicknameTooLongException extends NicknameInvalidException { - protected function defaultMessage() + protected function defaultMessage(): string { // TRANS: Validation error in form for registration, profile and group settings, etc. return _m('Nickname cannot be more than # character long.', ['count' => Nickname::MAX_LEN]);