Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 49
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
Tracker
0.00% covered (danger)
0.00%
0 / 49
0.00% covered (danger)
0.00%
0 / 7
306
0.00% covered (danger)
0.00%
0 / 1
 get_track_content
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
20
 update_track
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
12
 delete_track
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 reset
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
 set_path
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 set_file_name
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 get_full_path
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2namespace NewfoldLabs\WP\Module\Migration\Services;
3
4use NewfoldLabs\WP\Module\Migration\Steps\AbstractStep;
5
6/**
7 * Class to track migrations steps.
8 *
9 * @package wp-module-migration
10 */
11class Tracker {
12    /**
13     * Name of the tracking file.
14     *
15     * @var string $file_name.
16     */
17    protected $file_name = '.nfd-migration-tracking';
18    /**
19     * Path to the tracker file.
20     *
21     * @var string $path.
22     */
23    protected $path = ABSPATH;
24    /**
25     * Get the current step status.
26     *
27     * @return array
28     */
29    public function get_track_content() {
30        global $wp_filesystem;
31
32        // Make sure that the above variable is properly setup.
33        require_once ABSPATH . 'wp-admin/includes/file.php';
34        WP_Filesystem();
35
36        $file_path = $this->get_full_path();
37
38        if ( $wp_filesystem->exists( $file_path ) ) {
39            $track_content = $wp_filesystem->get_contents( $file_path );
40            $track_content = json_decode( $track_content, true );
41
42            if ( ! is_array( $track_content ) || empty( $track_content ) ) {
43                $track_content = array();
44            }
45        } else {
46            $track_content = array();
47        }
48
49        return $track_content;
50    }
51
52    /**
53     * Update the tracking file with the current step status
54     *
55     * @param AbstractStep $step the step to update.
56     * @return bool
57     */
58    public function update_track( AbstractStep $step ) {
59        global $wp_filesystem;
60
61        // Make sure that the above variable is properly setup.
62        require_once ABSPATH . 'wp-admin/includes/file.php';
63        WP_Filesystem();
64
65        $updated       = false;
66        $track_content = $this->get_track_content();
67        if ( $step && is_array( $track_content ) ) {
68            $datas         = array(
69                $step->get_step_slug() => array(
70                    'status'  => $step->get_status(),
71                    'intents' => $step->get_retry_count() + 1,
72                    'message' => $step->get_response()['message'] ?? '',
73                    'data'    => $step->get_data(),
74                    'time'    => current_time( 'mysql', 1 ),
75                ),
76            );
77            $updated_track = array_replace( $track_content, $datas );
78            $updated       = $wp_filesystem->put_contents( $this->get_full_path(), wp_json_encode( $updated_track ) );
79        }
80
81        return $updated;
82    }
83
84    /**
85     * Remove the tracking file
86     *
87     * @return bool
88     */
89    public function delete_track() {
90        global $wp_filesystem;
91
92        // Make sure that the above variable is properly setup.
93        require_once ABSPATH . 'wp-admin/includes/file.php';
94        WP_Filesystem();
95        $deleted = false;
96        if ( $wp_filesystem->exists( $this->get_full_path() ) ) {
97            $deleted = wp_delete_file( $this->get_full_path() );
98        }
99
100        return $deleted;
101    }
102
103    /**
104     * Reset the tracking file to an empty array to start from fresh.
105     *
106     * @return bool
107     */
108    public function reset() {
109        global $wp_filesystem;
110
111        // Make sure that the above variable is properly setup.
112        require_once ABSPATH . 'wp-admin/includes/file.php';
113        WP_Filesystem();
114
115        $path  = $this->get_full_path();
116        $reset = false;
117        if ( $wp_filesystem->exists( $path ) && $wp_filesystem->is_writable( $path ) ) {
118            $reset = $wp_filesystem->put_contents( $path, wp_json_encode( array() ) );
119        }
120
121        return $reset;
122    }
123
124    /**
125     * Set the tracker file path.
126     *
127     * @param string $path the path to the tracker file.
128     * @return void
129     */
130    public function set_path( $path ) {
131        $path       = ! empty( $path ) ? $path : ABSPATH;
132        $this->path = $path;
133    }
134    /**
135     * Set the tracker file name.
136     *
137     * @param string $file_name the name of the tracker file.
138     * @return void
139     */
140    public function set_file_name( $file_name ) {
141        $file_name       = ! empty( $file_name ) ? $file_name : '.nfd-migration-tracking';
142        $this->file_name = $file_name;
143    }
144
145    /**
146     * Get the tracker file path.
147     *
148     * @return string
149     */
150    private function get_full_path() {
151        return $this->path . $this->file_name;
152    }
153}