Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 17 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
ReduxStateService | |
0.00% |
0 / 17 |
|
0.00% |
0 / 3 |
90 | |
0.00% |
0 / 1 |
get | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
update | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
validate | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
20 |
1 | <?php |
2 | |
3 | namespace NewfoldLabs\WP\Module\Onboarding\Services; |
4 | |
5 | use NewfoldLabs\WP\Module\Onboarding\Data\Options; |
6 | |
7 | class ReduxStateService { |
8 | |
9 | /** |
10 | * Slices/States — database option names |
11 | * |
12 | * @var array |
13 | */ |
14 | private static $states = array( |
15 | 'input' => 'state_input', |
16 | 'steps' => 'state_steps', |
17 | 'sitegen' => 'state_sitegen', |
18 | ); |
19 | |
20 | /** |
21 | * Get the state data |
22 | * |
23 | * @param string $state The slice name to get |
24 | * @return array The state data |
25 | */ |
26 | public static function get( string $state ): array { |
27 | $data = array(); |
28 | if ( ! self::validate( $state ) ) { |
29 | return $data; |
30 | } |
31 | |
32 | $data = \get_option( Options::get_option_name( self::$states[ $state ] ), false ); |
33 | if ( ! $data ) { |
34 | $data = array(); |
35 | } |
36 | return $data; |
37 | } |
38 | |
39 | /** |
40 | * Update the input slice state |
41 | * |
42 | * @param string $state The slice name to update |
43 | * @param array $data The update slice state |
44 | * @return bool True if the update was successful, false otherwise |
45 | */ |
46 | public static function update( string $state, array $data ): bool { |
47 | if ( ! self::validate( $state, $data ) ) { |
48 | return false; |
49 | } |
50 | |
51 | // Update the last updated time |
52 | $data['last_updated'] = time(); |
53 | |
54 | return \update_option( Options::get_option_name( self::$states[ $state ] ), $data ); |
55 | } |
56 | |
57 | /** |
58 | * Validate the state and data |
59 | * |
60 | * @param string $state The slice name to update |
61 | * @param array|null $data The update slice state |
62 | * @return bool True if the update was successful, false otherwise |
63 | */ |
64 | private static function validate( string $state, ?array $data = null ): bool { |
65 | // Validate the state |
66 | $slices = array_keys( self::$states ); |
67 | if ( ! in_array( $state, $slices ) ) { |
68 | return false; |
69 | } |
70 | |
71 | // Validate the data if it is provided |
72 | if ( null !== $data && empty( $data ) ) { |
73 | return false; |
74 | } |
75 | |
76 | return true; |
77 | } |
78 | } |