Reduced job hierarchy

This commit is contained in:
Marcin Kurczewski 2014-05-17 13:07:54 +02:00
parent 634d0061d4
commit de078677fe
5 changed files with 62 additions and 41 deletions

View file

@ -3,7 +3,12 @@ class AddPostJob extends AbstractJob
{ {
public function __construct() public function __construct()
{ {
$this->addSubJob(new EditPostJob()); $this->addSubJob(new EditPostSafetyJob());
$this->addSubJob(new EditPostTagsJob());
$this->addSubJob(new EditPostSourceJob());
$this->addSubJob(new EditPostRelationsJob());
$this->addSubJob(new EditPostContentJob());
$this->addSubJob(new EditPostThumbJob());
} }
public function execute() public function execute()
@ -23,15 +28,20 @@ class AddPostJob extends AbstractJob
$arguments[JobArgs::ARG_POST_ENTITY] = $post; $arguments[JobArgs::ARG_POST_ENTITY] = $post;
Logger::bufferChanges(); Logger::bufferChanges();
try foreach ($this->getSubJobs() as $subJob)
{ {
$job = $this->getSubJobs()[0]; $subJob->setContext(AbstractJob::CONTEXT_BATCH_ADD);
$job->setContext(AbstractJob::CONTEXT_BATCH_ADD); try
Api::run($job, $arguments); {
} Api::run($subJob, $arguments);
finally }
{ catch (ApiJobUnsatisfiedException $e)
Logger::discardBuffer(); {
}
finally
{
Logger::discardBuffer();
}
} }
//save the post to db if everything went okay //save the post to db if everything went okay

View file

@ -18,30 +18,25 @@ class EditPostJob extends AbstractJob
{ {
$post = $this->postRetriever->retrieve(); $post = $this->postRetriever->retrieve();
Logger::bufferChanges(); $arguments = $this->getArguments();
$arguments[JobArgs::ARG_POST_ENTITY] = $post;
Logger::bufferChanges();
foreach ($this->getSubJobs() as $subJob) foreach ($this->getSubJobs() as $subJob)
{ {
$subJob->setContext($this->getContext() == self::CONTEXT_BATCH_ADD $subJob->setContext(self::CONTEXT_BATCH_EDIT);
? self::CONTEXT_BATCH_ADD
: self::CONTEXT_BATCH_EDIT);
$args = $this->getArguments();
$args[JobArgs::ARG_POST_ENTITY] = $post;
try try
{ {
Api::run($subJob, $args); Api::run($subJob, $arguments);
} }
catch (ApiJobUnsatisfiedException $e) catch (ApiJobUnsatisfiedException $e)
{ {
} }
} }
if ($this->getContext() == AbstractJob::CONTEXT_NORMAL) PostModel::save($post);
{ Logger::flush();
PostModel::save($post);
Logger::flush();
}
return $post; return $post;
} }
@ -54,9 +49,7 @@ class EditPostJob extends AbstractJob
public function getRequiredPrivileges() public function getRequiredPrivileges()
{ {
return new Privilege( return new Privilege(
$this->getContext() == self::CONTEXT_BATCH_ADD Privilege::EditPost,
? Privilege::AddPost
: Privilege::EditPost,
Access::getIdentity($this->postRetriever->retrieve()->getUploader())); Access::getIdentity($this->postRetriever->retrieve()->getUploader()));
} }
} }

View file

