Passed
Push — master ( dc3967...a95317 )
by Paul
07:16
created

DefaultsManager   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 95%

Importance

Changes 0
Metric Value
dl 0
loc 45
ccs 19
cts 20
cp 0.95
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A normalize() 0 7 2
A set() 0 8 1
A settings() 0 5 1
A get() 0 5 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 get()
14
	{
15 7
		$settings = $this->settings();
16 7
		$defaults = array_combine( array_keys( $settings ), array_column( $settings, 'default' ));
17 7
		return glsr( Helper::class )->convertDotNotationArray( $defaults );
18
	}
19
20
	/**
21
	 * @return array
22
	 */
23 7
	public function set()
24
	{
25 7
		$settings = glsr( OptionManager::class )->all();
26 7
		$currentSettings = glsr( Helper::class )->removeEmptyArrayValues( $settings );
27 7
		$defaultSettings = array_replace_recursive( $this->get(), $currentSettings );
28 7
		$updatedSettings = array_replace_recursive( $settings, $defaultSettings );
29 7
		update_option( OptionManager::databaseKey(), $updatedSettings );
30 7
		return $defaultSettings;
31
	}
32
33
	/**
34
	 * @return array
35
	 */
36 7
	public function settings()
37
	{
38 7
		$settings = include glsr()->path( 'config/settings.php' );
39 7
		$settings = apply_filters( 'site-reviews/addon/settings', $settings );
40 7
		return $this->normalize( $settings );
41
	}
42
43
	/**
44
	 * @return array
45
	 */
46
	protected function normalize( array $settings )
47
	{
48 7
		array_walk( $settings, function( &$setting ) {
49 7
			if( isset( $setting['default'] ))return;
50
			$setting['default'] = null;
51 7
		});
52 7
		return $settings;
53
	}
54
}
55