Memory   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 32
c 1
b 0
f 0
dl 0
loc 102
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A get() 0 24 5
A getValueFromArray() 0 12 4
A set() 0 16 2
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2025
6
 * @package Base
7
 * @subpackage Config
8
 */
9
10
11
namespace Aimeos\Base\Config\Decorator;
12
13
14
/**
15
 * Memory caching decorator for config classes.
16
 *
17
 * @package Base
18
 * @subpackage Config
19
 */
20
class Memory
21
	extends \Aimeos\Base\Config\Decorator\Base
22
	implements \Aimeos\Base\Config\Decorator\Iface
23
{
24
	private array $negCache = [];
25
	private array $cache = [];
26
	private array $config;
27
28
29
	/**
30
	 * Initializes the decorator.
31
	 *
32
	 * @param \Aimeos\Base\Config\Iface $object Config object or decorator
33
	 * @param array $config Pre-cached non-shared configuration
34
	 */
35
	public function __construct( \Aimeos\Base\Config\Iface $object, array $config = [] )
36
	{
37
		parent::__construct( $object );
38
39
		$this->config = $config;
40
	}
41
42
43
	/**
44
	 * Returns the value of the requested config key.
45
	 *
46
	 * @param string $name Path to the requested value like tree/node/classname
47
	 * @param mixed $default Value returned if requested key isn't found
48
	 * @return mixed Value associated to the requested key
49
	 */
50
	public function get( string $name, $default = null )
51
	{
52
		$name = trim( $name, '/' );
53
54
		if( isset( $this->negCache[$name] ) ) {
55
			return $default;
56
		}
57
58
		if( array_key_exists( $name, $this->cache ) ) {
59
			return $this->cache[$name];
60
		}
61
62
		if( ( $value = $this->getValueFromArray( $this->config, explode( '/', $name ) ) ) === null ) {
63
			$value = parent::get( $name, null );
64
		}
65
66
		if( $value === null )
67
		{
68
			$this->negCache[$name] = true;
69
			return $default;
70
		}
71
72
		$this->cache[$name] = $value;
73
		return $value;
74
	}
75
76
77
	/**
78
	 * Sets the value for the specified key.
79
	 *
80
	 * @param string $name Path to the requested value like tree/node/classname
81
	 * @param string $value Value that should be associated with the given path
82
	 * @return \Aimeos\Base\Config\Iface Config instance for method chaining
83
	 */
84
	public function set( string $name, $value ) : \Aimeos\Base\Config\Iface
85
	{
86
		$name = trim( $name, '/' );
87
88
		if( $value !== null )
0 ignored issues
show
introduced by
The condition $value !== null is always true.
Loading history...
89
		{
90
			$this->cache[$name] = $value;
91
			unset( $this->negCache[$name] );
92
		}
93
		else
94
		{
95
			$this->negCache[$name] = true;
96
		}
97
98
		// don't store local configuration
99
		return $this;
100
	}
101
102
103
	/**
104
	 * Returns the requested configuration value from the given array
105
	 *
106
	 * @param array $config The array to search in
107
	 * @param array $parts Configuration path parts to look for inside the array
108
	 * @return mixed Found configuration value or null if not available
109
	 */
110
	protected function getValueFromArray( array $config, array $parts )
111
	{
112
		if( ( $key = array_shift( $parts ) ) !== null && isset( $config[$key] ) )
113
		{
114
			if( count( $parts ) > 0 ) {
115
				return $this->getValueFromArray( $config[$key], $parts );
116
			}
117
118
			return $config[$key];
119
		}
120
121
		return null;
122
	}
123
}
124