From 05a3cf927bb2d91f3d484d538a449f6339bed899 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Mon, 5 May 2014 09:36:08 +0200 Subject: [PATCH] Moved validation to entities --- src/Api/Jobs/AddPostJob.php | 8 -------- src/Models/CommentModel.php | 2 ++ src/Models/Entities/AbstractEntity.php | 2 +- src/Models/Entities/CommentEntity.php | 7 ++++++- src/Models/Entities/PostEntity.php | 9 ++++++++- src/Models/Entities/TagEntity.php | 7 ++++++- src/Models/Entities/TokenEntity.php | 7 ++++++- src/Models/Entities/UserEntity.php | 12 +++++++++++- src/Models/PostModel.php | 2 ++ src/Models/TagModel.php | 2 ++ src/Models/TokenModel.php | 2 ++ src/Models/UserModel.php | 3 +-- 12 files changed, 47 insertions(+), 16 deletions(-) diff --git a/src/Api/Jobs/AddPostJob.php b/src/Api/Jobs/AddPostJob.php index dee29c2c..f9dfb29f 100644 --- a/src/Api/Jobs/AddPostJob.php +++ b/src/Api/Jobs/AddPostJob.php @@ -22,14 +22,6 @@ class AddPostJob extends AbstractJob $arguments[EditPostJob::POST_ENTITY] = $post; Api::run((new EditPostJob)->skipSaving(), $arguments); - // basically means that user didn't specify file nor url - //todo: - //- move this to PostEntity::isValid() - //- create IValidatable interface - //- enforce entity validity upon calling save() in models - if (empty($post->getType())) - throw new SimpleException('No post type detected; upload faled'); - //save to db PostModel::save($post); diff --git a/src/Models/CommentModel.php b/src/Models/CommentModel.php index 74e1a3a3..b9a85b7d 100644 --- a/src/Models/CommentModel.php +++ b/src/Models/CommentModel.php @@ -18,6 +18,8 @@ class CommentModel extends AbstractCrudModel public static function save($comment) { + $comment->validate(); + Database::transaction(function() use ($comment) { self::forgeId($comment); diff --git a/src/Models/Entities/AbstractEntity.php b/src/Models/Entities/AbstractEntity.php index d83d4b13..cf497596 100644 --- a/src/Models/Entities/AbstractEntity.php +++ b/src/Models/Entities/AbstractEntity.php @@ -1,5 +1,5 @@ text); diff --git a/src/Models/Entities/PostEntity.php b/src/Models/Entities/PostEntity.php index b93eecf9..70a137f0 100644 --- a/src/Models/Entities/PostEntity.php +++ b/src/Models/Entities/PostEntity.php @@ -2,7 +2,7 @@ use \Chibi\Sql as Sql; use \Chibi\Database as Database; -class PostEntity extends AbstractEntity +class PostEntity extends AbstractEntity implements IValidatable { protected $type; protected $name; @@ -21,6 +21,13 @@ class PostEntity extends AbstractEntity public $favCount = 0; public $score = 0; + public function validate() + { + //todo + if (empty($this->getType())) + throw new SimpleException('No post type detected'); + } + public function getUploader() { if ($this->hasCache('uploader')) diff --git a/src/Models/Entities/TagEntity.php b/src/Models/Entities/TagEntity.php index 14296944..a8ba5452 100644 --- a/src/Models/Entities/TagEntity.php +++ b/src/Models/Entities/TagEntity.php @@ -2,10 +2,15 @@ use \Chibi\Sql as Sql; use \Chibi\Database as Database; -class TagEntity extends AbstractEntity +class TagEntity extends AbstractEntity implements IValidatable { protected $name; + public function validate() + { + //todo + } + public function setName($name) { $this->name = $name; diff --git a/src/Models/Entities/TokenEntity.php b/src/Models/Entities/TokenEntity.php index 7ed81585..fc22f5f7 100644 --- a/src/Models/Entities/TokenEntity.php +++ b/src/Models/Entities/TokenEntity.php @@ -1,11 +1,16 @@ userId); diff --git a/src/Models/Entities/UserEntity.php b/src/Models/Entities/UserEntity.php index abb296ca..89221c2c 100644 --- a/src/Models/Entities/UserEntity.php +++ b/src/Models/Entities/UserEntity.php @@ -2,7 +2,7 @@ use \Chibi\Sql as Sql; use \Chibi\Database as Database; -class UserEntity extends AbstractEntity +class UserEntity extends AbstractEntity implements IValidatable { protected $name; public $passSalt; @@ -16,6 +16,16 @@ class UserEntity extends AbstractEntity public $settings; protected $banned = false; + public function validate() + { + //todo: add more validation + if (empty($this->getAccessRank())) + throw new SimpleException('No access rank detected'); + + if ($this->getAccessRank()->toInteger() == AccessRank::Anonymous) + throw new Exception('Trying to save anonymous user into database'); + } + public function isBanned() { return $this->banned; diff --git a/src/Models/PostModel.php b/src/Models/PostModel.php index b0e37e52..0046c040 100644 --- a/src/Models/PostModel.php +++ b/src/Models/PostModel.php @@ -45,6 +45,8 @@ class PostModel extends AbstractCrudModel public static function save($post) { + $post->validate(); + Database::transaction(function() use ($post) { self::forgeId($post); diff --git a/src/Models/TagModel.php b/src/Models/TagModel.php index 6bf91420..144428fd 100644 --- a/src/Models/TagModel.php +++ b/src/Models/TagModel.php @@ -19,6 +19,8 @@ class TagModel extends AbstractCrudModel public static function save($tag) { + $tag->validate(); + Database::transaction(function() use ($tag) { self::forgeId($tag, 'tag'); diff --git a/src/Models/TokenModel.php b/src/Models/TokenModel.php index b9336cd5..7886a92f 100644 --- a/src/Models/TokenModel.php +++ b/src/Models/TokenModel.php @@ -11,6 +11,8 @@ class TokenModel extends AbstractCrudModel public static function save($token) { + $token->validate(); + Database::transaction(function() use ($token) { self::forgeId($token); diff --git a/src/Models/UserModel.php b/src/Models/UserModel.php index 1811500b..dd82da7c 100644 --- a/src/Models/UserModel.php +++ b/src/Models/UserModel.php @@ -34,8 +34,7 @@ class UserModel extends AbstractCrudModel public static function save($user) { - if ($user->getAccessRank()->toInteger() == AccessRank::Anonymous) - throw new Exception('Trying to save anonymous user into database'); + $user->validate(); Database::transaction(function() use ($user) {