From 16942d9d1993ba6f3b05689f0c044ce4ed609160 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Wed, 7 May 2014 20:33:52 +0200 Subject: [PATCH] Continued work on getter/setters: timestamps --- src/Api/Jobs/AddCommentJob.php | 2 +- src/Api/Jobs/AddUserJob.php | 2 +- src/Api/Jobs/EditCommentJob.php | 2 +- src/Api/Jobs/PreviewCommentJob.php | 2 +- src/Mailer.php | 2 +- src/Models/CommentModel.php | 4 ++-- src/Models/Entities/CommentEntity.php | 8 ++++---- src/Models/Entities/PostEntity.php | 13 +++++++++++- src/Models/Entities/TokenEntity.php | 6 +++--- src/Models/Entities/UserEntity.php | 26 +++++++++++++++++++++--- src/Models/PostModel.php | 4 ++-- src/Models/TokenModel.php | 4 ++-- src/Models/UserModel.php | 4 ++-- src/Views/comment-small.phtml | 4 ++-- src/Views/post-view.phtml | 4 ++-- src/Views/user-list.phtml | 4 ++-- src/Views/user-view.phtml | 8 ++++---- tests/JobTests/AddCommentJobTest.php | 2 +- tests/JobTests/EditCommentJobTest.php | 2 +- tests/JobTests/PreviewCommentJobTest.php | 2 +- 20 files changed, 68 insertions(+), 37 deletions(-) diff --git a/src/Api/Jobs/AddCommentJob.php b/src/Api/Jobs/AddCommentJob.php index f6dc4fb7..8d43e8ac 100644 --- a/src/Api/Jobs/AddCommentJob.php +++ b/src/Api/Jobs/AddCommentJob.php @@ -10,7 +10,7 @@ class AddCommentJob extends AbstractJob $comment = CommentModel::spawn(); $comment->setCommenter($user); $comment->setPost($post); - $comment->setDateTime(time()); + $comment->setCreationTime(time()); $comment->setText($text); CommentModel::save($comment); diff --git a/src/Api/Jobs/AddUserJob.php b/src/Api/Jobs/AddUserJob.php index b9068fe5..479649a9 100644 --- a/src/Api/Jobs/AddUserJob.php +++ b/src/Api/Jobs/AddUserJob.php @@ -6,7 +6,7 @@ class AddUserJob extends AbstractJob $firstUser = UserModel::getCount() == 0; $user = UserModel::spawn(); - $user->joinDate = time(); + $user->setJoinTime(time()); $user->staffConfirmed = $firstUser; UserModel::forgeId($user); diff --git a/src/Api/Jobs/EditCommentJob.php b/src/Api/Jobs/EditCommentJob.php index 5531bfd3..afde3e5d 100644 --- a/src/Api/Jobs/EditCommentJob.php +++ b/src/Api/Jobs/EditCommentJob.php @@ -12,7 +12,7 @@ class EditCommentJob extends AbstractJob { $comment = $this->comment; - $comment->setDateTime(time()); + $comment->setCreationTime(time()); $comment->setText($this->getArgument(self::TEXT)); CommentModel::save($comment); diff --git a/src/Api/Jobs/PreviewCommentJob.php b/src/Api/Jobs/PreviewCommentJob.php index 32363e1b..8c901432 100644 --- a/src/Api/Jobs/PreviewCommentJob.php +++ b/src/Api/Jobs/PreviewCommentJob.php @@ -18,7 +18,7 @@ class PreviewCommentJob extends AbstractJob } $comment->setCommenter($user); - $comment->setDateTime(time()); + $comment->setCreationTime(time()); $comment->setText($text); $comment->validate(); diff --git a/src/Mailer.php b/src/Mailer.php index 244e6771..f8fee65b 100644 --- a/src/Mailer.php +++ b/src/Mailer.php @@ -90,7 +90,7 @@ class Mailer $token->setUser($user); $token->setText(TokenModel::forgeUnusedToken()); $token->setUsed(false); - $token->setExpirationDate(null); + $token->setExpirationTime(null); TokenModel::save($token); $tokens['link'] = \Chibi\Router::linkTo($linkDestination, ['tokenText' => $token->getText()]); diff --git a/src/Models/CommentModel.php b/src/Models/CommentModel.php index 41e94aa1..b6ce3b11 100644 --- a/src/Models/CommentModel.php +++ b/src/Models/CommentModel.php @@ -12,7 +12,7 @@ final class CommentModel extends AbstractCrudModel public static function spawn() { $comment = new CommentEntity; - $comment->setDateTime(time()); + $comment->setCreationTime(time()); return $comment; } @@ -27,7 +27,7 @@ final class CommentModel extends AbstractCrudModel $bindings = [ 'text' => $comment->getText(), 'post_id' => $comment->getPostId(), - 'comment_date' => $comment->getDateTime(), + 'comment_date' => $comment->getCreationTime(), 'commenter_id' => $comment->getCommenterId()]; $stmt = new Sql\UpdateStatement(); diff --git a/src/Models/Entities/CommentEntity.php b/src/Models/Entities/CommentEntity.php index 8eebddb2..f3c8080e 100644 --- a/src/Models/Entities/CommentEntity.php +++ b/src/Models/Entities/CommentEntity.php @@ -20,7 +20,7 @@ final class CommentEntity extends AbstractEntity implements IValidatable if (!$this->getPostId()) throw new SimpleException('Trying to save comment that doesn\'t refer to any post'); - if (!$this->getDateTime()) + if (!$this->getCreationTime()) throw new SimpleException('Trying to save comment that has no creation date specified'); $this->setText($text); @@ -61,14 +61,14 @@ final class CommentEntity extends AbstractEntity implements IValidatable $this->postId = $post->getId(); } - public function getDateTime() + public function getCreationTime() { return $this->commentDate; } - public function setDateTime($dateTime) + public function setCreationTime($unixTime) { - $this->commentDate = $dateTime; + $this->commentDate = $unixTime; } public function getCommenter() diff --git a/src/Models/Entities/PostEntity.php b/src/Models/Entities/PostEntity.php index affebc96..3603eb0f 100644 --- a/src/Models/Entities/PostEntity.php +++ b/src/Models/Entities/PostEntity.php @@ -12,11 +12,12 @@ class PostEntity extends AbstractEntity implements IValidatable protected $mimeType; protected $safety; protected $hidden; - public $uploadDate; + protected $uploadDate; protected $imageWidth; protected $imageHeight; protected $uploaderId; protected $source; + public $commentCount = 0; public $favCount = 0; public $score = 0; @@ -199,6 +200,16 @@ class PostEntity extends AbstractEntity implements IValidatable $this->hidden = boolval($hidden); } + public function getCreationTime() + { + return $this->uploadDate; + } + + public function setCreationTime($unixTime) + { + $this->uploadDate = $unixTime; + } + public function getImageWidth() { return $this->imageWidth; diff --git a/src/Models/Entities/TokenEntity.php b/src/Models/Entities/TokenEntity.php index f0f6a899..867cc21e 100644 --- a/src/Models/Entities/TokenEntity.php +++ b/src/Models/Entities/TokenEntity.php @@ -31,14 +31,14 @@ class TokenEntity extends AbstractEntity implements IValidatable $this->used = $used; } - public function getExpirationDate() + public function getExpirationTime() { return $this->expires; } - public function setExpirationDate($time) + public function setExpirationTime($unixTime) { - $this->expires = $time; + $this->expires = $unixTime; } public function getUser() diff --git a/src/Models/Entities/UserEntity.php b/src/Models/Entities/UserEntity.php index 24d3d3e2..3794fe05 100644 --- a/src/Models/Entities/UserEntity.php +++ b/src/Models/Entities/UserEntity.php @@ -10,8 +10,8 @@ class UserEntity extends AbstractEntity implements IValidatable public $staffConfirmed; protected $emailUnconfirmed; protected $emailConfirmed; - public $joinDate; - public $lastLoginDate; + protected $joinDate; + protected $lastLoginDate; protected $accessRank; public $settings; protected $banned = false; @@ -33,7 +33,6 @@ class UserEntity extends AbstractEntity implements IValidatable throw new Exception('Trying to save anonymous user into database'); } - protected function validateUserName() { $userName = $this->getName(); @@ -103,6 +102,7 @@ class UserEntity extends AbstractEntity implements IValidatable return $accessRank; } + public function isBanned() { return $this->banned; @@ -128,6 +128,26 @@ class UserEntity extends AbstractEntity implements IValidatable $this->name = trim($name); } + public function getJoinTime() + { + return $this->joinDate; + } + + public function setJoinTime($unixTime) + { + $this->joinDate = $unixTime; + } + + public function getLastLoginTime() + { + return $this->lastLoginDate; + } + + public function setLastLoginTime($unixTime) + { + $this->lastLoginDate = $unixTime; + } + public function getUnconfirmedEmail() { return $this->emailUnconfirmed; diff --git a/src/Models/PostModel.php b/src/Models/PostModel.php index 13877299..96edcb4b 100644 --- a/src/Models/PostModel.php +++ b/src/Models/PostModel.php @@ -27,7 +27,7 @@ class PostModel extends AbstractCrudModel $post = new PostEntity; $post->setSafety(new PostSafety(PostSafety::Safe)); $post->setHidden(false); - $post->uploadDate = time(); + $post->setCreationTime(time()); do { $post->setName(md5(mt_rand() . uniqid())); @@ -53,7 +53,7 @@ class PostModel extends AbstractCrudModel 'mime_type' => $post->getMimeType(), 'safety' => $post->getSafety()->toInteger(), 'hidden' => $post->isHidden(), - 'upload_date' => $post->uploadDate, + 'upload_date' => $post->getCreationTime(), 'image_width' => $post->getImageWidth(), 'image_height' => $post->getImageHeight(), 'uploader_id' => $post->getUploaderId(), diff --git a/src/Models/TokenModel.php b/src/Models/TokenModel.php index 18577b33..921855f2 100644 --- a/src/Models/TokenModel.php +++ b/src/Models/TokenModel.php @@ -21,7 +21,7 @@ class TokenModel extends AbstractCrudModel 'user_id' => $token->getUserId(), 'token' => $token->getText(), 'used' => $token->isUsed(), - 'expires' => $token->getExpirationDate(), + 'expires' => $token->getExpirationTime(), ]; $stmt = new Sql\UpdateStatement(); @@ -64,7 +64,7 @@ class TokenModel extends AbstractCrudModel if ($token->isUsed()) throw new SimpleException('This token was already used'); - if ($token->getExpirationDate() !== null and time() > $token->getExpirationDate()) + if ($token->getExpirationTime() !== null and time() > $token->getExpirationTime()) throw new SimpleException('This token has expired'); } diff --git a/src/Models/UserModel.php b/src/Models/UserModel.php index 1005669e..f94361f0 100644 --- a/src/Models/UserModel.php +++ b/src/Models/UserModel.php @@ -48,8 +48,8 @@ class UserModel extends AbstractCrudModel 'staff_confirmed' => $user->staffConfirmed, 'email_unconfirmed' => $user->getUnconfirmedEmail(), 'email_confirmed' => $user->getConfirmedEmail(), - 'join_date' => $user->joinDate, - 'last_login_date' => $user->lastLoginDate, + 'join_date' => $user->getJoinTime(), + 'last_login_date' => $user->getLastLoginTime(), 'access_rank' => $user->getAccessRank()->toInteger(), 'settings' => $user->settings, 'banned' => $user->isBanned(), diff --git a/src/Views/comment-small.phtml b/src/Views/comment-small.phtml index e2f52f0f..ef0ac062 100644 --- a/src/Views/comment-small.phtml +++ b/src/Views/comment-small.phtml @@ -32,8 +32,8 @@ Assets::addScript('comment-edit.js'); - - context->comment->getDateTime(), false) ?> + + context->comment->getCreationTime(), false) ?> 0;
- context->transport->post->uploadDate, false) ?> + title="context->transport->post->getCreationTime(), true) ?>"> + context->transport->post->getCreationTime(), false) ?> diff --git a/src/Views/user-list.phtml b/src/Views/user-list.phtml index ae9d8351..cbc06eb5 100644 --- a/src/Views/user-list.phtml +++ b/src/Views/user-list.phtml @@ -56,8 +56,8 @@ if (Auth::getCurrentUser()->hasEnabledEndlessScrolling()) -
- Registered: joinDate, false) ?> +
+ Registered: getJoinTime(), false) ?>
diff --git a/src/Views/user-view.phtml b/src/Views/user-view.phtml index 81d72547..4a59bc08 100644 --- a/src/Views/user-view.phtml +++ b/src/Views/user-view.phtml @@ -23,9 +23,9 @@ Assets::addStylesheet('user-view.css'); Joined: + title="context->transport->user->getJoinTime(), true) ?>"> - context->transport->user->joinDate, false) ?> + context->transport->user->getJoinTime(), false) ?>
@@ -34,9 +34,9 @@ Assets::addStylesheet('user-view.css'); Last login: + title="context->transport->user->getLastLoginTime(), true) ?>"> - context->transport->user->lastLoginDate, false) ?> + context->transport->user->getLastLoginTime(), false) ?>
diff --git a/tests/JobTests/AddCommentJobTest.php b/tests/JobTests/AddCommentJobTest.php index 717b580f..ac53abd0 100644 --- a/tests/JobTests/AddCommentJobTest.php +++ b/tests/JobTests/AddCommentJobTest.php @@ -17,7 +17,7 @@ class AddCommentJobTest extends AbstractTest $this->assert->areEqual($text, $comment->getText()); $this->assert->areEqual(Auth::getCurrentUser()->getId(), $comment->getCommenter()->getId()); $this->assert->areEqual(1, $comment->getPost()->getId()); - $this->assert->isNotNull($comment->getDateTime()); + $this->assert->isNotNull($comment->getCreationTime()); $this->assert->doesNotThrow(function() use ($comment) { CommentModel::findById($comment->getId()); diff --git a/tests/JobTests/EditCommentJobTest.php b/tests/JobTests/EditCommentJobTest.php index 4d0ed7cb..7ab4831b 100644 --- a/tests/JobTests/EditCommentJobTest.php +++ b/tests/JobTests/EditCommentJobTest.php @@ -15,7 +15,7 @@ class EditCommentJobTest extends AbstractTest $this->assert->areEqual($text, $comment->getText()); $this->assert->areEqual(Auth::getCurrentUser()->getId(), $comment->getCommenter()->getId()); $this->assert->areEqual(1, $comment->getPost()->getId()); - $this->assert->isNotNull($comment->getDateTime()); + $this->assert->isNotNull($comment->getCreationTime()); $this->assert->doesNotThrow(function() use ($comment) { CommentModel::findById($comment->getId()); diff --git a/tests/JobTests/PreviewCommentJobTest.php b/tests/JobTests/PreviewCommentJobTest.php index 1909902c..13a92a1e 100644 --- a/tests/JobTests/PreviewCommentJobTest.php +++ b/tests/JobTests/PreviewCommentJobTest.php @@ -14,7 +14,7 @@ class PreviewCommentJobTest extends AbstractTest $this->assert->areEqual(0, CommentModel::getCount()); $this->assert->areEqual($text, $comment->getText()); $this->assert->areEqual(Auth::getCurrentUser()->getId(), $comment->getCommenter()->getId()); - $this->assert->isNotNull($comment->getDateTime()); + $this->assert->isNotNull($comment->getCreationTime()); $this->assert->throws(function() use ($comment) { CommentModel::findById($comment->getId());