PluginProbe ʕ •ᴥ•ʔ
WooCommerce Square / 2.2.2
WooCommerce Square v2.2.2
5.5.0 5.4.3 5.4.2 5.4.1 5.4.0 trunk 1.0.25 1.0.26 1.0.27 1.0.28 1.0.29 1.0.30 1.0.31 1.0.32 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.38 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.7.0 2.8.0 2.9.0 2.9.1 3.0.0 3.0.1 3.0.2 3.0.3 3.1.0 3.2.0 3.3.0 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.6.1 3.7.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 4.0.0 4.1.0 4.2.0 4.2.1 4.2.2 4.2.3 4.3.0 4.3.1 4.3.2 4.4.0 4.4.1 4.4.2 4.5.0 4.5.1 4.5.2 4.6.0 4.6.1 4.6.2 4.6.3 4.6.4 4.7.0 4.7.1 4.7.2 4.7.3 4.7.4 4.8.0 4.8.1 4.8.2 4.8.3 4.8.4 4.8.5 4.8.6 4.8.7 4.8.8 4.9.0 4.9.1 4.9.2 4.9.3 4.9.4 4.9.5 4.9.6 4.9.7 4.9.8 4.9.9 5.0.0 5.0.1 5.1.0 5.1.1 5.1.2 5.2.0 5.3.0 5.3.1 5.3.2 5.3.3
woocommerce-square / includes / Sync / Stepped_Job.php
woocommerce-square / includes / Sync Last commit date
Records 6 years ago Catalog_Item.php 6 years ago Interval_Polling.php 6 years ago Job.php 6 years ago Manual_Synchronization.php 6 years ago Product_Import.php 5 years ago Records.php 6 years ago Stepped_Job.php 6 years ago
Stepped_Job.php
234 lines
1 <?php
2 /**
3 * WooCommerce Square
4 *
5 * This source file is subject to the GNU General Public License v3.0
6 * that is bundled with this package in the file license.txt.
7 * It is also available through the world-wide-web at this URL:
8 * http://www.gnu.org/licenses/gpl-3.0.html
9 * If you did not receive a copy of the license and are unable to
10 * obtain it through the world-wide-web, please send an email
11 * to license@woocommerce.com so we can send you a copy immediately.
12 *
13 * DISCLAIMER
14 *
15 * Do not edit or add to this file if you wish to upgrade WooCommerce Square to newer
16 * versions in the future. If you wish to customize WooCommerce Square for your
17 * needs please refer to https://docs.woocommerce.com/document/woocommerce-square/
18 *
19 * @author WooCommerce
20 * @copyright Copyright: (c) 2019, Automattic, Inc.
21 * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0
22 */
23
24 namespace WooCommerce\Square\Sync;
25
26 use SkyVerge\WooCommerce\PluginFramework\v5_4_0 as Framework;
27
28 defined( 'ABSPATH' ) || exit;
29
30 /**
31 * Stepped Job abstract.
32 *
33 * Adds multi-step management to the job class.
34 *
35 * @since 2.0.0
36 */
37 abstract class Stepped_Job extends Job {
38
39
40 /**
41 * Executes the next step of this job.
42 *
43 * @since 2.0.0
44 *
45 * @return \stdClass the job object
46 */
47 public function run() {
48
49 parent::run();
50
51 if ( empty( $this->get_attr( 'next_steps' ) ) && empty( $this->get_attr( 'completed_steps' ) ) ) {
52 $this->assign_next_steps();
53 }
54
55 $this->do_next_step();
56
57 return $this->job;
58 }
59
60
61 /**
62 * Assigns the next steps needed for this sync job.
63 *
64 * Adds the next steps to the 'next_steps' attribute.
65 *
66 * @since 2.0.0
67 */
68 abstract protected function assign_next_steps();
69
70
71 /**
72 * Gets the next step in the sync process.
73 *
74 * @since 2.0.0
75 *
76 * @return string|null
77 */
78 protected function get_next_step() {
79
80 $next_steps = $this->get_next_steps();
81
82 return isset( $next_steps[0] ) ? $next_steps[0] : null;
83 }
84
85
86 /**
87 * Gets the next steps for the sync process.
88 *
89 * @since 2.0.0
90 *
91 * @return string[]
92 */
93 protected function get_next_steps() {
94
95 return $this->get_attr( 'next_steps' );
96 }
97
98
99 /**
100 * Performs the next step in the sync process.
101 *
102 * @since 2.0.0
103 */
104 protected function do_next_step() {
105
106 $next_step = $this->get_next_step();
107
108 if ( is_callable( array( $this, $next_step ) ) ) {
109
110 $this->start_step_cycle( $next_step );
111
112 try {
113
114 $this->$next_step();
115 $this->complete_step_cycle( $next_step );
116
117 } catch ( Framework\SV_WC_Plugin_Exception $exception ) {
118
119 $this->complete_step_cycle( $next_step, false, $exception->getMessage() );
120 $this->fail( $exception->getMessage() );
121 return;
122 }
123 }
124
125 if ( ! $this->get_next_step() ) {
126
127 $this->complete();
128 }
129 }
130
131
132 /**
133 * Records the beginning of a new step cycle, meaning a new loop on the job for a given step.
134 *
135 * @since 2.0.0
136 *
137 * @param string $step_name the step name
138 */
139 protected function start_step_cycle( $step_name ) {
140
141 $current_step_cycle = array(
142 'step_name' => $step_name,
143 'start_time' => microtime( true ),
144 );
145
146 wc_square()->log( "Starting step cycle: $step_name" );
147
148 $this->set_attr( 'current_step_cycle', $current_step_cycle );
149 }
150
151
152 /**
153 * Records the completion of a step cycle.
154 *
155 * @since 2.0.0
156 *
157 * @param string $step_name the step name
158 * @param bool $is_successful (optional) whether the step completion is from a success or not
159 * @param string $error_message (optional) error message to include with failed step log
160 */
161 protected function complete_step_cycle( $step_name, $is_successful = true, $error_message = '' ) {
162
163 $current_step_cycle = $this->get_attr( 'current_step_cycle', array() );
164
165 if ( ! empty( $current_step_cycle ) ) {
166
167 $current_step_cycle['end_time'] = microtime( true );
168 $current_step_cycle['runtime'] = number_format( $current_step_cycle['end_time'] - $current_step_cycle['start_time'], 2 ) . 's';
169 $current_step_cycle['success'] = true === $is_successful;
170
171 if ( true === $is_successful ) {
172
173 wc_square()->log( "Completed step cycle: $step_name (${current_step_cycle['runtime']})" );
174
175 } else {
176
177 wc_square()->log( "Failed step cycle: $step_name (${current_step_cycle['runtime']}) - $error_message" );
178 }
179
180 $completed_cycles = $this->get_attr( 'completed_step_cycles', array() );
181 $completed_cycles[] = $current_step_cycle;
182 $this->set_attr( 'completed_step_cycles', $completed_cycles );
183 }
184 }
185
186
187 /**
188 * Completes the specified step (if it's the next step).
189 *
190 * @since 2.0.0
191 *
192 * @param string $step_name
193 */
194 protected function complete_step( $step_name ) {
195
196 $next_steps = $this->get_next_steps();
197
198 if ( isset( $next_steps[0] ) && $step_name === $next_steps[0] ) {
199
200 $this->add_completed_step( $step_name );
201 array_shift( $next_steps );
202 $this->set_attr( 'next_steps', $next_steps );
203 }
204 }
205
206
207 /**
208 * Adds a step to the completed steps array.
209 *
210 * @since 2.0.0
211 *
212 * @param string $step_name
213 */
214 protected function add_completed_step( $step_name ) {
215
216 if ( empty( $step_name ) ) {
217 return;
218 }
219
220 $completed_steps = $this->get_attr( 'completed_steps', array() );
221
222 $completed_steps[] = array(
223 'name' => $step_name,
224 'completion_time' => current_time( 'mysql' ),
225 );
226
227 $this->set_attr( 'completed_steps', $completed_steps );
228
229 wc_square()->log( 'Completed job step: ' . $step_name );
230 }
231
232
233 }
234