Skip to content

Commit be19e12

Browse files
author
Sebastian Machuca
committed
Making the factory the default layer to construct jobs
1 parent df20186 commit be19e12

File tree

5 files changed

+64
-29
lines changed

5 files changed

+64
-29
lines changed

lib/Resque/Job.php

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @author Chris Boulton <chris@bigcommerce.com>
77
* @license http://www.opensource.org/licenses/mit-license.php
88
*/
9-
class Resque_Job implements Resque_JobInterface
9+
class Resque_Job
1010
{
1111
/**
1212
* @var string The name of the queue that this job belongs to.
@@ -24,7 +24,7 @@ class Resque_Job implements Resque_JobInterface
2424
public $payload;
2525

2626
/**
27-
* @var Resque_JobInterface Instance of the class performing work for this job.
27+
* @var object|Resque_JobInterface Instance of the class performing work for this job.
2828
*/
2929
private $instance;
3030

@@ -169,28 +169,9 @@ public function getInstance()
169169
return $this->instance;
170170
}
171171

172-
if(!class_exists($this->payload['class'])) {
173-
throw new Resque_Exception(
174-
'Could not find job class ' . $this->payload['class'] . '.'
175-
);
176-
}
177-
178-
if(!method_exists($this->payload['class'], 'perform')) {
179-
throw new Resque_Exception(
180-
'Job class ' . $this->payload['class'] . ' does not contain a perform method.'
181-
);
182-
}
183-
184-
if ($this->jobFactory !== null) {
185-
$this->instance = $this->jobFactory->create($this->payload['class'], $this->getArguments(), $this->queue);
186-
return $this->instance;
187-
}
188-
$this->instance = new $this->payload['class'];
189-
$this->instance->job = $this;
190-
$this->instance->args = $this->getArguments();
191-
$this->instance->queue = $this->queue;
192-
193-
return $this->instance;
172+
$this->instance = $this->getJobFactory()->create($this->payload['class'], $this->getArguments(), $this->queue);
173+
$this->instance->job = $this;
174+
return $this->instance;
194175
}
195176

196177
/**
@@ -294,4 +275,15 @@ public function setJobFactory(Resque_Job_FactoryInterface $jobFactory)
294275

295276
return $this;
296277
}
278+
279+
/**
280+
* @return Resque_Job_FactoryInterface
281+
*/
282+
public function getJobFactory()
283+
{
284+
if ($this->jobFactory === null) {
285+
$this->jobFactory = new Resque_Job_Factory();
286+
}
287+
return $this->jobFactory;
288+
}
297289
}

lib/Resque/Job/Factory.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
class Resque_Job_Factory implements Resque_Job_FactoryInterface
4+
{
5+
6+
/**
7+
* @param $className
8+
* @param array $args
9+
* @param $queue
10+
* @return Resque_JobInterface
11+
* @throws \Resque_Exception
12+
*/
13+
public function create($className, $args, $queue)
14+
{
15+
if (!class_exists($className)) {
16+
throw new Resque_Exception(
17+
'Could not find job class ' . $className . '.'
18+
);
19+
}
20+
21+
if (!method_exists($className, 'perform')) {
22+
throw new Resque_Exception(
23+
'Job class ' . $className . ' does not contain a perform method.'
24+
);
25+
}
26+
27+
$instance = new $className;
28+
$instance->args = $args;
29+
$instance->queue = $queue;
30+
return $instance;
31+
}
32+
}

lib/Resque/Job/FactoryInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ interface Resque_Job_FactoryInterface
88
* @param $queue
99
* @return Resque_JobInterface
1010
*/
11-
public function create($className, array $args, $queue);
11+
public function create($className, $args, $queue);
1212
}

test/Resque/Tests/EventTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function getEventTestJob()
3131
$payload = array(
3232
'class' => 'Test_Job',
3333
'args' => array(
34-
'somevar',
34+
array('somevar'),
3535
),
3636
);
3737
$job = new Resque_Job('jobs', $payload);

test/Resque/Tests/JobTest.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -362,13 +362,24 @@ public function testDequeueItemWithiWrongArg()
362362
$this->assertEquals(Resque::size($queue), 2);
363363
}
364364

365-
public function testUseFactoryToGetJobInstance()
365+
public function testUseDefaultFactoryToGetJobInstance()
366366
{
367367
$payload = array(
368368
'class' => 'Some_Job_Class',
369369
'args' => null
370370
);
371371
$job = new Resque_Job('jobs', $payload);
372+
$instance = $job->getInstance();
373+
$this->assertInstanceOf('Some_Job_Class', $instance);
374+
}
375+
376+
public function testUseFactoryToGetJobInstance()
377+
{
378+
$payload = array(
379+
'class' => 'Some_Job_Class',
380+
'args' => array(array())
381+
);
382+
$job = new Resque_Job('jobs', $payload);
372383
$factory = new Some_Stub_Factory();
373384
$job->setJobFactory($factory);
374385
$instance = $job->getInstance();
@@ -379,7 +390,7 @@ public function testDoNotUseFactoryToGetInstance()
379390
{
380391
$payload = array(
381392
'class' => 'Some_Job_Class',
382-
'args' => null
393+
'args' => array(array())
383394
);
384395
$job = new Resque_Job('jobs', $payload);
385396
$factory = $this->getMock('Resque_Job_FactoryInterface');
@@ -411,7 +422,7 @@ class Some_Stub_Factory implements Resque_Job_FactoryInterface
411422
* @param $queue
412423
* @return Resque_JobInterface
413424
*/
414-
public function create($className, array $args, $queue)
425+
public function create($className, $args, $queue)
415426
{
416427
return new Some_Job_Class();
417428
}

0 commit comments

Comments
 (0)