Completed
Push — master ( 04e2c5...10745e )
by Ashley
01:49
created

Worker   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 20%

Importance

Changes 0
Metric Value
dl 0
loc 56
ccs 4
cts 20
cp 0.2
rs 10
c 0
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B process() 0 28 6
1
<?php
2
3
namespace WP_Queue;
4
5
use Exception;
6
use WP_Queue\Connections\ConnectionInterface;
7
8
class Worker {
9
10
	/**
11
	 * @var ConnectionInterface
12
	 */
13
	protected $connection;
14
15
	/**
16
	 * @var int
17
	 */
18
	protected $attempts;
19
20
	/**
21
	 * Worker constructor.
22
	 *
23
	 * @param ConnectionInterface $connection
24
	 * @param int                 $attempts
25
	 */
26 2
	public function __construct( $connection, $attempts = 3 ) {
27 2
		$this->connection = $connection;
28 2
		$this->attempts   = $attempts;
29 2
	}
30
31
	/**
32
	 * Process a job on the queue.
33
	 *
34
	 * @return bool
35
	 */
36
	public function process() {
37
		$job = $this->connection->pop();
38
39
		if ( ! $job ) {
40
			return false;
41
		}
42
43
		$exception = null;
44
45
		try {
46
			$job->handle();
47
		} catch ( Exception $exception ) {
48
			$job->release();
49
		}
50
51
		if ( $job->attempts() >= $this->attempts ) {
52
			$job->fail();
53
		}
54
55
		if ( $job->failed() ) {
56
			$this->connection->failure( $job, $exception );
0 ignored issues
show
Bug introduced by
It seems like $job can also be of type true; however, parameter $job of WP_Queue\Connections\Con...ionInterface::failure() does only seem to accept WP_Queue\Job, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

56
			$this->connection->failure( /** @scrutinizer ignore-type */ $job, $exception );
Loading history...
Bug introduced by
It seems like $exception can also be of type null; however, parameter $exception of WP_Queue\Connections\Con...ionInterface::failure() does only seem to accept Exception, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

56
			$this->connection->failure( $job, /** @scrutinizer ignore-type */ $exception );
Loading history...
57
		} else if ( $job->released() ) {
58
			$this->connection->release( $job );
0 ignored issues
show
Bug introduced by
It seems like $job can also be of type true; however, parameter $job of WP_Queue\Connections\Con...ionInterface::release() does only seem to accept WP_Queue\Job, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

58
			$this->connection->release( /** @scrutinizer ignore-type */ $job );
Loading history...
59
		} else {
60
			$this->connection->delete( $job );
0 ignored issues
show
Bug introduced by
It seems like $job can also be of type true; however, parameter $job of WP_Queue\Connections\ConnectionInterface::delete() does only seem to accept WP_Queue\Job, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

60
			$this->connection->delete( /** @scrutinizer ignore-type */ $job );
Loading history...
61
		}
62
63
		return true;
64
	}
65
66
}