SiteMetaManager::__get()   A
last analyzed

Complexity

Conditions 5
Paths 9

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 9
c 1
b 0
f 0
nc 9
nop 1
dl 0
loc 14
ccs 0
cts 10
cp 0
crap 30
rs 9.6111
1
<?php
2
3
namespace GeminiLabs\Pollux\MetaBox;
4
5
use GeminiLabs\Pollux\Settings\Settings;
6
7
/**
8
 * SiteMeta::all();
9
 * SiteMeta::group();
10
 * SiteMeta::group('option','fallback');
11
 * SiteMeta::get('group');
12
 * SiteMeta::get('group','option','fallback');
13
 *
14
 * @property object all
15
 */
16
class SiteMetaManager
17
{
18
	protected $options;
19
20
	public function __construct()
21
	{
22
		$this->options = get_option( Settings::id(), [] );
23
	}
24
25
	/**
26
	 * @param string $group
27
	 * @return object|array|null
28
	 */
29
	public function __call( $group, $args )
30
	{
31
		$args = array_pad( $args, 2, null );
32
		$group = $this->$group;
33
		if( is_object( $group )) {
34
			return $group;
35
		}
36
		return $this->get( $group, $args[0], $args[1] );
37
	}
38
39
	/**
40
	 * @param string $group
41
	 * @return object|array|null
42
	 */
43
	public function __get( $group )
44
	{
45
		if( $group == 'all' ) {
46
			return (object) $this->options;
47
		}
48
		if( empty( $group )) {
49
			$group = $this->getDefaultGroup();
50
		}
51
		if( is_array( $group )) {
0 ignored issues
show
introduced by
The condition is_array($group) is always false.
Loading history...
52
			$group = reset( $group );
53
		}
54
		return isset( $this->options[$group] )
55
			? $this->options[$group]
56
			: null;
57
	}
58
59
	/**
60
	 * @param string $group
61
	 * @param string|null $key
62
	 * @param mixed $fallback
63
	 * @return mixed
64
	 */
65
	public function get( $group = '', $key = '', $fallback = null )
66
	{
67
		if( func_num_args() < 1 ) {
68
			return $this->all;
69
		}
70
		if( is_string( $group )) {
0 ignored issues
show
introduced by
The condition is_string($group) is always true.
Loading history...
71
			$group = $this->$group;
72
		}
73
		if( !is_array( $group )) {
74
			return $fallback;
75
		}
76
		if( is_null( $key )) {
77
			return $group;
78
		}
79
		return $this->getValue( $group, $key, $fallback );
80
	}
81
82
	/**
83
	 * @return string
84
	 */
85
	protected function getDefaultGroup()
86
	{
87
		return '';
88
	}
89
90
	/**
91
	 * @param string $key
92
	 * @param mixed $fallback
93
	 * @return mixed
94
	 */
95
	protected function getValue( array $group, $key, $fallback )
96
	{
97
		if( !array_key_exists( $key, $group )) {
98
			return $fallback;
99
		}
100
		return empty( $group[$key] ) && !is_null( $fallback )
101
			? $fallback
102
			: $group[$key];
103
	}
104
}
105