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        'blueprints' => 'state_blueprints',
28    );
29
30    /**
31     * Get the state data
32     *
33     * @param string $state The slice name to get
34     * @return array The state data
35     */
36    public static function get( string $state ): array {
37        $data = array();
38        if ( ! self::validate( $state ) ) {
39            return $data;
40        }
41
42        $data = \get_option( Options::get_option_name( self::$states[ $state ] ), false );
43        if ( ! $data ) {
44            $data = array();
45        }
46        return $data;
47    }
48
49    /**
50     * Update the input slice state
51     *
52     * @param string $state The slice name to update
53     * @param array  $data The update slice state
54     * @return bool True if the update was successful, false otherwise
55     */
56    public static function update( string $state, array $data ): bool {
57        if ( ! self::validate( $state, $data ) ) {
58            return false;
59        }
60
61        // Update the last updated time
62        $data['last_updated'] = time();
63
64        return \update_option( Options::get_option_name( self::$states[ $state ] ), $data );
65    }
66
67    /**
68     * Validate the state and data
69     *
70     * @param string     $state The slice name to update
71     * @param array|null $data The update slice state
72     * @return bool True if the update was successful, false otherwise
73     */
74    private static function validate( string $state, ?array $data = null ): bool {
75        // Validate the state
76        $slices = array_keys( self::$states );
77        if ( ! in_array( $state, $slices ) ) {
78            return false;
79        }
80
81        // Validate the data if it is provided
82        if ( null !== $data && empty( $data ) ) {
83            return false;
84        }
85
86        return true;
87    }
88}