Protect   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
eloc 17
c 2
b 0
f 0
dl 0
loc 55
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 3
A get() 0 18 5
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2016-2026
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 array $allow = [];
25
	private array $deny = [];
26
27
28
	/**
29
	 * Initializes the decorator
30
	 *
31
	 * @param \Aimeos\Base\Config\Iface $object Config object or decorator
32
	 * @param string[] $allow Allowed prefixes for getting and setting values
33
	 * @param string[] $deny Denied prefixes for getting and setting values
34
	 */
35
	public function __construct( \Aimeos\Base\Config\Iface $object, array $allow = [], array $deny = [] )
36
	{
37
		parent::__construct( $object );
38
39
		foreach( $allow as $prefix ) {
40
			$this->allow[] = '#^' . str_replace( '*', '[^/]+', $prefix ) . '#';
41
		}
42
43
		foreach( $deny as $prefix ) {
44
			$this->deny[] = '#^' . str_replace( '*', '[^/]+', $prefix ) . '#';
45
		}
46
	}
47
48
49
	/**
50
	 * Returns the value of the requested config key
51
	 *
52
	 * @param string $name Path to the requested value like tree/node/classname
53
	 * @param mixed $default Value returned if requested key isn't found
54
	 * @return mixed Value associated to the requested key
55
	 * @throws \Aimeos\Base\Config\Exception If retrieving configuration isn't allowed
56
	 */
57
	public function get( string $name, $default = null )
58
	{
59
		foreach( $this->deny as $deny )
60
		{
61
			if( preg_match( $deny, $name ) === 1 )
62
			{
63
				foreach( $this->allow as $allow )
64
				{
65
					if( preg_match( $allow, $name ) === 1 ) {
66
						return parent::get( $name, $default );
67
					}
68
				}
69
70
				throw new \Aimeos\Base\Config\Exception( sprintf( 'Not allowed to access "%1$s" configuration', $name ) );
71
			}
72
		}
73
74
		return parent::get( $name, $default );
75
	}
76
}
77