From 16c5d6961b549c3bfa797b9cf37d7cb05a6c9902 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Fri, 2 May 2014 09:32:47 +0200 Subject: [PATCH] More robust argument handling --- src/Api.php | 5 +++-- src/Controllers/CommentController.php | 10 +++++----- src/Jobs/AbstractJob.php | 13 ------------- src/Jobs/Abstraction/AbstractJob.php | 28 +++++++++++++++++++++++++++ src/Jobs/Abstraction/JobArgs.php | 7 +++++++ src/Jobs/AddCommentJob.php | 6 +++--- src/Jobs/EditCommentJob.php | 8 ++++---- src/Jobs/PreviewCommentJob.php | 4 ++-- 8 files changed, 52 insertions(+), 29 deletions(-) delete mode 100644 src/Jobs/AbstractJob.php create mode 100644 src/Jobs/Abstraction/AbstractJob.php create mode 100644 src/Jobs/Abstraction/JobArgs.php diff --git a/src/Api.php b/src/Api.php index 64dc2879..2b372a46 100644 --- a/src/Api.php +++ b/src/Api.php @@ -7,7 +7,8 @@ class Api return \Chibi\Database::transaction(function() use ($job, $jobArgs) { - $job->prepare($jobArgs); + $job->setArguments($jobArgs); + $job->prepare(); if ($job->requiresAuthentication()) Access::assertAuthentication(); @@ -22,7 +23,7 @@ class Api if ($privilege !== null) Access::assert($privilege, $subPrivilege); - return $job->execute($jobArgs); + return $job->execute(); }); } diff --git a/src/Controllers/CommentController.php b/src/Controllers/CommentController.php index 5c4a6ea5..2bdcc43f 100644 --- a/src/Controllers/CommentController.php +++ b/src/Controllers/CommentController.php @@ -35,7 +35,7 @@ class CommentController $comment = Api::run( new PreviewCommentJob(), [ - 'text' => InputHelper::get('text') + JobArgs::TEXT => InputHelper::get('text') ]); getContext()->transport->textPreview = $comment->getText(); @@ -49,8 +49,8 @@ class CommentController $comment = Api::run( new AddCommentJob(), [ - 'post-id' => InputHelper::get('post-id'), - 'text' => InputHelper::get('text') + JobArgs::POST_ID => InputHelper::get('post-id'), + JobArgs::TEXT => InputHelper::get('text') ]); } @@ -67,8 +67,8 @@ class CommentController $comment = Api::run( new EditCommentJob(), [ - 'comment-id' => $id, - 'text' => InputHelper::get('text') + JobArgs::COMMENT_ID => $id, + JobArgs::TEXT => InputHelper::get('text') ]); } diff --git a/src/Jobs/AbstractJob.php b/src/Jobs/AbstractJob.php deleted file mode 100644 index d168fcc4..00000000 --- a/src/Jobs/AbstractJob.php +++ /dev/null @@ -1,13 +0,0 @@ -arguments[$key])) + throw new SimpleException('Expected argument "' . $key . '" was not specified'); + + return $this->arguments[$key]; + } + + public function setArguments($arguments) + { + $this->arguments = $arguments; + } +} diff --git a/src/Jobs/Abstraction/JobArgs.php b/src/Jobs/Abstraction/JobArgs.php new file mode 100644 index 00000000..677f11c1 --- /dev/null +++ b/src/Jobs/Abstraction/JobArgs.php @@ -0,0 +1,7 @@ +getArgument(JobArgs::POST_ID)); + $text = CommentModel::validateText($this->getArgument(JobArgs::TEXT)); $comment = CommentModel::spawn(); $comment->setCommenter($user); diff --git a/src/Jobs/EditCommentJob.php b/src/Jobs/EditCommentJob.php index 8419b844..f82abf03 100644 --- a/src/Jobs/EditCommentJob.php +++ b/src/Jobs/EditCommentJob.php @@ -3,18 +3,18 @@ class EditCommentJob extends AbstractJob { protected $comment; - public function prepare($arguments) + public function prepare() { - $this->comment = CommentModel::findById($arguments['comment-id']); + $this->comment = CommentModel::findById($this->getArgument(JobArgs::COMMENT_ID)); } - public function execute($arguments) + public function execute() { $user = Auth::getCurrentUser(); $comment = $this->comment; $comment->commentDate = time(); - $comment->text = CommentModel::validateText($arguments['text']); + $comment->text = CommentModel::validateText($this->getArgument(JobArgs::TEXT)); CommentModel::save($comment); LogHelper::log('{user} edited comment in {post}', [ diff --git a/src/Jobs/PreviewCommentJob.php b/src/Jobs/PreviewCommentJob.php index 232258b5..04f235bd 100644 --- a/src/Jobs/PreviewCommentJob.php +++ b/src/Jobs/PreviewCommentJob.php @@ -1,10 +1,10 @@ getArgument(JobArgs::TEXT)); $comment = CommentModel::spawn(); $comment->setCommenter($user);