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

DefaultsManager::defaults()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
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