From be19e12c315923345d9d6c5b911d57cdfaad012b Mon Sep 17 00:00:00 2001 From: Sebastian Machuca Date: Fri, 14 Oct 2016 14:41:19 +1100 Subject: [PATCH] Making the factory the default layer to construct jobs --- lib/Resque/Job.php | 40 ++++++++++++----------------- lib/Resque/Job/Factory.php | 32 +++++++++++++++++++++++ lib/Resque/Job/FactoryInterface.php | 2 +- test/Resque/Tests/EventTest.php | 2 +- test/Resque/Tests/JobTest.php | 17 +++++++++--- 5 files changed, 64 insertions(+), 29 deletions(-) create mode 100644 lib/Resque/Job/Factory.php diff --git a/lib/Resque/Job.php b/lib/Resque/Job.php index bb79f32..8508f76 100755 --- a/lib/Resque/Job.php +++ b/lib/Resque/Job.php @@ -6,7 +6,7 @@ * @author Chris Boulton * @license http://www.opensource.org/licenses/mit-license.php */ -class Resque_Job implements Resque_JobInterface +class Resque_Job { /** * @var string The name of the queue that this job belongs to. @@ -24,7 +24,7 @@ class Resque_Job implements Resque_JobInterface public $payload; /** - * @var Resque_JobInterface Instance of the class performing work for this job. + * @var object|Resque_JobInterface Instance of the class performing work for this job. */ private $instance; @@ -169,28 +169,9 @@ class Resque_Job implements Resque_JobInterface return $this->instance; } - if(!class_exists($this->payload['class'])) { - throw new Resque_Exception( - 'Could not find job class ' . $this->payload['class'] . '.' - ); - } - - if(!method_exists($this->payload['class'], 'perform')) { - throw new Resque_Exception( - 'Job class ' . $this->payload['class'] . ' does not contain a perform method.' - ); - } - - if ($this->jobFactory !== null) { - $this->instance = $this->jobFactory->create($this->payload['class'], $this->getArguments(), $this->queue); - return $this->instance; - } - $this->instance = new $this->payload['class']; - $this->instance->job = $this; - $this->instance->args = $this->getArguments(); - $this->instance->queue = $this->queue; - - return $this->instance; + $this->instance = $this->getJobFactory()->create($this->payload['class'], $this->getArguments(), $this->queue); + $this->instance->job = $this; + return $this->instance; } /** @@ -294,4 +275,15 @@ class Resque_Job implements Resque_JobInterface return $this; } + + /** + * @return Resque_Job_FactoryInterface + */ + public function getJobFactory() + { + if ($this->jobFactory === null) { + $this->jobFactory = new Resque_Job_Factory(); + } + return $this->jobFactory; + } } diff --git a/lib/Resque/Job/Factory.php b/lib/Resque/Job/Factory.php new file mode 100644 index 0000000..cf17294 --- /dev/null +++ b/lib/Resque/Job/Factory.php @@ -0,0 +1,32 @@ +args = $args; + $instance->queue = $queue; + return $instance; + } +} diff --git a/lib/Resque/Job/FactoryInterface.php b/lib/Resque/Job/FactoryInterface.php index be573a2..b8c102c 100644 --- a/lib/Resque/Job/FactoryInterface.php +++ b/lib/Resque/Job/FactoryInterface.php @@ -8,5 +8,5 @@ interface Resque_Job_FactoryInterface * @param $queue * @return Resque_JobInterface */ - public function create($className, array $args, $queue); + public function create($className, $args, $queue); } diff --git a/test/Resque/Tests/EventTest.php b/test/Resque/Tests/EventTest.php index 1147c99..6e102cf 100644 --- a/test/Resque/Tests/EventTest.php +++ b/test/Resque/Tests/EventTest.php @@ -31,7 +31,7 @@ class Resque_Tests_EventTest extends Resque_Tests_TestCase $payload = array( 'class' => 'Test_Job', 'args' => array( - 'somevar', + array('somevar'), ), ); $job = new Resque_Job('jobs', $payload); diff --git a/test/Resque/Tests/JobTest.php b/test/Resque/Tests/JobTest.php index a1788ed..fe58ce4 100644 --- a/test/Resque/Tests/JobTest.php +++ b/test/Resque/Tests/JobTest.php @@ -362,13 +362,24 @@ class Resque_Tests_JobTest extends Resque_Tests_TestCase $this->assertEquals(Resque::size($queue), 2); } - public function testUseFactoryToGetJobInstance() + public function testUseDefaultFactoryToGetJobInstance() { $payload = array( 'class' => 'Some_Job_Class', 'args' => null ); $job = new Resque_Job('jobs', $payload); + $instance = $job->getInstance(); + $this->assertInstanceOf('Some_Job_Class', $instance); + } + + public function testUseFactoryToGetJobInstance() + { + $payload = array( + 'class' => 'Some_Job_Class', + 'args' => array(array()) + ); + $job = new Resque_Job('jobs', $payload); $factory = new Some_Stub_Factory(); $job->setJobFactory($factory); $instance = $job->getInstance(); @@ -379,7 +390,7 @@ class Resque_Tests_JobTest extends Resque_Tests_TestCase { $payload = array( 'class' => 'Some_Job_Class', - 'args' => null + 'args' => array(array()) ); $job = new Resque_Job('jobs', $payload); $factory = $this->getMock('Resque_Job_FactoryInterface'); @@ -411,7 +422,7 @@ class Some_Stub_Factory implements Resque_Job_FactoryInterface * @param $queue * @return Resque_JobInterface */ - public function create($className, array $args, $queue) + public function create($className, $args, $queue) { return new Some_Job_Class(); }