QueueManager::build()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace WP_Queue;
4
5
use WP_Queue\Connections\DatabaseConnection;
6
use WP_Queue\Connections\RedisConnection;
7
use WP_Queue\Connections\SyncConnection;
8
use WP_Queue\Exceptions\ConnectionNotFoundException;
9
10
class QueueManager {
11
12
	/**
13
	 * @var array
14
	 */
15
	protected static $instances = array();
16
17
	/**
18
	 * Resolve a Queue instance for required connection.
19
	 *
20
	 * @param string $connection
21
	 *
22
	 * @return Queue
23
	 */
24 3
	public static function resolve( $connection ) {
25 3
		if ( isset( static::$instances[ $connection ] ) ) {
26 1
			return static::$instances[ $connection ];
27
		}
28
29 2
		return static::build( $connection );
30
	}
31
32
	/**
33
	 * Build a queue instance.
34
	 *
35
	 * @param string $connection
36
	 *
37
	 * @return Queue
38
	 * @throws \Exception
39
	 */
40 2
	protected static function build( $connection ) {
41 2
		$connections = static::connections();
42
43 2
		if ( empty( $connections[ $connection ] ) ) {
44 1
			throw new ConnectionNotFoundException();
45
		}
46
47 1
		static::$instances[ $connection ] = new Queue( $connections[ $connection ] );
48
49 1
		return static::$instances[ $connection ];
50
	}
51
52
	/**
53
	 * Get available connections.
54
	 *
55
	 * @return array
56
	 */
57 2
	protected static function connections() {
58
		$connections = array(
59 2
			'database' => new DatabaseConnection( $GLOBALS['wpdb'] ),
60 2
			'redis'    => new RedisConnection(),
61 2
			'sync'     => new SyncConnection(),
62
		);
63
64 2
		return apply_filters( 'wp_queue_connections', $connections );
65
	}
66
}