Test Failed
Pull Request — master (#56)
by
unknown
02:50
created

FailureHandler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 59
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getBackend() 0 7 2
A setBackend() 0 3 1
A createFromError() 0 4 1
A create() 0 4 1
1
<?php
2
3
namespace Resque;
4
5
use \Resque\Worker\ResqueWorker;
6
use \Exception as CoreException;
7
use \Error;
8
9
/**
10
 * Failed Resque job.
11
 *
12
 * @package		Resque/FailureHandler
13
 * @author		Chris Boulton <[email protected]>
14
 * @license		http://www.opensource.org/licenses/mit-license.php
15
 */
16
class FailureHandler
17
{
18
	/**
19
	 * @var string Class name representing the backend to pass failed jobs off to.
20
	 */
21
	private static $backend;
22
23
	/**
24
	 * Create a new failed job on the backend.
25
	 *
26
	 * @param object $payload        The contents of the job that has just failed.
27
	 * @param \Exception $exception  The exception generated when the job failed to run.
28
	 * @param \Resque\Worker\ResqueWorker $worker Instance of Resque\Worker\ResqueWorker that was running this job when it failed.
29
	 * @param string $queue          The name of the queue that this job was fetched from.
30
	 */
31
	public static function create($payload, CoreException $exception, ResqueWorker $worker, $queue)
32
	{
33
		$backend = self::getBackend();
34
		new $backend($payload, $exception, $worker, $queue);
35
	}
36
37
	/**
38
	 * Create a new failed job on the backend from PHP 7 errors.
39
	 *
40
	 * @param object $payload        The contents of the job that has just failed.
41
	 * @param \Error $exception  The PHP 7 error generated when the job failed to run.
42
	 * @param \Resque\Worker\ResqueWorker $worker Instance of Resque\Worker\ResqueWorker that was running this job when it failed.
43
	 * @param string $queue          The name of the queue that this job was fetched from.
44
	 */
45
	public static function createFromError($payload, Error $exception, ResqueWorker $worker, $queue)
46
	{
47
		$backend = self::getBackend();
48
		new $backend($payload, $exception, $worker, $queue);
49
	}
50
51
	/**
52
	 * Return an instance of the backend for saving job failures.
53
	 *
54
	 * @return object Instance of backend object.
55
	 */
56
	public static function getBackend()
57
	{
58
		if (self::$backend === null) {
0 ignored issues
show
introduced by
The condition self::backend === null is always false.
Loading history...
59
			self::$backend = 'Resque\Failure\RedisFailure';
60
		}
61
62
		return self::$backend;
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::backend returns the type string which is incompatible with the documented return type object.
Loading history...
63
	}
64
65
	/**
66
	 * Set the backend to use for raised job failures. The supplied backend
67
	 * should be the name of a class to be instantiated when a job fails.
68
	 * It is your responsibility to have the backend class loaded (or autoloaded)
69
	 *
70
	 * @param string $backend The class name of the backend to pipe failures to.
71
	 */
72
	public static function setBackend($backend)
73
	{
74
		self::$backend = $backend;
75
	}
76
}
77