@ -3,7 +3,10 @@ class AddUserJob extends AbstractJob
{ {
public function __construct() public function __construct()
{ {
$this->addSubJob(new EditUserJob()); $this->addSubJob(new EditUserAccessRankJob());
$this->addSubJob(new EditUserNameJob());
$this->addSubJob(new EditUserPasswordJob());
$this->addSubJob(new EditUserEmailJob());
} }
public function execute() public function execute()
@ -28,15 +31,21 @@ class AddUserJob extends AbstractJob
$arguments[JobArgs::ARG_USER_ENTITY] = $user; $arguments[JobArgs::ARG_USER_ENTITY] = $user;
Logger::bufferChanges(); Logger::bufferChanges();
try foreach ($this->getSubJobs() as $subJob)
{ {
$job = $this->getSubJobs()[0]; $subJob->setContext(self::CONTEXT_BATCH_ADD);
$job->setContext(self::CONTEXT_BATCH_ADD);
Api::run($job, $arguments); try
} {
finally Api::run($subJob, $arguments);
{ }
Logger::discardBuffer(); catch (ApiJobUnsatisfiedException $e)
{
}
finally
{
Logger::discardBuffer();
}
} }
//save the user to db if everything went okay //save the user to db if everything went okay

View file

@ -34,19 +34,17 @@ class EditUserJob extends AbstractJob
{ {
$user = $this->userRetriever->retrieve(); $user = $this->userRetriever->retrieve();
Logger::bufferChanges(); $arguments = $this->getArguments();
$arguments[JobArgs::ARG_USER_ENTITY] = $user;
Logger::bufferChanges();
foreach ($this->getSubJobs() as $subJob) foreach ($this->getSubJobs() as $subJob)
{ {
$subJob->setContext($this->getContext() == self::CONTEXT_BATCH_ADD $subJob->setContext(self::CONTEXT_BATCH_EDIT);
? self::CONTEXT_BATCH_ADD
: self::CONTEXT_BATCH_EDIT);
$args = $this->getArguments();
$args[JobArgs::ARG_USER_ENTITY] = $user;
try try
{ {
Api::run($subJob, $args); Api::run($subJob, $arguments);
} }
catch (ApiJobUnsatisfiedException $e) catch (ApiJobUnsatisfiedException $e)
{ {

View file

@ -55,7 +55,6 @@ class ApiPrivilegeTest extends AbstractFullApiTest
$job->setContext(AbstractJob::CONTEXT_BATCH_ADD); $job->setContext(AbstractJob::CONTEXT_BATCH_ADD);
return $job; return $job;
}; };
$this->testDynamicPostPrivilege($ctx(new EditPostJob), new Privilege(Privilege::AddPost));
$this->testDynamicPostPrivilege($ctx(new EditPostContentJob), new Privilege(Privilege::AddPostContent)); $this->testDynamicPostPrivilege($ctx(new EditPostContentJob), new Privilege(Privilege::AddPostContent));
$this->testDynamicPostPrivilege($ctx(new EditPostRelationsJob), new Privilege(Privilege::AddPostRelations)); $this->testDynamicPostPrivilege($ctx(new EditPostRelationsJob), new Privilege(Privilege::AddPostRelations));
$this->testDynamicPostPrivilege($ctx(new EditPostSafetyJob), new Privilege(Privilege::AddPostSafety)); $this->testDynamicPostPrivilege($ctx(new EditPostSafetyJob), new Privilege(Privilege::AddPostSafety));
@ -135,6 +134,18 @@ class ApiPrivilegeTest extends AbstractFullApiTest
$this->testDynamicUserPrivilege(new EditUserNameJob(), new Privilege(Privilege::ChangeUserName)); $this->testDynamicUserPrivilege(new EditUserNameJob(), new Privilege(Privilege::ChangeUserName));
$this->testDynamicUserPrivilege(new EditUserPasswordJob(), new Privilege(Privilege::ChangeUserPassword)); $this->testDynamicUserPrivilege(new EditUserPasswordJob(), new Privilege(Privilege::ChangeUserPassword));
$this->testDynamicUserPrivilege(new EditUserSettingsJob(), new Privilege(Privilege::ChangeUserSettings)); $this->testDynamicUserPrivilege(new EditUserSettingsJob(), new Privilege(Privilege::ChangeUserSettings));
$ctx = function($job)
{
$job->setContext(AbstractJob::CONTEXT_BATCH_ADD);
return $job;
};
$this->testDynamicUserPrivilege($ctx(new EditUserAccessRankJob()), new Privilege(Privilege::ChangeUserAccessRank));
$this->testDynamicUserPrivilege($ctx(new EditUserEmailJob()), new Privilege(Privilege::RegisterAccount));
$this->testDynamicUserPrivilege($ctx(new EditUserNameJob()), new Privilege(Privilege::RegisterAccount));
$this->testDynamicUserPrivilege($ctx(new EditUserPasswordJob()), new Privilege(Privilege::RegisterAccount));
$this->testDynamicUserPrivilege($ctx(new EditUserSettingsJob()), new Privilege(Privilege::ChangeUserSettings));
$this->testDynamicUserPrivilege(new FlagUserJob(), new Privilege(Privilege::FlagUser)); $this->testDynamicUserPrivilege(new FlagUserJob(), new Privilege(Privilege::FlagUser));
$this->testDynamicUserPrivilege(new GetUserJob(), new Privilege(Privilege::ViewUser)); $this->testDynamicUserPrivilege(new GetUserJob(), new Privilege(Privilege::ViewUser));
$this->testDynamicUserPrivilege(new GetUserSettingsJob(), new Privilege(Privilege::ChangeUserSettings)); $this->testDynamicUserPrivilege(new GetUserSettingsJob(), new Privilege(Privilege::ChangeUserSettings));