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

OptionManager::normalize()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 1
nop 1
dl 0
loc 11
ccs 0
cts 9
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Database;
4
5
use GeminiLabs\SiteReviews\Application;
6
use GeminiLabs\SiteReviews\Helper;
7
use GeminiLabs\SiteReviews\Database\DefaultsManager;
8
9
class OptionManager
10
{
11
	/**
12
	 * @return string
13
	 */
14 7
	public static function databaseKey()
15
	{
16 7
		return glsr( Helper::class )->snakeCase(
17 7
			Application::ID.'-v'.explode( '.', glsr()->version )[0]
18
		);
19
	}
20
21
	/**
22
	 * @return array
23
	 */
24 7
	public function all()
25
	{
26 7
		$options = get_option( static::databaseKey(), [] );
27 7
		if( !is_array( $options )) {
28
			delete_option( static::databaseKey() );
29
			$options = ['settings' => []];
30
		}
31 7
		return $options;
32
	}
33
34
	/**
35
	 * @param string $path
36
	 * @return bool
37
	 */
38
	public function delete( $path )
39
	{
40
		$keys = explode( '.', $path );
41
		$last = array_pop( $keys );
42
		$options = $this->all();
43
		$pointer = &$options;
44
		foreach( $keys as $key ) {
45
			if( !isset( $pointer[$key] ) || !is_array( $pointer[$key] ))continue;
46
			$pointer = &$pointer[$key];
47
		}
48
		unset( $pointer[$last] );
49
		return $this->set( $options );
50
	}
51
52
	/**
53
	 * @param string $path
54
	 * @param mixed $fallback
55
	 * @return mixed
56
	 */
57 7
	public function get( $path = '', $fallback = '' )
58
	{
59 7
		return glsr( Helper::class )->getPathValue( $path, $this->all(), $fallback );
60
	}
61
62
	/**
63
	 * @return string
64
	 */
65
	public function json()
66
	{
67
		return json_encode( $this->all() );
68
	}
69
70
	/**
71
	 * @return array
72
	 */
73
	public function normalize( array $options = [] )
74
	{
75
		$options = wp_parse_args(
76
			glsr( Helper::class )->flattenArray( $options ),
77
			glsr( DefaultsManager::class )->defaults()
78
		);
79
		array_walk( $options, function( &$value, $key ) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

79
		array_walk( $options, function( &$value, /** @scrutinizer ignore-unused */ $key ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
80
			if( !is_string( $value ))return;
81
			$value = wp_kses( $value, wp_kses_allowed_html( 'post' ));
82
		});
83
		return glsr( Helper::class )->convertDotNotationArray( $options );
84
	}
85
86
	/**
87
	 * @param string|array $pathOrOptions
88
	 * @param mixed $value
89
	 * @return bool
90
	 */
91 7
	public function set( $pathOrOptions, $value = '' )
92
	{
93 7
		if( is_string( $pathOrOptions )) {
94 7
			$pathOrOptions = glsr( Helper::class )->setPathValue( $pathOrOptions, $value, $this->all() );
95
		}
96 7
		return update_option( static::databaseKey(), (array)$pathOrOptions );
97
	}
98
}
99