From de078677fe3e708a5fabaedf522a87fc606694b9 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Sat, 17 May 2014 13:07:54 +0200 Subject: [PATCH] Reduced job hierarchy --- src/Api/Jobs/PostJobs/AddPostJob.php | 28 +++++++++++++++-------- src/Api/Jobs/PostJobs/EditPostJob.php | 23 +++++++------------ src/Api/Jobs/UserJobs/AddUserJob.php | 27 ++++++++++++++-------- src/Api/Jobs/UserJobs/EditUserJob.php | 12 ++++------ tests/Tests/ApiTests/ApiPrivilegeTest.php | 13 ++++++++++- 5 files changed, 62 insertions(+), 41 deletions(-) diff --git a/src/Api/Jobs/PostJobs/AddPostJob.php b/src/Api/Jobs/PostJobs/AddPostJob.php index 017e1371..a09eed6c 100644 --- a/src/Api/Jobs/PostJobs/AddPostJob.php +++ b/src/Api/Jobs/PostJobs/AddPostJob.php @@ -3,7 +3,12 @@ class AddPostJob extends AbstractJob { 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() @@ -23,15 +28,20 @@ class AddPostJob extends AbstractJob $arguments[JobArgs::ARG_POST_ENTITY] = $post; Logger::bufferChanges(); - try + foreach ($this->getSubJobs() as $subJob) { - $job = $this->getSubJobs()[0]; - $job->setContext(AbstractJob::CONTEXT_BATCH_ADD); - Api::run($job, $arguments); - } - finally - { - Logger::discardBuffer(); + $subJob->setContext(AbstractJob::CONTEXT_BATCH_ADD); + try + { + Api::run($subJob, $arguments); + } + catch (ApiJobUnsatisfiedException $e) + { + } + finally + { + Logger::discardBuffer(); + } } //save the post to db if everything went okay diff --git a/src/Api/Jobs/PostJobs/EditPostJob.php b/src/Api/Jobs/PostJobs/EditPostJob.php index bf68e2ae..39dd3091 100644 --- a/src/Api/Jobs/PostJobs/EditPostJob.php +++ b/src/Api/Jobs/PostJobs/EditPostJob.php @@ -18,30 +18,25 @@ class EditPostJob extends AbstractJob { $post = $this->postRetriever->retrieve(); - Logger::bufferChanges(); + $arguments = $this->getArguments(); + $arguments[JobArgs::ARG_POST_ENTITY] = $post; + Logger::bufferChanges(); foreach ($this->getSubJobs() as $subJob) { - $subJob->setContext($this->getContext() == self::CONTEXT_BATCH_ADD - ? self::CONTEXT_BATCH_ADD - : self::CONTEXT_BATCH_EDIT); + $subJob->setContext(self::CONTEXT_BATCH_EDIT); - $args = $this->getArguments(); - $args[JobArgs::ARG_POST_ENTITY] = $post; try { - Api::run($subJob, $args); + Api::run($subJob, $arguments); } catch (ApiJobUnsatisfiedException $e) { } } - if ($this->getContext() == AbstractJob::CONTEXT_NORMAL) - { - PostModel::save($post); - Logger::flush(); - } + PostModel::save($post); + Logger::flush(); return $post; } @@ -54,9 +49,7 @@ class EditPostJob extends AbstractJob public function getRequiredPrivileges() { return new Privilege( - $this->getContext() == self::CONTEXT_BATCH_ADD - ? Privilege::AddPost - : Privilege::EditPost, + Privilege::EditPost, Access::getIdentity($this->postRetriever->retrieve()->getUploader())); } } diff --git a/src/Api/Jobs/UserJobs/AddUserJob.php b/src/Api/Jobs/UserJobs/AddUserJob.php index b4029563..0797b9c5 100644 --- a/src/Api/Jobs/UserJobs/AddUserJob.php +++ b/src/Api/Jobs/UserJobs/AddUserJob.php @@ -3,7 +3,10 @@ class AddUserJob extends AbstractJob { 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() @@ -28,15 +31,21 @@ class AddUserJob extends AbstractJob $arguments[JobArgs::ARG_USER_ENTITY] = $user; Logger::bufferChanges(); - try + foreach ($this->getSubJobs() as $subJob) { - $job = $this->getSubJobs()[0]; - $job->setContext(self::CONTEXT_BATCH_ADD); - Api::run($job, $arguments); - } - finally - { - Logger::discardBuffer(); + $subJob->setContext(self::CONTEXT_BATCH_ADD); + + try + { + Api::run($subJob, $arguments); + } + catch (ApiJobUnsatisfiedException $e) + { + } + finally + { + Logger::discardBuffer(); + } } //save the user to db if everything went okay diff --git a/src/Api/Jobs/UserJobs/EditUserJob.php b/src/Api/Jobs/UserJobs/EditUserJob.php index cd2dd555..5e9f4b93 100644 --- a/src/Api/Jobs/UserJobs/EditUserJob.php +++ b/src/Api/Jobs/UserJobs/EditUserJob.php @@ -34,19 +34,17 @@ class EditUserJob extends AbstractJob { $user = $this->userRetriever->retrieve(); - Logger::bufferChanges(); + $arguments = $this->getArguments(); + $arguments[JobArgs::ARG_USER_ENTITY] = $user; + Logger::bufferChanges(); foreach ($this->getSubJobs() as $subJob) { - $subJob->setContext($this->getContext() == self::CONTEXT_BATCH_ADD - ? self::CONTEXT_BATCH_ADD - : self::CONTEXT_BATCH_EDIT); + $subJob->setContext(self::CONTEXT_BATCH_EDIT); - $args = $this->getArguments(); - $args[JobArgs::ARG_USER_ENTITY] = $user; try { - Api::run($subJob, $args); + Api::run($subJob, $arguments); } catch (ApiJobUnsatisfiedException $e) { diff --git a/tests/Tests/ApiTests/ApiPrivilegeTest.php b/tests/Tests/ApiTests/ApiPrivilegeTest.php index a286ccb5..d489c938 100644 --- a/tests/Tests/ApiTests/ApiPrivilegeTest.php +++ b/tests/Tests/ApiTests/ApiPrivilegeTest.php @@ -55,7 +55,6 @@ class ApiPrivilegeTest extends AbstractFullApiTest $job->setContext(AbstractJob::CONTEXT_BATCH_ADD); return $job; }; - $this->testDynamicPostPrivilege($ctx(new EditPostJob), new Privilege(Privilege::AddPost)); $this->testDynamicPostPrivilege($ctx(new EditPostContentJob), new Privilege(Privilege::AddPostContent)); $this->testDynamicPostPrivilege($ctx(new EditPostRelationsJob), new Privilege(Privilege::AddPostRelations)); $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 EditUserPasswordJob(), new Privilege(Privilege::ChangeUserPassword)); $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 GetUserJob(), new Privilege(Privilege::ViewUser)); $this->testDynamicUserPrivilege(new GetUserSettingsJob(), new Privilege(Privilege::ChangeUserSettings));