Moved JobArgs to Jobs

Reason: trying to make unique string for every possible argument in
global fashion is difficult. For example it would make sense for
EditPostRelationsJob to accept argument named "post-ids", but it
wouldn't make much sense for AddPostJob to accept "post-ids" since it
doesn't tell much. Thus, common arguments are going to be defined in
top-level AbstractJob for ease of control, while more job-specific
arguments are going to be specified in respective job implementations.
This commit is contained in:
Marcin Kurczewski 2014-05-03 11:43:29 +02:00
parent 162b131435
commit f383a5ed21
14 changed files with 39 additions and 42 deletions

View file

@ -6,7 +6,7 @@ class CommentController
$ret = Api::run( $ret = Api::run(
new ListCommentsJob(), new ListCommentsJob(),
[ [
JobArgs::PAGE_NUMBER => $page, ListCommentsJob::PAGE_NUMBER => $page,
]); ]);
$context = getContext(); $context = getContext();
@ -23,7 +23,7 @@ class CommentController
$comment = Api::run( $comment = Api::run(
new PreviewCommentJob(), new PreviewCommentJob(),
[ [
JobArgs::TEXT => InputHelper::get('text') PreviewCommentJob::TEXT => InputHelper::get('text')
]); ]);
getContext()->transport->textPreview = $comment->getText(); getContext()->transport->textPreview = $comment->getText();
@ -37,8 +37,8 @@ class CommentController
Api::run( Api::run(
new AddCommentJob(), new AddCommentJob(),
[ [
JobArgs::POST_ID => InputHelper::get('post-id'), AddCommentJob::POST_ID => InputHelper::get('post-id'),
JobArgs::TEXT => InputHelper::get('text') AddCommentJob::TEXT => InputHelper::get('text')
]); ]);
} }
@ -55,8 +55,8 @@ class CommentController
Api::run( Api::run(
new EditCommentJob(), new EditCommentJob(),
[ [
JobArgs::COMMENT_ID => $id, EditCommentJob::COMMENT_ID => $id,
JobArgs::TEXT => InputHelper::get('text') EditCommentJob::TEXT => InputHelper::get('text')
]); ]);
} }
@ -65,7 +65,7 @@ class CommentController
$comment = Api::run( $comment = Api::run(
new DeleteCommentJob(), new DeleteCommentJob(),
[ [
JobArgs::COMMENT_ID => $id, DeleteCommentJob::COMMENT_ID => $id,
]); ]);
} }
} }

View file

@ -30,9 +30,9 @@ class LogController
$ret = Api::run( $ret = Api::run(
new GetLogJob(), new GetLogJob(),
[ [
JobArgs::PAGE_NUMBER => $page, GetLogJob::PAGE_NUMBER => $page,
JobArgs::LOG_ID => $name, GetLogJob::LOG_ID => $name,
JobArgs::QUERY => $filter, GetLogJob::QUERY => $filter,
]); ]);
//stylize important lines //stylize important lines

View file

@ -42,8 +42,8 @@ class PostController
$ret = Api::run( $ret = Api::run(
new ListPostsJob(), new ListPostsJob(),
[ [
JobArgs::PAGE_NUMBER => $page, ListPostsJob::PAGE_NUMBER => $page,
JobArgs::QUERY => $query ListPostsJob::QUERY => $query
]); ]);
$context->transport->posts = $ret->posts; $context->transport->posts = $ret->posts;
@ -78,9 +78,9 @@ class PostController
Api::run( Api::run(
new TogglePostTagJob(), new TogglePostTagJob(),
[ [
JobArgs::POST_ID => $id, TogglePostTagJob::POST_ID => $id,
JobArgs::TAG_NAME => $tag, TogglePostTagJob::TAG_NAME => $tag,
JobArgs::STATE => $enable, TogglePostTagJob::STATE => $enable,
]); ]);
} }

View file

@ -1,6 +1,15 @@
<?php <?php
abstract class AbstractJob abstract class AbstractJob
{ {
const COMMENT_ID = 'comment-id';
const POST_ID = 'post-id';
const TAG_NAME = 'tag-name';
const TEXT = 'text';
const PAGE_NUMBER = 'page-number';
const QUERY = 'query';
const LOG_ID = 'log-id';
const STATE = 'state';
protected $arguments; protected $arguments;
public function prepare() public function prepare()

View file

@ -5,7 +5,7 @@ abstract class AbstractPostEditJob extends AbstractJob
public function prepare() public function prepare()
{ {
$postId = $this->getArgument(JobArgs::POST_ID); $postId = $this->getArgument(self::POST_ID);
$this->post = PostModel::findByIdOrName($postId); $this->post = PostModel::findByIdOrName($postId);
} }
} }

View file

