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