More robust argument handling
This commit is contained in:
parent
3cdaa85511
commit
16c5d6961b
8 changed files with 52 additions and 29 deletions
|
@ -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();
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -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')
|
||||
]);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,13 +0,0 @@
|
|||
<?php
|
||||
abstract class AbstractJob
|
||||
{
|
||||
public function prepare($arguments)
|
||||
{
|
||||
}
|
||||
|
||||
public abstract function execute($arguments);
|
||||
|
||||
public abstract function requiresAuthentication();
|
||||
public abstract function requiresConfirmedEmail();
|
||||
public abstract function requiresPrivilege();
|
||||
}
|
28
src/Jobs/Abstraction/AbstractJob.php
Normal file
28
src/Jobs/Abstraction/AbstractJob.php
Normal file
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
abstract class AbstractJob
|
||||
{
|
||||
protected $arguments;
|
||||
|
||||
public function prepare()
|
||||
{
|
||||
}
|
||||
|
||||
public abstract function execute();
|
||||
|
||||
public abstract function requiresAuthentication();
|
||||
public abstract function requiresConfirmedEmail();
|
||||
public abstract function requiresPrivilege();
|
||||
|
||||
public function getArgument($key)
|
||||
{
|
||||
if (!isset($this->arguments[$key]))
|
||||
throw new SimpleException('Expected argument "' . $key . '" was not specified');
|
||||
|
||||
return $this->arguments[$key];
|
||||
}
|
||||
|
||||
public function setArguments($arguments)
|
||||
{
|
||||
$this->arguments = $arguments;
|
||||
}
|
||||
}
|
7
src/Jobs/Abstraction/JobArgs.php
Normal file
7
src/Jobs/Abstraction/JobArgs.php
Normal file
|
@ -0,0 +1,7 @@
|
|||
<?php
|
||||
class JobArgs
|
||||
{
|
||||
const COMMENT_ID = 'comment-id';
|
||||
const POST_ID = 'post-id';
|
||||
const TEXT = 'text';
|
||||
}
|
|
@ -1,11 +1,11 @@
|
|||
<?php
|
||||
class AddCommentJob extends AbstractJob
|
||||
{
|
||||
public function execute($arguments)
|
||||
public function execute()
|
||||
{
|
||||
$user = Auth::getCurrentUser();
|
||||
$post = PostModel::findById($arguments['post-id']);
|
||||
$text = CommentModel::validateText($arguments['text']);
|
||||
$post = PostModel::findById($this->getArgument(JobArgs::POST_ID));
|
||||
$text = CommentModel::validateText($this->getArgument(JobArgs::TEXT));
|
||||
|
||||
$comment = CommentModel::spawn();
|
||||
$comment->setCommenter($user);
|
||||
|
|
|
@ -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}', [
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
<?php
|
||||
class PreviewCommentJob extends AbstractJob
|
||||
{
|
||||
public function execute($arguments)
|
||||
public function execute()
|
||||
{
|
||||
$user = Auth::getCurrentUser();
|
||||
$text = CommentModel::validateText($arguments['text']);
|
||||
$text = CommentModel::validateText($this->getArgument(JobArgs::TEXT));
|
||||
|
||||
$comment = CommentModel::spawn();
|
||||
$comment->setCommenter($user);
|
||||
|
|
Loading…
Reference in a new issue