Worker   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 68.18%

Importance

Changes 0
Metric Value
dl 0
loc 60
ccs 15
cts 22
cp 0.6818
rs 10
c 0
b 0
f 0
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
C process() 0 32 7
1
<?php
2
3
namespace WP_Queue;
4
5
use Exception;
6
use WP_Queue\Connections\ConnectionInterface;
7
use WP_Queue\Exceptions\WorkerAttemptsExceededException;
8
9
class Worker {
10
11
	/**
12
	 * @var ConnectionInterface
13
	 */
14
	protected $connection;
15
16
	/**
17
	 * @var int
18
	 */
19
	protected $attempts;
20
21
	/**
22
	 * Worker constructor.
23
	 *
24
	 * @param ConnectionInterface $connection
25
	 * @param int                 $attempts
26
	 */
27 4
	public function __construct( $connection, $attempts = 3 ) {
28 4
		$this->connection = $connection;
29 4
		$this->attempts   = $attempts;
30 4
	}
31
32
	/**
33
	 * Process a job on the queue.
34
	 *
35
	 * @return bool
36
	 */
37 2
	public function process() {
38 2
		$job = $this->connection->pop();
39
40 2
		if ( ! $job ) {
41 1
			return false;
42
		}
43
44 1
		$exception = null;
45
46
		try {
47 1
			$job->handle();
48
		} catch ( Exception $exception ) {
49
			$job->release();
50
		}
51
52 1
		if ( $job->attempts() >= $this->attempts ) {
53
			if ( empty( $exception ) ) {
0 ignored issues
show
introduced by
The condition empty($exception) is always false.
Loading history...
54
				$exception = new WorkerAttemptsExceededException();
55
			}
56
			
57
			$job->fail();
58
		}
59
60 1
		if ( $job->failed() ) {
61
			$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

61
			$this->connection->failure( /** @scrutinizer ignore-type */ $job, $exception );
Loading history...
62 1
		} else if ( $job->released() ) {
63
			$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

63
			$this->connection->release( /** @scrutinizer ignore-type */ $job );
Loading history...
64
		} else {
65 1
			$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

65
			$this->connection->delete( /** @scrutinizer ignore-type */ $job );
Loading history...
66
		}
67
68 1
		return true;
69
	}
70
71
}