Completed
Push — master ( ac522a...86f5a9 )
by Ashley
01:46
created

QueueManager   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 54
ccs 14
cts 14
cp 1
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A connections() 0 7 1
A resolve() 0 6 2
A build() 0 10 2
1
<?php
2
3
namespace WP_Queue;
4
5
use WP_Queue\Connections\DatabaseConnection;
6
use WP_Queue\Connections\RedisConnection;
7
use WP_Queue\Exceptions\ConnectionNotFoundException;
8
9
class QueueManager {
10
11
	/**
12
	 * @var array
13
	 */
14
	protected static $instances = array();
15
16
	/**
17
	 * Resolve a Queue instance for required connection.
18
	 *
19
	 * @param string $connection
20
	 *
21
	 * @return Queue
22
	 */
23 2
	public static function resolve( $connection ) {
24 2
		if ( isset( static::$instances[ $connection ] ) ) {
25 1
			return static::$instances[ $connection ];
26
		}
27
28 2
		return static::build( $connection );
29
	}
30
31
	/**
32
	 * Build a queue instance.
33
	 *
34
	 * @param string $connection
35
	 *
36
	 * @return Queue
37
	 * @throws \Exception
38
	 */
39 2
	protected static function build( $connection ) {
40 2
		$connections = static::connections();
41
42 2
		if ( empty( $connections[ $connection ] ) ) {
43 1
			throw new ConnectionNotFoundException();
44
		}
45
46 1
		static::$instances[ $connection ] = new Queue( $connections[ $connection ], $connection );
47
48 1
		return static::$instances[ $connection ];
49
	}
50
51
	/**
52
	 * Get available connections.
53
	 *
54
	 * @return array
55
	 */
56 2
	protected static function connections() {
0 ignored issues
show
Coding Style introduced by
connections uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
57
		$connections = array(
58 2
			'database' => new DatabaseConnection( $GLOBALS['wpdb'] ),
59 2
			'redis'    => new RedisConnection(),
60
		);
61
62 2
		return apply_filters( 'wp_queue_connections', $connections );
63
	}
64
}