Passed
Push — master ( f2ee69...1fe896 )
by Paul
14:29 queued 06:54
created

DefaultsManager   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 48.28%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 25
dl 0
loc 71
ccs 14
cts 29
cp 0.4828
rs 10
c 1
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 3 1
A defaults() 0 7 1
A pluck() 0 4 1
A set() 0 8 1
A normalize() 0 9 2
A settings() 0 8 2
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Database;
4
5
use GeminiLabs\SiteReviews\Helpers\Arr;
6
7
class DefaultsManager
8
{
9
    /**
10
     * @return array
11
     */
12 7
    public function defaults()
13
    {
14 7
        $settings = $this->settings();
15 7
        $defaults = (array) array_combine(array_keys($settings), wp_list_pluck($settings, 'default'));
16 7
        return wp_parse_args($defaults, [
17 7
            'version' => '',
18
            'version_upgraded_from' => '0.0.0',
19
        ]);
20
    }
21
22
    /**
23
     * @return array
24
     */
25 7
    public function get()
26
    {
27 7
        return Arr::convertFromDotNotation($this->defaults());
28
    }
29
30
    /**
31
     * @param string $path
32
     * @return mixed
33
     */
34 2
    public function pluck($path)
35
    {
36 2
        $settings = Arr::convertFromDotNotation($this->settings());
37 2
        return Arr::get($settings, $path);
38
    }
39
40
    /**
41
     * @return array
42
     */
43
    public function set()
44
    {
45
        $settings = glsr(OptionManager::class)->all();
46
        $currentSettings = Arr::removeEmptyValues($settings);
47
        $defaultSettings = array_replace_recursive($this->get(), $currentSettings);
48
        $updatedSettings = array_replace_recursive($settings, $defaultSettings);
49
        update_option(OptionManager::databaseKey(), $updatedSettings);
50
        return $defaultSettings;
51
    }
52
53
    /**
54
     * @return array
55
     */
56 9
    public function settings()
57
    {
58 9
        static $settings;
59 9
        if (empty($settings)) {
60
            $settings = glsr()->filterArray('addon/settings', glsr()->config('settings'));
61
            $settings = $this->normalize($settings);
62
        }
63 9
        return $settings;
64
    }
65
66
    /**
67
     * @return array
68
     */
69
    protected function normalize(array $settings)
70
    {
71
        array_walk($settings, function (&$setting) {
72
            if (isset($setting['default'])) {
73
                return;
74
            }
75
            $setting['default'] = '';
76
        });
77
        return $settings;
78
    }
79
}
80