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