Passed
Push — master ( 796b78...de3336 )
by Paul
05:32
created

DefaultsManager::set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

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