diff --git a/src/Api/AbstractPostJob.php b/src/Api/AbstractPostJob.php index 470a2187..80a1c131 100644 --- a/src/Api/AbstractPostJob.php +++ b/src/Api/AbstractPostJob.php @@ -12,7 +12,7 @@ abstract class AbstractPostJob extends AbstractJob else { $postId = $this->getArgument(self::POST_ID); - $this->post = PostModel::findByIdOrName($postId); + $this->post = PostModel::getByIdOrName($postId); } } } diff --git a/src/Api/AbstractUserJob.php b/src/Api/AbstractUserJob.php index 1b2acf11..a3b722be 100644 --- a/src/Api/AbstractUserJob.php +++ b/src/Api/AbstractUserJob.php @@ -12,7 +12,7 @@ abstract class AbstractUserJob extends AbstractJob else { $userName = $this->getArgument(self::USER_NAME); - $this->user = UserModel::findByNameOrEmail($userName); + $this->user = UserModel::getByNameOrEmail($userName); } } } diff --git a/src/Api/Jobs/ActivateUserEmailJob.php b/src/Api/Jobs/ActivateUserEmailJob.php index 313a3a35..fed754e1 100644 --- a/src/Api/Jobs/ActivateUserEmailJob.php +++ b/src/Api/Jobs/ActivateUserEmailJob.php @@ -7,7 +7,7 @@ class ActivateUserEmailJob extends AbstractJob { if (!$this->hasArgument(self::TOKEN)) { - $user = UserModel::findByNameOrEmail($this->getArgument(self::USER_NAME)); + $user = UserModel::getByNameOrEmail($this->getArgument(self::USER_NAME)); if (empty($user->getUnconfirmedEmail())) { @@ -24,7 +24,7 @@ class ActivateUserEmailJob extends AbstractJob else { $tokenText = $this->getArgument(self::TOKEN); - $token = TokenModel::findByToken($tokenText); + $token = TokenModel::getByToken($tokenText); TokenModel::checkValidity($token); $user = $token->getUser(); diff --git a/src/Api/Jobs/AddCommentJob.php b/src/Api/Jobs/AddCommentJob.php index 8d43e8ac..e6b742a3 100644 --- a/src/Api/Jobs/AddCommentJob.php +++ b/src/Api/Jobs/AddCommentJob.php @@ -4,7 +4,7 @@ class AddCommentJob extends AbstractJob public function execute() { $user = Auth::getCurrentUser(); - $post = PostModel::findById($this->getArgument(self::POST_ID)); + $post = PostModel::getById($this->getArgument(self::POST_ID)); $text = $this->getArgument(self::TEXT); $comment = CommentModel::spawn(); diff --git a/src/Api/Jobs/DeleteCommentJob.php b/src/Api/Jobs/DeleteCommentJob.php index 9e6894da..47fb7c2b 100644 --- a/src/Api/Jobs/DeleteCommentJob.php +++ b/src/Api/Jobs/DeleteCommentJob.php @@ -5,7 +5,7 @@ class DeleteCommentJob extends AbstractJob public function prepare() { - $this->comment = CommentModel::findById($this->getArgument(self::COMMENT_ID)); + $this->comment = CommentModel::getById($this->getArgument(self::COMMENT_ID)); } public function execute() diff --git a/src/Api/Jobs/EditCommentJob.php b/src/Api/Jobs/EditCommentJob.php index afde3e5d..c9dd0b52 100644 --- a/src/Api/Jobs/EditCommentJob.php +++ b/src/Api/Jobs/EditCommentJob.php @@ -5,7 +5,7 @@ class EditCommentJob extends AbstractJob public function prepare() { - $this->comment = CommentModel::findById($this->getArgument(self::COMMENT_ID)); + $this->comment = CommentModel::getById($this->getArgument(self::COMMENT_ID)); } public function execute() diff --git a/src/Api/Jobs/GetPostContentJob.php b/src/Api/Jobs/GetPostContentJob.php index 7b976184..c5c3b6f2 100644 --- a/src/Api/Jobs/GetPostContentJob.php +++ b/src/Api/Jobs/GetPostContentJob.php @@ -5,7 +5,7 @@ class GetPostContentJob extends AbstractJob public function prepare() { - $this->post = PostModel::findByName($this->getArgument(self::POST_NAME)); + $this->post = PostModel::getByName($this->getArgument(self::POST_NAME)); } public function execute() diff --git a/src/Api/Jobs/GetPostThumbJob.php b/src/Api/Jobs/GetPostThumbJob.php index 20c533a2..14a59e0a 100644 --- a/src/Api/Jobs/GetPostThumbJob.php +++ b/src/Api/Jobs/GetPostThumbJob.php @@ -16,7 +16,7 @@ class GetPostThumbJob extends AbstractJob $path = PostModel::getThumbDefaultPath($name, $width, $height); if (!file_exists($path)) { - $post = PostModel::findByIdOrName($name); + $post = PostModel::getByIdOrName($name); if ($post->isHidden()) Access::assert(new Privilege(Privilege::ListPosts, 'hidden')); diff --git a/src/Api/Jobs/PasswordResetJob.php b/src/Api/Jobs/PasswordResetJob.php index de7e77f2..785a431c 100644 --- a/src/Api/Jobs/PasswordResetJob.php +++ b/src/Api/Jobs/PasswordResetJob.php @@ -8,7 +8,7 @@ class PasswordResetJob extends AbstractJob { if (!$this->hasArgument(self::TOKEN)) { - $user = UserModel::findByNameOrEmail($this->getArgument(self::USER_NAME)); + $user = UserModel::getByNameOrEmail($this->getArgument(self::USER_NAME)); if (empty($user->getConfirmedEmail())) throw new SimpleException('This user has no e-mail confirmed; password reset cannot proceed'); @@ -20,7 +20,7 @@ class PasswordResetJob extends AbstractJob else { $tokenText = $this->getArgument(self::TOKEN); - $token = TokenModel::findByToken($tokenText); + $token = TokenModel::getByToken($tokenText); TokenModel::checkValidity($token); $alphabet = array_merge(range('A', 'Z'), range('a', 'z'), range('0', '9')); diff --git a/src/Api/Jobs/PreviewCommentJob.php b/src/Api/Jobs/PreviewCommentJob.php index 8c901432..e159e8b6 100644 --- a/src/Api/Jobs/PreviewCommentJob.php +++ b/src/Api/Jobs/PreviewCommentJob.php @@ -8,13 +8,13 @@ class PreviewCommentJob extends AbstractJob if ($this->hasArgument(self::POST_ID)) { - $post = PostModel::findById($this->getArgument(self::POST_ID)); + $post = PostModel::getById($this->getArgument(self::POST_ID)); $comment = CommentModel::spawn(); $comment->setPost($post); } else { - $comment = CommentModel::findById($this->getArgument(self::COMMENT_ID)); + $comment = CommentModel::getById($this->getArgument(self::COMMENT_ID)); } $comment->setCommenter($user); diff --git a/src/Api/Jobs/TogglePostTagJob.php b/src/Api/Jobs/TogglePostTagJob.php index 577f2e61..9e851c63 100644 --- a/src/Api/Jobs/TogglePostTagJob.php +++ b/src/Api/Jobs/TogglePostTagJob.php @@ -11,7 +11,7 @@ class TogglePostTagJob extends AbstractPostJob if ($enable) { - $tag = TagModel::findByName($tagName, false); + $tag = TagModel::tryGetByName($tagName); if ($tag === null) { $tag = TagModel::spawn(); diff --git a/src/Auth.php b/src/Auth.php index c5ded64a..604d4d1b 100644 --- a/src/Auth.php +++ b/src/Auth.php @@ -13,7 +13,7 @@ class Auth $config = getConfig(); $context = getContext(); - $user = UserModel::findByNameOrEmail($name, false); + $user = UserModel::tryGetByNameOrEmail($name); if ($user === null) throw new SimpleException('Invalid username'); diff --git a/src/Controllers/CommentController.php b/src/Controllers/CommentController.php index dc380ff3..9a0e9519 100644 --- a/src/Controllers/CommentController.php +++ b/src/Controllers/CommentController.php @@ -38,7 +38,7 @@ class CommentController public function editView($id) { - getContext()->transport->comment = CommentModel::findById($id); + getContext()->transport->comment = CommentModel::getById($id); } public function editAction($id) diff --git a/src/Controllers/PostController.php b/src/Controllers/PostController.php index c7138cc8..82895bff 100644 --- a/src/Controllers/PostController.php +++ b/src/Controllers/PostController.php @@ -69,7 +69,7 @@ class PostController { Access::assert(new Privilege( Privilege::MassTag, - Access::getIdentity(PostModel::findById($id)->getUploader()))); + Access::getIdentity(PostModel::getById($id)->getUploader()))); Api::run( new TogglePostTagJob(), @@ -121,7 +121,7 @@ class PostController public function editAction($id) { - $post = PostModel::findByIdOrName($id); + $post = PostModel::getByIdOrName($id); $editToken = InputHelper::get('edit-token'); if ($editToken != $post->getEditToken()) diff --git a/src/Controllers/StaticPagesController.php b/src/Controllers/StaticPagesController.php index c98d5dec..c95fb694 100644 --- a/src/Controllers/StaticPagesController.php +++ b/src/Controllers/StaticPagesController.php @@ -12,7 +12,7 @@ class StaticPagesController { $context->featuredPost = $featuredPost; $context->featuredPostDate = PropertyModel::get(PropertyModel::FeaturedPostDate); - $context->featuredPostUser = UserModel::findByNameOrEmail( + $context->featuredPostUser = UserModel::getByNameOrEmail( PropertyModel::get(PropertyModel::FeaturedPostUserName), false); } @@ -48,7 +48,7 @@ class StaticPagesController return PropertyModel::featureNewPost(); //check if post was deleted - $featuredPost = PostModel::findById($featuredPostId, false); + $featuredPost = PostModel::tryGetById($featuredPostId); if (!$featuredPost) return PropertyModel::featureNewPost(); diff --git a/src/Controllers/UserController.php b/src/Controllers/UserController.php index fc2b0c58..d19a0d17 100644 --- a/src/Controllers/UserController.php +++ b/src/Controllers/UserController.php @@ -135,7 +135,7 @@ class UserController Api::run(new DeleteUserJob(), [ DeleteUserJob::USER_NAME => $name]); - $user = UserModel::findById(Auth::getCurrentUser()->getId(), false); + $user = UserModel::tryGetById(Auth::getCurrentUser()->getId()); if (!$user) Auth::logOut(); diff --git a/src/Models/AbstractCrudModel.php b/src/Models/AbstractCrudModel.php index c7173a26..2afd85fa 100644 --- a/src/Models/AbstractCrudModel.php +++ b/src/Models/AbstractCrudModel.php @@ -23,8 +23,15 @@ abstract class AbstractCrudModel implements IModel } + public static function getById($key) + { + $ret = self::tryGetById($key); + if (!$ret) + throw new SimpleNotFoundException('Invalid %s ID "%s"', static::getTableName(), $key); + return $ret; + } - public static function findById($key, $throw = true) + public static function tryGetById($key) { $stmt = new Sql\SelectStatement(); $stmt->setColumn('*'); @@ -32,15 +39,12 @@ abstract class AbstractCrudModel implements IModel $stmt->setCriterion(new Sql\EqualsFunctor('id', new Sql\Binding($key))); $row = Database::fetchOne($stmt); - if ($row) - return static::convertRow($row); - - if ($throw) - throw new SimpleNotFoundException('Invalid %s ID "%s"', static::getTableName(), $key); - return null; + return $row + ? static::convertRow($row) + : null; } - public static function findByIds(array $ids) + public static function getAllByIds(array $ids) { $stmt = new Sql\SelectStatement(); $stmt->setColumn('*'); diff --git a/src/Models/CommentModel.php b/src/Models/CommentModel.php index b6ce3b11..9b378d7c 100644 --- a/src/Models/CommentModel.php +++ b/src/Models/CommentModel.php @@ -56,7 +56,7 @@ final class CommentModel extends AbstractCrudModel - public static function findAllByPostId($key) + public static function getAllByPostId($key) { $stmt = new Sql\SelectStatement(); $stmt->setColumn('comment.*'); @@ -76,7 +76,7 @@ final class CommentModel extends AbstractCrudModel self::preloadOneToMany($comments, function($comment) { return $comment->getCommenterId(); }, function($user) { return $user->getId(); }, - function($userIds) { return UserModel::findByIds($userIds); }, + function($userIds) { return UserModel::getAllByIds($userIds); }, function($comment, $user) { return $comment->setCache('commenter', $user); }); } @@ -85,7 +85,7 @@ final class CommentModel extends AbstractCrudModel self::preloadOneToMany($comments, function($comment) { return $comment->getPostId(); }, function($post) { return $post->getId(); }, - function($postIds) { return PostModel::findByIds($postIds); }, + function($postIds) { return PostModel::getAllByIds($postIds); }, function($comment, $post) { $comment->setCache('post', $post); }); } } diff --git a/src/Models/Entities/CommentEntity.php b/src/Models/Entities/CommentEntity.php index f3c8080e..3b9e9d4c 100644 --- a/src/Models/Entities/CommentEntity.php +++ b/src/Models/Entities/CommentEntity.php @@ -45,7 +45,7 @@ final class CommentEntity extends AbstractEntity implements IValidatable { if ($this->hasCache('post')) return $this->getCache('post'); - $post = PostModel::findById($this->getPostId()); + $post = PostModel::getById($this->getPostId()); $this->setCache('post', $post); return $post; } @@ -77,7 +77,7 @@ final class CommentEntity extends AbstractEntity implements IValidatable return $this->getCache('commenter'); if (!$this->commenterId) return null; - $user = UserModel::findById($this->commenterId, false); + $user = UserModel::tryGetById($this->commenterId); $this->setCache('commenter', $user); return $user; } diff --git a/src/Models/Entities/PostEntity.php b/src/Models/Entities/PostEntity.php index 958ccfbc..38335641 100644 --- a/src/Models/Entities/PostEntity.php +++ b/src/Models/Entities/PostEntity.php @@ -45,7 +45,7 @@ class PostEntity extends AbstractEntity implements IValidatable return $this->getCache('uploader'); if (!$this->uploaderId) return null; - $uploader = UserModel::findById($this->uploaderId, false); + $uploader = UserModel::tryGetById($this->uploaderId); $this->setCache('uploader', $uploader); return $uploader; } @@ -65,7 +65,7 @@ class PostEntity extends AbstractEntity implements IValidatable { if ($this->hasCache('comments')) return $this->getCache('comments'); - $comments = CommentModel::findAllByPostId($this->getId()); + $comments = CommentModel::getAllByPostId($this->getId()); $this->setCache('comments', $comments); return $comments; } @@ -139,7 +139,7 @@ class PostEntity extends AbstractEntity implements IValidatable $config->browsing->maxRelatedPosts); } - $relatedPosts []= PostModel::findById($relatedId); + $relatedPosts []= PostModel::getById($relatedId); } $this->setRelations($relatedPosts); @@ -149,7 +149,7 @@ class PostEntity extends AbstractEntity implements IValidatable { if ($this->hasCache('tags')) return $this->getCache('tags'); - $tags = TagModel::findAllByPostId($this->getId()); + $tags = TagModel::getAllByPostId($this->getId()); $this->setCache('tags', $tags); return $tags; } @@ -400,7 +400,7 @@ class PostEntity extends AbstractEntity implements IValidatable throw new SimpleException('Invalid file type "%s"', $this->getMimeType()); } - $duplicatedPost = PostModel::findByHash($this->getFileHash(), false); + $duplicatedPost = PostModel::tryGetByHash($this->getFileHash()); if ($duplicatedPost !== null and (!$this->getId() or $this->getId() != $duplicatedPost->getId())) { throw new SimpleException( @@ -438,7 +438,7 @@ class PostEntity extends AbstractEntity implements IValidatable if (file_exists($thumbPath)) unlink($thumbPath); - $duplicatedPost = PostModel::findByHash($youtubeId, false); + $duplicatedPost = PostModel::tryGetByHash($youtubeId); if ($duplicatedPost !== null and (!$this->getId() or $this->getId() != $duplicatedPost->getId())) { throw new SimpleException( diff --git a/src/Models/Entities/TokenEntity.php b/src/Models/Entities/TokenEntity.php index 867cc21e..37d31449 100644 --- a/src/Models/Entities/TokenEntity.php +++ b/src/Models/Entities/TokenEntity.php @@ -43,7 +43,7 @@ class TokenEntity extends AbstractEntity implements IValidatable public function getUser() { - return UserModel::findById($this->userId); + return UserModel::getById($this->userId); } public function getUserId() diff --git a/src/Models/Entities/UserEntity.php b/src/Models/Entities/UserEntity.php index dd121a22..f4242e54 100644 --- a/src/Models/Entities/UserEntity.php +++ b/src/Models/Entities/UserEntity.php @@ -38,7 +38,7 @@ class UserEntity extends AbstractEntity implements IValidatable $userName = $this->getName(); $config = getConfig(); - $otherUser = UserModel::findByName($userName, false); + $otherUser = UserModel::tryGetByName($userName); if ($otherUser !== null and $otherUser->getId() != $this->getId()) { if (!$otherUser->getConfirmedEmail() diff --git a/src/Models/PostModel.php b/src/Models/PostModel.php index 96edcb4b..62a33ef5 100644 --- a/src/Models/PostModel.php +++ b/src/Models/PostModel.php @@ -145,7 +145,15 @@ class PostModel extends AbstractCrudModel - public static function findByName($key, $throw = true) + public static function getByName($key) + { + $ret = self::tryGetByName($key); + if (!$ret) + throw new SimpleNotFoundException('Invalid post name "%s"', $key); + return $ret; + } + + public static function tryGetByName($key, $throw = true) { $stmt = new Sql\SelectStatement(); $stmt->setColumn('*'); @@ -153,24 +161,29 @@ class PostModel extends AbstractCrudModel $stmt->setCriterion(new Sql\EqualsFunctor('name', new Sql\Binding($key))); $row = Database::fetchOne($stmt); - if ($row) - return self::convertRow($row); - - if ($throw) - throw new SimpleNotFoundException('Invalid post name "%s"', $key); - return null; + return $row + ? self::convertRow($row) + : null; } - public static function findByIdOrName($key, $throw = true) + public static function getByIdOrName($key) { if (is_numeric($key)) - $post = self::findById($key, $throw); + $post = self::getById($key); else - $post = self::findByName($key, $throw); + $post = self::getByName($key); return $post; } - public static function findByHash($key, $throw = true) + public static function getByHash($key) + { + $ret = self::tryGetByHash($key); + if (!$ret) + throw new SimpleNotFoundException('Invalid post hash "%s"', $hash); + return $ret; + } + + public static function tryGetByHash($key) { $stmt = new Sql\SelectStatement(); $stmt->setColumn('*'); @@ -178,12 +191,9 @@ class PostModel extends AbstractCrudModel $stmt->setCriterion(new Sql\EqualsFunctor('file_hash', new Sql\Binding($key))); $row = Database::fetchOne($stmt); - if ($row) - return self::convertRow($row); - - if ($throw) - throw new SimpleNotFoundException('Invalid post hash "%s"', $hash); - return null; + return $row + ? self::convertRow($row) + : null; } diff --git a/src/Models/PropertyModel.php b/src/Models/PropertyModel.php index 9c6319c5..5484871c 100644 --- a/src/Models/PropertyModel.php +++ b/src/Models/PropertyModel.php @@ -85,6 +85,6 @@ class PropertyModel implements IModel self::set(self::FeaturedPostId, $featuredPostId); self::set(self::FeaturedPostDate, time()); self::set(self::FeaturedPostUserName, null); - return PostModel::findById($featuredPostId); + return PostModel::getById($featuredPostId); } } diff --git a/src/Models/SearchParsers/PostSearchParser.php b/src/Models/SearchParsers/PostSearchParser.php index e4e018ad..a8ba0d5a 100644 --- a/src/Models/SearchParsers/PostSearchParser.php +++ b/src/Models/SearchParsers/PostSearchParser.php @@ -38,7 +38,7 @@ class PostSearchParser extends AbstractSearchParser foreach ($this->tags as $item) { list ($tagName, $neg) = $item; - $tag = TagModel::findByName($tagName); + $tag = TagModel::getByName($tagName); $innerStmt = new Sql\SelectStatement(); $innerStmt->setTable('post_tag'); $innerStmt->setCriterion((new Sql\ConjunctionFunctor) @@ -73,7 +73,7 @@ class PostSearchParser extends AbstractSearchParser elseif (in_array($key, ['fav', 'favs', 'favd'])) { - $user = UserModel::findByNameOrEmail($value); + $user = UserModel::getByNameOrEmail($value); $innerStmt = (new Sql\SelectStatement) ->setTable('favoritee') ->setCriterion((new Sql\ConjunctionFunctor) @@ -84,7 +84,7 @@ class PostSearchParser extends AbstractSearchParser elseif (in_array($key, ['comment', 'comments', 'commenter', 'commented'])) { - $user = UserModel::findByNameOrEmail($value); + $user = UserModel::getByNameOrEmail($value); $innerStmt = (new Sql\SelectStatement) ->setTable('comment') ->setCriterion((new Sql\ConjunctionFunctor) @@ -95,7 +95,7 @@ class PostSearchParser extends AbstractSearchParser elseif (in_array($key, ['submit', 'upload', 'uploads', 'uploader', 'uploaded'])) { - $user = UserModel::findByNameOrEmail($value); + $user = UserModel::getByNameOrEmail($value); return new Sql\EqualsFunctor('post.uploader_id', new Sql\Binding($user->getId())); } diff --git a/src/Models/SearchServices/TagSearchService.php b/src/Models/SearchServices/TagSearchService.php index 04c1a059..4c447c6b 100644 --- a/src/Models/SearchServices/TagSearchService.php +++ b/src/Models/SearchServices/TagSearchService.php @@ -11,7 +11,7 @@ class TagSearchService extends AbstractSearchService public static function getRelatedTags($parentTagName) { - $parentTagEntity = TagModel::findByName($parentTagName, false); + $parentTagEntity = TagModel::tryGetByName($parentTagName); if (empty($parentTagEntity)) return []; $parentTagId = $parentTagEntity->getId(); diff --git a/src/Models/TagModel.php b/src/Models/TagModel.php index aa6fa0d3..f8b0925e 100644 --- a/src/Models/TagModel.php +++ b/src/Models/TagModel.php @@ -55,8 +55,8 @@ class TagModel extends AbstractCrudModel { Database::transaction(function() use ($sourceName, $targetName) { - $sourceTag = TagModel::findByName($sourceName); - $targetTag = TagModel::findByName($targetName, false); + $sourceTag = TagModel::getByName($sourceName); + $targetTag = TagModel::tryGetByName($targetName); if ($targetTag and $targetTag->getId() != $sourceTag->getId()) throw new SimpleException('Target tag already exists'); @@ -71,8 +71,8 @@ class TagModel extends AbstractCrudModel { Database::transaction(function() use ($sourceName, $targetName) { - $sourceTag = TagModel::findByName($sourceName); - $targetTag = TagModel::findByName($targetName); + $sourceTag = TagModel::getByName($sourceName); + $targetTag = TagModel::getByName($targetName); if ($sourceTag->getId() == $targetTag->getId()) throw new SimpleException('Source and target tag are the same'); @@ -116,7 +116,7 @@ class TagModel extends AbstractCrudModel } - public static function findAllByPostId($key) + public static function getAllByPostId($key) { $stmt = new Sql\SelectStatement(); $stmt->setColumn('tag.*'); @@ -130,7 +130,15 @@ class TagModel extends AbstractCrudModel return []; } - public static function findByName($key, $throw = true) + public static function getByName($key) + { + $ret = self::tryGetByName($key); + if (!$ret) + throw new SimpleNotFoundException('Invalid tag name "%s"', $key); + return $ret; + } + + public static function tryGetByName($key) { $stmt = new Sql\SelectStatement(); $stmt->setColumn('tag.*'); @@ -138,12 +146,9 @@ class TagModel extends AbstractCrudModel $stmt->setCriterion(new Sql\NoCaseFunctor(new Sql\EqualsFunctor('name', new Sql\Binding($key)))); $row = Database::fetchOne($stmt); - if ($row) - return self::convertRow($row); - - if ($throw) - throw new SimpleNotFoundException('Invalid tag name "%s"', $key); - return null; + return $row + ? self::convertRow($row) + : null; } @@ -166,7 +171,7 @@ class TagModel extends AbstractCrudModel $tags = []; foreach ($tagNames as $tagName) { - $tag = TagModel::findByName($tagName, false); + $tag = TagModel::tryGetByName($tagName); if (!$tag) { $tag = TagModel::spawn(); diff --git a/src/Models/TokenModel.php b/src/Models/TokenModel.php index 921855f2..6e13476b 100644 --- a/src/Models/TokenModel.php +++ b/src/Models/TokenModel.php @@ -37,7 +37,15 @@ class TokenModel extends AbstractCrudModel return $token; } - public static function findByToken($key, $throw = true) + public static function getByToken($key) + { + $ret = self::tryGetByToken($key); + if (!$ret) + throw new SimpleNotFoundException('No user with such security token'); + return $ret; + } + + public static function tryGetByToken($key) { if (empty($key)) throw new SimpleNotFoundException('Invalid security token'); @@ -48,12 +56,9 @@ class TokenModel extends AbstractCrudModel $stmt->setCriterion(new Sql\EqualsFunctor('token', new Sql\Binding($key))); $row = Database::fetchOne($stmt); - if ($row) - return self::convertRow($row); - - if ($throw) - throw new SimpleNotFoundException('No user with such security token'); - return null; + return $row + ? self::convertRow($row) + : null; } public static function checkValidity($token) @@ -74,7 +79,7 @@ class TokenModel extends AbstractCrudModel while (true) { $tokenText = md5(mt_rand() . uniqid()); - $token = self::findByToken($tokenText, false); + $token = self::tryGetByToken($tokenText); if (!$token) return $tokenText; } diff --git a/src/Models/UserModel.php b/src/Models/UserModel.php index abe6cd52..39f1f7c4 100644 --- a/src/Models/UserModel.php +++ b/src/Models/UserModel.php @@ -101,7 +101,15 @@ class UserModel extends AbstractCrudModel - public static function findByName($key, $throw = true) + public static function getByName($key) + { + $ret = self::tryGetByName($key); + if (!$ret) + throw new SimpleNotFoundException('Invalid user name "%s"', $key); + return $ret; + } + + public static function tryGetByName($key) { $stmt = new Sql\SelectStatement(); $stmt->setColumn('*'); @@ -109,15 +117,20 @@ class UserModel extends AbstractCrudModel $stmt->setCriterion(new Sql\NoCaseFunctor(new Sql\EqualsFunctor('name', new Sql\Binding(trim($key))))); $row = Database::fetchOne($stmt); - if ($row) - return self::convertRow($row); - - if ($throw) - throw new SimpleNotFoundException('Invalid user name "%s"', $key); - return null; + return $row + ? self::convertRow($row) + : null; } - public static function findByNameOrEmail($key, $throw = true) + public static function getByNameOrEmail($key) + { + $ret = self::tryGetByNameOrEmail($key); + if (!$ret) + throw new SimpleNotFoundException('Invalid user name "%s"', $key); + return $ret; + } + + public static function tryGetByNameOrEmail($key) { $key = trim($key); @@ -130,12 +143,9 @@ class UserModel extends AbstractCrudModel ->add(new Sql\NoCaseFunctor(new Sql\EqualsFunctor('email_confirmed', new Sql\Binding($key))))); $row = Database::fetchOne($stmt); - if ($row) - return self::convertRow($row); - - if ($throw) - throw new SimpleNotFoundException('Invalid user name "%s"', $key); - return null; + return $row + ? self::convertRow($row) + : null; } diff --git a/tests/JobTests/ActivateUserEmailJobTest.php b/tests/JobTests/ActivateUserEmailJobTest.php index a326f59c..94160e7a 100644 --- a/tests/JobTests/ActivateUserEmailJobTest.php +++ b/tests/JobTests/ActivateUserEmailJobTest.php @@ -25,7 +25,7 @@ class ActivateUserEmailJobTest extends AbstractTest $tokens = Mailer::getMailsSent()[0]->tokens; $tokenText = $tokens['token']; - $token = TokenModel::findByToken($tokenText); + $token = TokenModel::getByToken($tokenText); $this->assert->areEqual($user->getId(), $token->getUser()->getId()); $this->assert->isTrue(strpos($tokens['link'], $tokenText) !== false); @@ -66,7 +66,7 @@ class ActivateUserEmailJobTest extends AbstractTest }); //reload local entity after changes done by the job - $user = UserModel::findById($user->getId()); + $user = UserModel::getById($user->getId()); $this->assert->areEqual(null, $user->getUnconfirmedEmail()); $this->assert->areEqual('godzilla@whitestar.gov', $user->getConfirmedEmail()); @@ -139,8 +139,8 @@ class ActivateUserEmailJobTest extends AbstractTest $token2text = $tokens2['token']; $this->assert->areNotEqual($token1text, $token2text); - $token1 = TokenModel::findByToken($token1text); - $token2 = TokenModel::findByToken($token2text); + $token1 = TokenModel::getByToken($token1text); + $token2 = TokenModel::getByToken($token2text); $this->assert->areEqual($user1->getId(), $token1->getUser()->getId()); $this->assert->areEqual($user2->getId(), $token2->getUser()->getId()); diff --git a/tests/JobTests/AddCommentJobTest.php b/tests/JobTests/AddCommentJobTest.php index ac53abd0..e06deeae 100644 --- a/tests/JobTests/AddCommentJobTest.php +++ b/tests/JobTests/AddCommentJobTest.php @@ -20,7 +20,7 @@ class AddCommentJobTest extends AbstractTest $this->assert->isNotNull($comment->getCreationTime()); $this->assert->doesNotThrow(function() use ($comment) { - CommentModel::findById($comment->getId()); + CommentModel::getById($comment->getId()); }); } diff --git a/tests/JobTests/AddUserJobTest.php b/tests/JobTests/AddUserJobTest.php index 97d49f83..ddc55492 100644 --- a/tests/JobTests/AddUserJobTest.php +++ b/tests/JobTests/AddUserJobTest.php @@ -298,8 +298,8 @@ class AddUserJobTest extends AbstractTest $token2text = Mailer::getMailsSent()[1]->tokens['token']; $this->assert->areNotEqual($token1text, $token2text); - $token1 = TokenModel::findByToken($token1text); - $token2 = TokenModel::findByToken($token2text); + $token1 = TokenModel::getByToken($token1text); + $token2 = TokenModel::getByToken($token2text); $this->assert->areEqual($user1->getId(), $token1->getUser()->getId()); $this->assert->areEqual($user2->getId(), $token2->getUser()->getId()); diff --git a/tests/JobTests/EditCommentJobTest.php b/tests/JobTests/EditCommentJobTest.php index 7ab4831b..492396f6 100644 --- a/tests/JobTests/EditCommentJobTest.php +++ b/tests/JobTests/EditCommentJobTest.php @@ -18,7 +18,7 @@ class EditCommentJobTest extends AbstractTest $this->assert->isNotNull($comment->getCreationTime()); $this->assert->doesNotThrow(function() use ($comment) { - CommentModel::findById($comment->getId()); + CommentModel::getById($comment->getId()); }); } diff --git a/tests/JobTests/EditPostContentJobTest.php b/tests/JobTests/EditPostContentJobTest.php index 70977db0..bb1234b4 100644 --- a/tests/JobTests/EditPostContentJobTest.php +++ b/tests/JobTests/EditPostContentJobTest.php @@ -8,7 +8,7 @@ class EditPostContentJobTest extends AbstractTest $post = $this->uploadFromFile('image.jpg'); $this->assert->doesNotThrow(function() use ($post) { - PostModel::findById($post->getId()); + PostModel::getById($post->getId()); }); } @@ -74,7 +74,7 @@ class EditPostContentJobTest extends AbstractTest $post = $this->uploadFromUrl('image.jpg'); $this->assert->doesNotThrow(function() use ($post) { - PostModel::findById($post->getId()); + PostModel::getById($post->getId()); }); } @@ -99,7 +99,7 @@ class EditPostContentJobTest extends AbstractTest $this->assert->doesNotThrow(function() use ($post) { - PostModel::findById($post->getId()); + PostModel::getById($post->getId()); }); } diff --git a/tests/JobTests/EditPostSourceJobTest.php b/tests/JobTests/EditPostSourceJobTest.php index 38b1deb8..f4f5556b 100644 --- a/tests/JobTests/EditPostSourceJobTest.php +++ b/tests/JobTests/EditPostSourceJobTest.php @@ -13,7 +13,7 @@ class EditPostSourceJobTest extends AbstractTest $this->assert->areEqual('a', $post->getSource()); $this->assert->doesNotThrow(function() use ($post) { - PostModel::findById($post->getId()); + PostModel::getById($post->getId()); }); } diff --git a/tests/JobTests/ListCommentsJobTest.php b/tests/JobTests/ListCommentsJobTest.php index 4735c162..c94ad58a 100644 --- a/tests/JobTests/ListCommentsJobTest.php +++ b/tests/JobTests/ListCommentsJobTest.php @@ -26,7 +26,7 @@ class ListCommentJobTest extends AbstractTest $post = $ret->entities[0]; $samePost = $this->assert->doesNotThrow(function() use ($post) { - return PostModel::findById($post->getId()); + return PostModel::getById($post->getId()); }); //posts retrieved via ListCommentsJob should already have cached its comments $this->assert->areNotEquivalent($post, $samePost); diff --git a/tests/JobTests/PreviewCommentJobTest.php b/tests/JobTests/PreviewCommentJobTest.php index 13a92a1e..03a74f59 100644 --- a/tests/JobTests/PreviewCommentJobTest.php +++ b/tests/JobTests/PreviewCommentJobTest.php @@ -17,7 +17,7 @@ class PreviewCommentJobTest extends AbstractTest $this->assert->isNotNull($comment->getCreationTime()); $this->assert->throws(function() use ($comment) { - CommentModel::findById($comment->getId()); + CommentModel::getById($comment->getId()); }, 'Invalid comment ID'); }