Standard::config()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 11
rs 10
cc 3
nc 3
nop 1
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2016-2025
6
 * @package Base
7
 * @subpackage MQueue
8
 */
9
10
11
namespace Aimeos\Base\MQueue\Manager;
12
13
14
/**
15
 * Standard message queue manager
16
 *
17
 * @package Base
18
 * @subpackage MQueue
19
 */
20
class Standard implements Iface
21
{
22
	private array $config;
23
	private array $objects = [];
24
25
26
	/**
27
	 * Initializes the object
28
	 *
29
	 * @param array $config Associative multi-dimensional configuration
30
	 */
31
	public function __construct( array $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 $key => $object ) {
43
			unset( $this->objects[$key] );
44
		}
45
	}
46
47
48
	/**
49
	 * Clones the objects inside.
50
	 */
51
	public function __clone()
52
	{
53
		$this->objects[] = [];
54
	}
55
56
57
	/**
58
	 * Clean up the objects inside
59
	 *
60
	 * @return array List of properties to serialize
61
	 */
62
	public function __sleep() : array
63
	{
64
		$this->__destruct();
65
		$this->objects = [];
66
67
		return array_keys( get_object_vars( $this ) );
68
	}
69
70
71
	/**
72
	 * Returns the message queue for the given name
73
	 *
74
	 * @param string $name Resource name of the message queue
75
	 * @return \Aimeos\Base\MQueue\Iface Message queue object
76
	 * @throws \Aimeos\Base\MQueue\Exception If an no configuration for that name is found
77
	 */
78
	public function get( string $name ) : \Aimeos\Base\MQueue\Iface
79
	{
80
		if( !isset( $this->objects[$name] ) ) {
81
			$this->objects[$name] = $this->create( $this->config( $name ) );
82
		}
83
84
		return $this->objects[$name];
85
	}
86
87
88
	/**
89
	 * Returns the configuration for the given name
90
	 *
91
	 * @param string $name Name of the resource, e.g. "mq" or "mq-email"
92
	 * @return array Configuration values
93
	 * @throws \Aimeos\Base\MQueue\Exception If an no configuration for that name is found
94
	 */
95
	protected function config( string $name ) : array
96
	{
97
		foreach( [$name, 'mq'] as $mqname )
98
		{
99
			if( isset( $this->config[$mqname] ) ) {
100
				return $this->config[$mqname];
101
			}
102
		}
103
104
		$msg = sprintf( 'No resource configuration for "%1$s" available', $name );
105
		throw new \Aimeos\Base\MQueue\Exception( $msg );
106
	}
107
108
109
	/**
110
	 * Creates and returns a new message queue object
111
	 *
112
	 * @param array $config Resource configuration
113
	 * @return \Aimeos\Base\MQueue\Iface Message queue object
114
	 * @throws \Aimeos\Base\MQueue\Exception if message queue class isn't found
115
	 */
116
	protected function create( array $config )
117
	{
118
		if( !isset( $config['adapter'] ) ) {
119
			throw new \Aimeos\Base\MQueue\Exception( 'Message queue not configured' );
120
		}
121
122
		$classname = '\Aimeos\Base\MQueue\\' . ucfirst( (string) $config['adapter'] );
123
124
		if( !class_exists( $classname ) ) {
125
			throw new \Aimeos\Base\MQueue\Exception( sprintf( 'Message queue "%1$s" not found', $config['adapter'] ) );
126
		}
127
128
		$config['db'] = $this->config[$config['db'] ?? 'db'] ?? [];
129
130
		return new $classname( $config );
131
	}
132
}
133