Completed
Push — master ( d47874...c19952 )
by Ashley
02:14
created

Worker::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 3
ccs 3
cts 3
cp 1
crap 1
rs 10
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 4
	public function __construct( $connection, $attempts = 3 ) {
27 4
		$this->connection = $connection;
28 4
		$this->attempts   = $attempts;
29 4
	}
30
31
	/**
32
	 * Process a job on the queue.
33
	 *
34
	 * @return bool
35
	 */
36 2
	public function process() {
37 2
		$job = $this->connection->pop();
38
39 2
		if ( ! $job ) {
40 1
			return false;
41
		}
42
43 1
		$exception = null;
44
45
		try {
46 1
			$job->handle();
47 1
		} catch ( Exception $exception ) {
48
			$job->release();
49
		}
50
51 1
		if ( $job->attempts() >= $this->attempts ) {
52
			$job->fail();
53
		}
54
55 1
		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 1
		} 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 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

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