Passed
Push — master ( ece31d...41b8a6 )
by Paul
10:20 queued 04:17
created

DefaultsManager::normalize()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 9
ccs 5
cts 6
cp 0.8333
crap 2.0185
rs 10
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Database;
4
5
use GeminiLabs\SiteReviews\Helper;
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), glsr_array_column($settings, 'default'));
16 7
        return wp_parse_args($defaults, [
17 7
            'version' => '',
18
            'version_upgraded_from' => '',
19
        ]);
20
    }
21
22
    /**
23
     * @return array
24
     */
25 7
    public function get()
26
    {
27 7
        return glsr(Helper::class)->convertDotNotationArray($this->defaults());
28
    }
29
30
    /**
31
     * @return array
32
     */
33 7
    public function set()
34
    {
35 7
        $settings = glsr(OptionManager::class)->all();
36 7
        $currentSettings = glsr(Helper::class)->removeEmptyArrayValues($settings);
37 7
        $defaultSettings = array_replace_recursive($this->get(), $currentSettings);
38 7
        $updatedSettings = array_replace_recursive($settings, $defaultSettings);
39 7
        update_option(OptionManager::databaseKey(), $updatedSettings);
40 7
        return $defaultSettings;
41
    }
42
43
    /**
44
     * @return array
45
     */
46 7
    public function settings()
47
    {
48 7
        $settings = apply_filters('site-reviews/addon/settings', glsr()->config('settings'));
49 7
        return $this->normalize($settings);
50
    }
51
52
    /**
53
     * @return array
54
     */
55
    protected function normalize(array $settings)
56
    {
57 7
        array_walk($settings, function (&$setting) {
58 7
            if (isset($setting['default'])) {
59 7
                return;
60
            }
61
            $setting['default'] = '';
62 7
        });
63 7
        return $settings;
64
    }
65
}
66