Passed
Push — master ( ceec33...d1be46 )
by Paul
05:18
created

DefaultsManager   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 95.24%

Importance

Changes 0
Metric Value
dl 0
loc 52
ccs 20
cts 21
cp 0.9524
rs 10
c 0
b 0
f 0
wmc 6

5 Methods

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