Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 30 |
|
0.00% |
0 / 17 |
CRAP | |
0.00% |
0 / 1 |
| AbstractStep | |
0.00% |
0 / 30 |
|
0.00% |
0 / 17 |
812 | |
0.00% |
0 / 1 |
| run | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
| success | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| failure | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| failed | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| retry | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
| set_step_slug | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| get_step_slug | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| set_max_retries | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| set_retry_count | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| get_retry_count | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_max_retries | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_status | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| set_status | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_response | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| set_response | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
12 | |||
| set_tracker | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| set_data | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
12 | |||
| get_data | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | namespace NewfoldLabs\WP\Module\Migration\Steps; |
| 3 | |
| 4 | use NewfoldLabs\WP\Module\Migration\Services\Tracker; |
| 5 | |
| 6 | /** |
| 7 | * Abstract class representing a step in the migration process. |
| 8 | * |
| 9 | * This class provides a base implementation for all migration steps. |
| 10 | * It should be extended by specific step classes to define the actual |
| 11 | * step logic. |
| 12 | * |
| 13 | * @package wp-module-migration |
| 14 | */ |
| 15 | abstract class AbstractStep { |
| 16 | /** |
| 17 | * The actual retry count, it will increment on each retry. |
| 18 | * |
| 19 | * @var int $retry_count |
| 20 | */ |
| 21 | protected $retry_count = 0; |
| 22 | |
| 23 | /** |
| 24 | * The maximum retries possible. |
| 25 | * |
| 26 | * @var int $max_retries |
| 27 | */ |
| 28 | protected $max_retries = 0; |
| 29 | |
| 30 | /** |
| 31 | * The possible statuses for the step. |
| 32 | * |
| 33 | * @var array $statuses |
| 34 | */ |
| 35 | public $statuses = array( |
| 36 | 'running' => 'running', |
| 37 | 'completed' => 'completed', |
| 38 | 'failed' => 'failed', |
| 39 | 'aborted' => 'aborted', |
| 40 | ); |
| 41 | |
| 42 | /** |
| 43 | * Status of the current step, it could be success, running or failed |
| 44 | * |
| 45 | * @var string $status |
| 46 | */ |
| 47 | protected $status; |
| 48 | |
| 49 | /** |
| 50 | * The current step slug. |
| 51 | * |
| 52 | * @var string $step_slug |
| 53 | */ |
| 54 | protected $step_slug = ''; |
| 55 | |
| 56 | /** |
| 57 | * Collect response messages. |
| 58 | * |
| 59 | * @var array $response |
| 60 | */ |
| 61 | protected $response = array(); |
| 62 | |
| 63 | /** |
| 64 | * Collect datas for the step if any. |
| 65 | * |
| 66 | * @var array $response |
| 67 | */ |
| 68 | protected $datas = array(); |
| 69 | |
| 70 | /** |
| 71 | * Tracker class instance. |
| 72 | * |
| 73 | * @var Tracker $tracker |
| 74 | */ |
| 75 | protected $tracker; |
| 76 | |
| 77 | /** |
| 78 | * Run the main code for. |
| 79 | */ |
| 80 | protected function run() {} |
| 81 | |
| 82 | /** |
| 83 | * Set the step status as successful & reset the retry count to 0 and print success log. |
| 84 | */ |
| 85 | protected function success() { |
| 86 | $this->set_status( $this->statuses['completed'] ); |
| 87 | $this->set_retry_count( 0 ); |
| 88 | } |
| 89 | /** |
| 90 | * Set the step status as failed & reset the retry count to 0 and print failed log. |
| 91 | */ |
| 92 | protected function failure() { |
| 93 | $this->set_status( $this->statuses['failed'] ); |
| 94 | } |
| 95 | /** |
| 96 | * Check if the step is completed. |
| 97 | * |
| 98 | * @return bool |
| 99 | */ |
| 100 | public function failed() { |
| 101 | return $this->get_status() === 'failed'; |
| 102 | } |
| 103 | /** |
| 104 | * Retry the run method. |
| 105 | * |
| 106 | * @return bool; |
| 107 | */ |
| 108 | protected function retry() { |
| 109 | $count = $this->retry_count + 1; |
| 110 | if ( $count >= $this->get_max_retries() ) { |
| 111 | $this->failure(); |
| 112 | return $this; |
| 113 | } |
| 114 | |
| 115 | sleep( 1 ); |
| 116 | |
| 117 | $this->set_retry_count( $count ); |
| 118 | |
| 119 | $this->run(); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Set the current step slug |
| 124 | * |
| 125 | * @param string $slug the retry count value. |
| 126 | */ |
| 127 | protected function set_step_slug( string $slug ) { |
| 128 | $this->step_slug = empty( $slug ) ? 'generic' : $slug; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Get the current step slug |
| 133 | * |
| 134 | * @return string |
| 135 | */ |
| 136 | public function get_step_slug() { |
| 137 | return $this->step_slug; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Set the max retry value |
| 142 | * |
| 143 | * @param int $max the max number of retries. |
| 144 | */ |
| 145 | protected function set_max_retries( int $max ) { |
| 146 | $this->max_retries = $max < 0 ? 0 : $max; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Set the retry value |
| 151 | * |
| 152 | * @param int $retry_count the retry count value. |
| 153 | */ |
| 154 | protected function set_retry_count( int $retry_count ) { |
| 155 | $this->retry_count = $retry_count > $this->max_retries ? $this->max_retries : $retry_count; |
| 156 | } |
| 157 | /** |
| 158 | * Get the actual retry count |
| 159 | * |
| 160 | * @return int |
| 161 | */ |
| 162 | public function get_retry_count() { |
| 163 | return (int) $this->retry_count; |
| 164 | } |
| 165 | /** |
| 166 | * Get the max retries count |
| 167 | * |
| 168 | * @return int |
| 169 | */ |
| 170 | public function get_max_retries() { |
| 171 | return (int) $this->max_retries; |
| 172 | } |
| 173 | /** |
| 174 | * Get the status |
| 175 | * |
| 176 | * @return int |
| 177 | */ |
| 178 | public function get_status() { |
| 179 | return $this->status; |
| 180 | } |
| 181 | /** |
| 182 | * Set the status |
| 183 | * |
| 184 | * @param string $status the status; |
| 185 | */ |
| 186 | public function set_status( $status ) { |
| 187 | $this->status = $status; |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Get the response |
| 192 | * |
| 193 | * @return array |
| 194 | */ |
| 195 | public function get_response() { |
| 196 | return $this->response; |
| 197 | } |
| 198 | /** |
| 199 | * Set the response |
| 200 | * |
| 201 | * @param array $response the response; |
| 202 | */ |
| 203 | public function set_response( $response ) { |
| 204 | $response = empty( $response ) || ! is_array( $response ) ? array() : $response; |
| 205 | $this->response = $response; |
| 206 | } |
| 207 | /** |
| 208 | * Set the tracker instance for the step |
| 209 | * |
| 210 | * @param Tracker $tracker the tracker instance. |
| 211 | */ |
| 212 | public function set_tracker( Tracker $tracker ) { |
| 213 | $this->tracker = $tracker; |
| 214 | } |
| 215 | /** |
| 216 | * Get the data by data key. |
| 217 | * |
| 218 | * @param string $data_key the data key to set. |
| 219 | * @param mixed $data_value the data value to set. |
| 220 | * @return void |
| 221 | */ |
| 222 | protected function set_data( $data_key, $data_value ) { |
| 223 | if ( ! empty( $data_key ) && is_string( $data_key ) ) { |
| 224 | $this->datas[ $data_key ] = $data_value; |
| 225 | } |
| 226 | } |
| 227 | /** |
| 228 | * Get the data by data key, or empty string if not isset, or all datas if param is empty. |
| 229 | * |
| 230 | * @param string $data_key the data key to get. |
| 231 | * @return string |
| 232 | */ |
| 233 | public function get_data( $data_key = '' ) { |
| 234 | if ( isset( $this->datas[ $data_key ] ) ) { |
| 235 | return $this->datas[ $data_key ]; |
| 236 | } |
| 237 | |
| 238 | if ( empty( $data_key ) ) { |
| 239 | return $this->datas; |
| 240 | } |
| 241 | return ''; |
| 242 | } |
| 243 | } |