@ -1,12 +0,0 @@
<?php
class JobArgs
{
const COMMENT_ID = 'comment-id';
const POST_ID = 'post-id';
const TAG_NAME = 'tag-name';
const TEXT = 'text';
const PAGE_NUMBER = 'page-number';
const QUERY = 'query';
const LOG_ID = 'log-id';
const STATE = 'state';
}

View file

@ -4,8 +4,8 @@ class AddCommentJob extends AbstractJob
public function execute() public function execute()
{ {
$user = Auth::getCurrentUser(); $user = Auth::getCurrentUser();
$post = PostModel::findById($this->getArgument(JobArgs::POST_ID)); $post = PostModel::findById($this->getArgument(self::POST_ID));
$text = CommentModel::validateText($this->getArgument(JobArgs::TEXT)); $text = CommentModel::validateText($this->getArgument(self::TEXT));
$comment = CommentModel::spawn(); $comment = CommentModel::spawn();
$comment->setCommenter($user); $comment->setCommenter($user);

View file

@ -5,7 +5,7 @@ class DeleteCommentJob extends AbstractJob
public function prepare() public function prepare()
{ {
$this->comment = CommentModel::findById($this->getArgument(JobArgs::COMMENT_ID)); $this->comment = CommentModel::findById($this->getArgument(self::COMMENT_ID));
} }
public function execute() public function execute()

View file

@ -5,7 +5,7 @@ class EditCommentJob extends AbstractJob
public function prepare() public function prepare()
{ {
$this->comment = CommentModel::findById($this->getArgument(JobArgs::COMMENT_ID)); $this->comment = CommentModel::findById($this->getArgument(self::COMMENT_ID));
} }
public function execute() public function execute()
@ -13,7 +13,7 @@ class EditCommentJob extends AbstractJob
$comment = $this->comment; $comment = $this->comment;
$comment->commentDate = time(); $comment->commentDate = time();
$comment->text = CommentModel::validateText($this->getArgument(JobArgs::TEXT)); $comment->text = CommentModel::validateText($this->getArgument(self::TEXT));
CommentModel::save($comment); CommentModel::save($comment);
LogHelper::log('{user} edited comment in {post}', [ LogHelper::log('{user} edited comment in {post}', [

View file

@ -3,9 +3,9 @@ class GetLogJob extends AbstractJob
{ {
public function execute() public function execute()
{ {
$page = $this->getArgument(JobArgs::PAGE_NUMBER); $page = $this->getArgument(self::PAGE_NUMBER);
$name = $this->getArgument(JobArgs::LOG_ID); $name = $this->getArgument(self::LOG_ID);
$query = $this->getArgument(JobArgs::QUERY); $query = $this->getArgument(self::QUERY);
//parse input //parse input
$page = max(1, intval($page)); $page = max(1, intval($page));

View file

@ -3,7 +3,7 @@ class ListCommentsJob extends AbstractJob
{ {
public function execute() public function execute()
{ {
$page = $this->getArgument(JobArgs::PAGE_NUMBER); $page = $this->getArgument(self::PAGE_NUMBER);
$page = max(1, intval($page)); $page = max(1, intval($page));
$commentsPerPage = intval(getConfig()->comments->commentsPerPage); $commentsPerPage = intval(getConfig()->comments->commentsPerPage);

View file

@ -3,8 +3,8 @@ class ListPostsJob extends AbstractJob
{ {
public function execute() public function execute()
{ {
$page = $this->getArgument(JobArgs::PAGE_NUMBER); $page = $this->getArgument(self::PAGE_NUMBER);
$query = $this->getArgument(JobArgs::QUERY); $query = $this->getArgument(self::QUERY);
$page = max(1, intval($page)); $page = max(1, intval($page));
$postsPerPage = intval(getConfig()->browsing->postsPerPage); $postsPerPage = intval(getConfig()->browsing->postsPerPage);

View file

@ -4,7 +4,7 @@ class PreviewCommentJob extends AbstractJob
public function execute() public function execute()
{ {
$user = Auth::getCurrentUser(); $user = Auth::getCurrentUser();
$text = CommentModel::validateText($this->getArgument(JobArgs::TEXT)); $text = CommentModel::validateText($this->getArgument(self::TEXT));
$comment = CommentModel::spawn(); $comment = CommentModel::spawn();
$comment->setCommenter($user); $comment->setCommenter($user);

View file

@ -3,8 +3,8 @@ class TogglePostTagJob extends AbstractPostEditJob
{ {
public function execute() public function execute()
{ {
$tagName = $this->getArgument(JobArgs::TAG_NAME); $tagName = $this->getArgument(self::TAG_NAME);
$enable = boolval($this->getArgument(JobArgs::STATE)); $enable = boolval($this->getArgument(self::STATE));
$post = $this->post; $post = $this->post;
$tags = $post->getTags(); $tags = $post->getTags();