| Total Complexity | 5 |
| Total Lines | 54 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 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() { |
|
| 63 | } |
||
| 64 | } |
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: