Passed
Push — master ( d86a4e...217e0a )
by Aimeos
02:12
created

Protect::set()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 12
rs 10
cc 3
nc 3
nop 2
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2016-2022
6
 * @package Base
7
 * @subpackage Config
8
 */
9
10
11
namespace Aimeos\Base\Config\Decorator;
12
13
14
/**
15
 * Protection decorator for config classes.
16
 *
17
 * @package Base
18
 * @subpackage Config
19
 */
20
class Protect
21
	extends \Aimeos\Base\Config\Decorator\Base
22
	implements \Aimeos\Base\Config\Decorator\Iface
23
{
24
	private $prefixes = [];
25
26
27
	/**
28
	 * Initializes the decorator
29
	 *
30
	 * @param \Aimeos\Base\Config\Iface $object Config object or decorator
31
	 * @param string[] $prefixes Allowed prefixes for getting and setting values
32
	 */
33
	public function __construct( \Aimeos\Base\Config\Iface $object, array $prefixes = [] )
34
	{
35
		parent::__construct( $object );
36
37
		foreach( $prefixes as $prefix ) {
38
			$this->prefixes[$prefix] = strlen( $prefix );
39
		}
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
	 * @throws \Aimeos\Base\Config\Exception If retrieving configuration isn't allowed
50
	 */
51
	public function get( string $name, $default = null )
52
	{
53
		foreach( $this->prefixes as $prefix => $len )
54
		{
55
			if( strncmp( $name, $prefix, $len ) === 0 ) {
56
				return parent::get( $name, $default );
57
			}
58
		}
59
60
		throw new \Aimeos\Base\Config\Exception( sprintf( 'Not allowed to access "%1$s" configuration', $name ) );
61
	}
62
}
63