SiteMeta   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
eloc 32
c 4
b 1
f 0
dl 0
loc 87
rs 10
wmc 19

6 Methods

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