Passed
Push — master ( 364259...237be9 )
by Paul
04:18
created

OptionManager::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
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\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, $fallback, $this->all() );
60
	}
61
62
	/**
63
	 * @param string|array $pathOrOptions
64
	 * @param mixed $value
65
	 * @return bool
66
	 */
67 2
	public function set( $pathOrOptions, $value = '' )
68
	{
69 2
		if( is_string( $pathOrOptions )) {
70 2
			$pathOrOptions = glsr( Helper::class )->setPathValue( $pathOrOptions, $value, $this->all() );
71
		}
72 2
		return update_option( static::databaseKey(), (array)$pathOrOptions );
73
	}
74
}
75