Completed
Push — master ( 212c5f...8d6a89 )
by Aimeos
08:59
created

Standard::__clone()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
rs 9.4285
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2016
6
 * @package MW
7
 * @subpackage MQueue
8
 */
9
10
11
namespace Aimeos\MW\MQueue\Manager;
12
13
14
/**
15
 * Standard message queue manager
16
 *
17
 * @package MW
18
 * @subpackage MQueue
19
 */
20
class Standard implements Iface
21
{
22
	private $config;
23
	private $objects = [];
24
25
26
	/**
27
	 * Initializes the object
28
	 *
29
	 * @param \Aimeos\MW\Config\Iface $config Configuration object
30
	 */
31
	public function __construct( \Aimeos\MW\Config\Iface $config )
32
	{
33
		$this->config = $config;
34
	}
35
36
37
	/**
38
	 * Cleans up the object
39
	 */
40
	public function __destruct()
41
	{
42
		foreach( $this->objects as $object ) {
43
			unset( $object );
44
		}
45
	}
46
47
48
	/**
49
	 * Clones the objects inside.
50
	 */
51
	public function __clone()
52
	{
53
		$this->config = clone $this->config;
54
55
		foreach( $this->objects as $resource => $object ) {
56
			unset( $this->objects[$resource] );
57
		}
58
	}
59
60
61
	/**
62
	 * Returns the message queue for the given name
63
	 *
64
	 * @param string $resource Resource name of the message queue
65
	 * @return \Aimeos\MW\MQueue\Iface Message queue object
66
	 * @throws \Aimeos\MW\MQueue\Exception If an no configuration for that name is found
67
	 */
68
	public function get( $resource )
69
	{
70
		$conf = (array) $this->getConfig( $resource );
71
72
		if( isset( $conf['db'] ) && is_string( $conf['db'] ) ) {
73
			$conf['db'] = (array) $this->getConfig( $conf['db'] );
74
		}
75
76
		if( !isset( $this->objects[$resource] ) ) {
77
			$this->objects[$resource] = \Aimeos\MW\MQueue\Factory::create( $conf );
78
		}
79
80
		return $this->objects[$resource];
81
	}
82
83
84
	/**
85
	 * Returns the configuration for the given name
86
	 *
87
	 * @param string &$resource Name of the resource, e.g. "mq" or "mq-email"
88
	 * @return array|string Configuration values
89
	 * @throws \Aimeos\MW\MQueue\Exception If an no configuration for that name is found
90
	 */
91
	protected function getConfig( &$resource )
92
	{
93
		if( ( $conf = $this->config->get( 'resource/' . $resource ) ) !== null ) {
94
			return $conf;
95
		}
96
97
		$resource = 'mq';
98
		if( ( $conf = $this->config->get( 'resource/mq' ) ) !== null ) {
99
			return $conf;
100
		}
101
102
		$msg = sprintf( 'No resource configuration for "%1$s" available', $resource );
103
		throw new \Aimeos\MW\MQueue\Exception( $msg );
104
	}
105
}
106