@@ -24,203 +24,203 @@ |
||
24 | 24 | */ |
25 | 25 | class BatchRequestProcessor |
26 | 26 | { |
27 | - // phpcs:disable PSR2.Classes.PropertyDeclaration.Underscore |
|
28 | - // phpcs:disable PSR2.Methods.MethodDeclaration.Underscore |
|
29 | - /** |
|
30 | - * Current job's ID (if assigned) |
|
31 | - * |
|
32 | - * @var string|null |
|
33 | - */ |
|
34 | - protected $_job_id; |
|
27 | + // phpcs:disable PSR2.Classes.PropertyDeclaration.Underscore |
|
28 | + // phpcs:disable PSR2.Methods.MethodDeclaration.Underscore |
|
29 | + /** |
|
30 | + * Current job's ID (if assigned) |
|
31 | + * |
|
32 | + * @var string|null |
|
33 | + */ |
|
34 | + protected $_job_id; |
|
35 | 35 | |
36 | - /** |
|
37 | - * Current job's parameters |
|
38 | - * |
|
39 | - * @var JobParameters|null |
|
40 | - */ |
|
41 | - protected $_job_parameters; |
|
36 | + /** |
|
37 | + * Current job's parameters |
|
38 | + * |
|
39 | + * @var JobParameters|null |
|
40 | + */ |
|
41 | + protected $_job_parameters; |
|
42 | 42 | |
43 | - // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
44 | - /** |
|
45 | - * Creates a job for the specified batch handler class (which should be autoloaded) |
|
46 | - * and the specified request data |
|
47 | - * |
|
48 | - * @param string $batch_job_handler_class of an auto-loaded class implementing JobHandlerInterface |
|
49 | - * @param array $request_data to be used by the batch job handler |
|
50 | - * @return JobStepResponse |
|
51 | - */ |
|
52 | - public function create_job($batch_job_handler_class, $request_data) |
|
53 | - { |
|
54 | - try { |
|
55 | - $this->_job_id = wp_generate_password(15, false); |
|
56 | - $obj = $this->instantiate_batch_job_handler_from_classname($batch_job_handler_class); |
|
57 | - $this->_job_parameters = new JobParameters($this->_job_id, $batch_job_handler_class, $request_data); |
|
58 | - $response = $obj->create_job($this->_job_parameters); |
|
59 | - if (! $response instanceof JobStepResponse) { |
|
60 | - throw new BatchRequestException( |
|
61 | - sprintf( |
|
62 | - __( |
|
63 | - 'The class implementing JobHandlerInterface did not return a JobStepResponse when create_job was called with %1$s. It needs to return one or throw an Exception', |
|
64 | - 'event_espresso' |
|
65 | - ), |
|
66 | - wp_json_encode($request_data) |
|
67 | - ) |
|
68 | - ); |
|
69 | - } |
|
70 | - $success = $this->_job_parameters->save(true); |
|
71 | - if (! $success) { |
|
72 | - throw new BatchRequestException( |
|
73 | - sprintf( |
|
74 | - __( |
|
75 | - 'Could not save job %1$s to the Wordpress Options table. These were the arguments used: %2$s', |
|
76 | - 'event_espresso' |
|
77 | - ), |
|
78 | - $this->_job_id, |
|
79 | - wp_json_encode($request_data) |
|
80 | - ) |
|
81 | - ); |
|
82 | - } |
|
83 | - } catch (\Exception $e) { |
|
84 | - $response = $this->_get_error_response($e, 'create_job'); |
|
85 | - } |
|
86 | - return $response; |
|
87 | - } |
|
43 | + // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
44 | + /** |
|
45 | + * Creates a job for the specified batch handler class (which should be autoloaded) |
|
46 | + * and the specified request data |
|
47 | + * |
|
48 | + * @param string $batch_job_handler_class of an auto-loaded class implementing JobHandlerInterface |
|
49 | + * @param array $request_data to be used by the batch job handler |
|
50 | + * @return JobStepResponse |
|
51 | + */ |
|
52 | + public function create_job($batch_job_handler_class, $request_data) |
|
53 | + { |
|
54 | + try { |
|
55 | + $this->_job_id = wp_generate_password(15, false); |
|
56 | + $obj = $this->instantiate_batch_job_handler_from_classname($batch_job_handler_class); |
|
57 | + $this->_job_parameters = new JobParameters($this->_job_id, $batch_job_handler_class, $request_data); |
|
58 | + $response = $obj->create_job($this->_job_parameters); |
|
59 | + if (! $response instanceof JobStepResponse) { |
|
60 | + throw new BatchRequestException( |
|
61 | + sprintf( |
|
62 | + __( |
|
63 | + 'The class implementing JobHandlerInterface did not return a JobStepResponse when create_job was called with %1$s. It needs to return one or throw an Exception', |
|
64 | + 'event_espresso' |
|
65 | + ), |
|
66 | + wp_json_encode($request_data) |
|
67 | + ) |
|
68 | + ); |
|
69 | + } |
|
70 | + $success = $this->_job_parameters->save(true); |
|
71 | + if (! $success) { |
|
72 | + throw new BatchRequestException( |
|
73 | + sprintf( |
|
74 | + __( |
|
75 | + 'Could not save job %1$s to the Wordpress Options table. These were the arguments used: %2$s', |
|
76 | + 'event_espresso' |
|
77 | + ), |
|
78 | + $this->_job_id, |
|
79 | + wp_json_encode($request_data) |
|
80 | + ) |
|
81 | + ); |
|
82 | + } |
|
83 | + } catch (\Exception $e) { |
|
84 | + $response = $this->_get_error_response($e, 'create_job'); |
|
85 | + } |
|
86 | + return $response; |
|
87 | + } |
|
88 | 88 | |
89 | 89 | |
90 | - /** |
|
91 | - * Retrieves the job's arguments |
|
92 | - * |
|
93 | - * @param string $job_id |
|
94 | - * @param int $batch_size |
|
95 | - * @return JobStepResponse |
|
96 | - */ |
|
97 | - public function continue_job($job_id, $batch_size = 50) |
|
98 | - { |
|
99 | - try { |
|
100 | - $this->_job_id = $job_id; |
|
101 | - $batch_size = defined('EE_BATCHRUNNER_BATCH_SIZE') ? EE_BATCHRUNNER_BATCH_SIZE : $batch_size; |
|
102 | - // get the corresponding WordPress option for the job |
|
103 | - $this->_job_parameters = JobParameters::load($this->_job_id); |
|
104 | - $handler_obj = $this->instantiate_batch_job_handler_from_classname($this->_job_parameters->classname()); |
|
105 | - // continue it |
|
106 | - $response = $handler_obj->continue_job($this->_job_parameters, $batch_size); |
|
107 | - if (! $response instanceof JobStepResponse) { |
|
108 | - throw new BatchRequestException( |
|
109 | - sprintf( |
|
110 | - __( |
|
111 | - 'The class implementing JobHandlerInterface did not return a JobStepResponse when continue_job was called with job %1$s. It needs to return one or throw an Exception', |
|
112 | - 'event_espresso' |
|
113 | - ), |
|
114 | - $this->_job_id |
|
115 | - ) |
|
116 | - ); |
|
117 | - } |
|
118 | - $this->_job_parameters->save(); |
|
119 | - } catch (\Exception $e) { |
|
120 | - $response = $this->_get_error_response($e, 'continue_job'); |
|
121 | - } |
|
122 | - return $response; |
|
123 | - } |
|
90 | + /** |
|
91 | + * Retrieves the job's arguments |
|
92 | + * |
|
93 | + * @param string $job_id |
|
94 | + * @param int $batch_size |
|
95 | + * @return JobStepResponse |
|
96 | + */ |
|
97 | + public function continue_job($job_id, $batch_size = 50) |
|
98 | + { |
|
99 | + try { |
|
100 | + $this->_job_id = $job_id; |
|
101 | + $batch_size = defined('EE_BATCHRUNNER_BATCH_SIZE') ? EE_BATCHRUNNER_BATCH_SIZE : $batch_size; |
|
102 | + // get the corresponding WordPress option for the job |
|
103 | + $this->_job_parameters = JobParameters::load($this->_job_id); |
|
104 | + $handler_obj = $this->instantiate_batch_job_handler_from_classname($this->_job_parameters->classname()); |
|
105 | + // continue it |
|
106 | + $response = $handler_obj->continue_job($this->_job_parameters, $batch_size); |
|
107 | + if (! $response instanceof JobStepResponse) { |
|
108 | + throw new BatchRequestException( |
|
109 | + sprintf( |
|
110 | + __( |
|
111 | + 'The class implementing JobHandlerInterface did not return a JobStepResponse when continue_job was called with job %1$s. It needs to return one or throw an Exception', |
|
112 | + 'event_espresso' |
|
113 | + ), |
|
114 | + $this->_job_id |
|
115 | + ) |
|
116 | + ); |
|
117 | + } |
|
118 | + $this->_job_parameters->save(); |
|
119 | + } catch (\Exception $e) { |
|
120 | + $response = $this->_get_error_response($e, 'continue_job'); |
|
121 | + } |
|
122 | + return $response; |
|
123 | + } |
|
124 | 124 | |
125 | 125 | |
126 | - /** |
|
127 | - * Instantiates an object of type $classname, which implements |
|
128 | - * JobHandlerInterface |
|
129 | - * |
|
130 | - * @param string $classname |
|
131 | - * @return JobHandlerInterface |
|
132 | - * @throws BatchRequestException |
|
133 | - */ |
|
134 | - public function instantiate_batch_job_handler_from_classname($classname) |
|
135 | - { |
|
136 | - if (! class_exists($classname)) { |
|
137 | - throw new BatchRequestException( |
|
138 | - sprintf( |
|
139 | - __( |
|
140 | - 'The class %1$s does not exist, and so could not be used for running a job. It should implement JobHandlerInterface.', |
|
141 | - 'event_espresso' |
|
142 | - ), |
|
143 | - $classname |
|
144 | - ) |
|
145 | - ); |
|
146 | - } |
|
147 | - $obj = LoaderFactory::getLoader()->getNew($classname); |
|
148 | - if (! $obj instanceof JobHandlerInterface) { |
|
149 | - throw new BatchRequestException( |
|
150 | - sprintf( |
|
151 | - __( |
|
152 | - 'The class %1$s does not implement JobHandlerInterface and so could not be used for running a job', |
|
153 | - 'event_espresso' |
|
154 | - ), |
|
155 | - $classname |
|
156 | - ) |
|
157 | - ); |
|
158 | - } |
|
159 | - return $obj; |
|
160 | - } |
|
126 | + /** |
|
127 | + * Instantiates an object of type $classname, which implements |
|
128 | + * JobHandlerInterface |
|
129 | + * |
|
130 | + * @param string $classname |
|
131 | + * @return JobHandlerInterface |
|
132 | + * @throws BatchRequestException |
|
133 | + */ |
|
134 | + public function instantiate_batch_job_handler_from_classname($classname) |
|
135 | + { |
|
136 | + if (! class_exists($classname)) { |
|
137 | + throw new BatchRequestException( |
|
138 | + sprintf( |
|
139 | + __( |
|
140 | + 'The class %1$s does not exist, and so could not be used for running a job. It should implement JobHandlerInterface.', |
|
141 | + 'event_espresso' |
|
142 | + ), |
|
143 | + $classname |
|
144 | + ) |
|
145 | + ); |
|
146 | + } |
|
147 | + $obj = LoaderFactory::getLoader()->getNew($classname); |
|
148 | + if (! $obj instanceof JobHandlerInterface) { |
|
149 | + throw new BatchRequestException( |
|
150 | + sprintf( |
|
151 | + __( |
|
152 | + 'The class %1$s does not implement JobHandlerInterface and so could not be used for running a job', |
|
153 | + 'event_espresso' |
|
154 | + ), |
|
155 | + $classname |
|
156 | + ) |
|
157 | + ); |
|
158 | + } |
|
159 | + return $obj; |
|
160 | + } |
|
161 | 161 | |
162 | 162 | |
163 | - /** |
|
164 | - * Forces a job to be cleaned up |
|
165 | - * |
|
166 | - * @param string $job_id |
|
167 | - * @return JobStepResponse |
|
168 | - * @throws BatchRequestException |
|
169 | - */ |
|
170 | - public function cleanup_job($job_id) |
|
171 | - { |
|
172 | - try { |
|
173 | - $this->_job_id = $job_id; |
|
174 | - $job_parameters = JobParameters::load($this->_job_id); |
|
175 | - $handler_obj = $this->instantiate_batch_job_handler_from_classname($job_parameters->classname()); |
|
176 | - // continue it |
|
177 | - $response = $handler_obj->cleanup_job($job_parameters); |
|
178 | - if (! $response instanceof JobStepResponse) { |
|
179 | - throw new BatchRequestException( |
|
180 | - sprintf( |
|
181 | - __( |
|
182 | - 'The class implementing JobHandlerInterface did not return a JobStepResponse when cleanup_job was called with job %1$s. It needs to return one or throw an Exception', |
|
183 | - 'event_espresso' |
|
184 | - ), |
|
185 | - $this->_job_id |
|
186 | - ) |
|
187 | - ); |
|
188 | - } |
|
189 | - $job_parameters->set_status(JobParameters::status_cleaned_up); |
|
190 | - $job_parameters->delete(); |
|
191 | - return $response; |
|
192 | - } catch (\Exception $e) { |
|
193 | - $response = $this->_get_error_response($e, 'cleanup_job'); |
|
194 | - } |
|
195 | - return $response; |
|
196 | - } |
|
163 | + /** |
|
164 | + * Forces a job to be cleaned up |
|
165 | + * |
|
166 | + * @param string $job_id |
|
167 | + * @return JobStepResponse |
|
168 | + * @throws BatchRequestException |
|
169 | + */ |
|
170 | + public function cleanup_job($job_id) |
|
171 | + { |
|
172 | + try { |
|
173 | + $this->_job_id = $job_id; |
|
174 | + $job_parameters = JobParameters::load($this->_job_id); |
|
175 | + $handler_obj = $this->instantiate_batch_job_handler_from_classname($job_parameters->classname()); |
|
176 | + // continue it |
|
177 | + $response = $handler_obj->cleanup_job($job_parameters); |
|
178 | + if (! $response instanceof JobStepResponse) { |
|
179 | + throw new BatchRequestException( |
|
180 | + sprintf( |
|
181 | + __( |
|
182 | + 'The class implementing JobHandlerInterface did not return a JobStepResponse when cleanup_job was called with job %1$s. It needs to return one or throw an Exception', |
|
183 | + 'event_espresso' |
|
184 | + ), |
|
185 | + $this->_job_id |
|
186 | + ) |
|
187 | + ); |
|
188 | + } |
|
189 | + $job_parameters->set_status(JobParameters::status_cleaned_up); |
|
190 | + $job_parameters->delete(); |
|
191 | + return $response; |
|
192 | + } catch (\Exception $e) { |
|
193 | + $response = $this->_get_error_response($e, 'cleanup_job'); |
|
194 | + } |
|
195 | + return $response; |
|
196 | + } |
|
197 | 197 | |
198 | 198 | |
199 | - /** |
|
200 | - * Creates a valid JobStepResponse object from an exception and method name. |
|
201 | - * |
|
202 | - * @param \Exception $exception |
|
203 | - * @param string $method_name |
|
204 | - * @return JobStepResponse |
|
205 | - */ |
|
206 | - protected function _get_error_response(\Exception $exception, $method_name) |
|
207 | - { |
|
208 | - if (! $this->_job_parameters instanceof JobParameters) { |
|
209 | - $this->_job_parameters = new JobParameters($this->_job_id, __('__Unknown__', 'event_espresso'), array()); |
|
210 | - } |
|
211 | - $this->_job_parameters->set_status(JobParameters::status_error); |
|
212 | - return new JobStepResponse( |
|
213 | - $this->_job_parameters, |
|
214 | - sprintf( |
|
215 | - __( |
|
216 | - 'An exception of type %1$s occurred while running %2$s. Its message was %3$s and had trace %4$s', |
|
217 | - 'event_espresso' |
|
218 | - ), |
|
219 | - get_class($exception), |
|
220 | - 'BatchRunner::' . $method_name . '()', |
|
221 | - $exception->getMessage(), |
|
222 | - $exception->getTraceAsString() |
|
223 | - ) |
|
224 | - ); |
|
225 | - } |
|
199 | + /** |
|
200 | + * Creates a valid JobStepResponse object from an exception and method name. |
|
201 | + * |
|
202 | + * @param \Exception $exception |
|
203 | + * @param string $method_name |
|
204 | + * @return JobStepResponse |
|
205 | + */ |
|
206 | + protected function _get_error_response(\Exception $exception, $method_name) |
|
207 | + { |
|
208 | + if (! $this->_job_parameters instanceof JobParameters) { |
|
209 | + $this->_job_parameters = new JobParameters($this->_job_id, __('__Unknown__', 'event_espresso'), array()); |
|
210 | + } |
|
211 | + $this->_job_parameters->set_status(JobParameters::status_error); |
|
212 | + return new JobStepResponse( |
|
213 | + $this->_job_parameters, |
|
214 | + sprintf( |
|
215 | + __( |
|
216 | + 'An exception of type %1$s occurred while running %2$s. Its message was %3$s and had trace %4$s', |
|
217 | + 'event_espresso' |
|
218 | + ), |
|
219 | + get_class($exception), |
|
220 | + 'BatchRunner::' . $method_name . '()', |
|
221 | + $exception->getMessage(), |
|
222 | + $exception->getTraceAsString() |
|
223 | + ) |
|
224 | + ); |
|
225 | + } |
|
226 | 226 | } |
@@ -56,7 +56,7 @@ discard block |
||
56 | 56 | $obj = $this->instantiate_batch_job_handler_from_classname($batch_job_handler_class); |
57 | 57 | $this->_job_parameters = new JobParameters($this->_job_id, $batch_job_handler_class, $request_data); |
58 | 58 | $response = $obj->create_job($this->_job_parameters); |
59 | - if (! $response instanceof JobStepResponse) { |
|
59 | + if ( ! $response instanceof JobStepResponse) { |
|
60 | 60 | throw new BatchRequestException( |
61 | 61 | sprintf( |
62 | 62 | __( |
@@ -68,7 +68,7 @@ discard block |
||
68 | 68 | ); |
69 | 69 | } |
70 | 70 | $success = $this->_job_parameters->save(true); |
71 | - if (! $success) { |
|
71 | + if ( ! $success) { |
|
72 | 72 | throw new BatchRequestException( |
73 | 73 | sprintf( |
74 | 74 | __( |
@@ -104,7 +104,7 @@ discard block |
||
104 | 104 | $handler_obj = $this->instantiate_batch_job_handler_from_classname($this->_job_parameters->classname()); |
105 | 105 | // continue it |
106 | 106 | $response = $handler_obj->continue_job($this->_job_parameters, $batch_size); |
107 | - if (! $response instanceof JobStepResponse) { |
|
107 | + if ( ! $response instanceof JobStepResponse) { |
|
108 | 108 | throw new BatchRequestException( |
109 | 109 | sprintf( |
110 | 110 | __( |
@@ -133,7 +133,7 @@ discard block |
||
133 | 133 | */ |
134 | 134 | public function instantiate_batch_job_handler_from_classname($classname) |
135 | 135 | { |
136 | - if (! class_exists($classname)) { |
|
136 | + if ( ! class_exists($classname)) { |
|
137 | 137 | throw new BatchRequestException( |
138 | 138 | sprintf( |
139 | 139 | __( |
@@ -145,7 +145,7 @@ discard block |
||
145 | 145 | ); |
146 | 146 | } |
147 | 147 | $obj = LoaderFactory::getLoader()->getNew($classname); |
148 | - if (! $obj instanceof JobHandlerInterface) { |
|
148 | + if ( ! $obj instanceof JobHandlerInterface) { |
|
149 | 149 | throw new BatchRequestException( |
150 | 150 | sprintf( |
151 | 151 | __( |
@@ -175,7 +175,7 @@ discard block |
||
175 | 175 | $handler_obj = $this->instantiate_batch_job_handler_from_classname($job_parameters->classname()); |
176 | 176 | // continue it |
177 | 177 | $response = $handler_obj->cleanup_job($job_parameters); |
178 | - if (! $response instanceof JobStepResponse) { |
|
178 | + if ( ! $response instanceof JobStepResponse) { |
|
179 | 179 | throw new BatchRequestException( |
180 | 180 | sprintf( |
181 | 181 | __( |
@@ -205,7 +205,7 @@ discard block |
||
205 | 205 | */ |
206 | 206 | protected function _get_error_response(\Exception $exception, $method_name) |
207 | 207 | { |
208 | - if (! $this->_job_parameters instanceof JobParameters) { |
|
208 | + if ( ! $this->_job_parameters instanceof JobParameters) { |
|
209 | 209 | $this->_job_parameters = new JobParameters($this->_job_id, __('__Unknown__', 'event_espresso'), array()); |
210 | 210 | } |
211 | 211 | $this->_job_parameters->set_status(JobParameters::status_error); |
@@ -217,7 +217,7 @@ discard block |
||
217 | 217 | 'event_espresso' |
218 | 218 | ), |
219 | 219 | get_class($exception), |
220 | - 'BatchRunner::' . $method_name . '()', |
|
220 | + 'BatchRunner::'.$method_name.'()', |
|
221 | 221 | $exception->getMessage(), |
222 | 222 | $exception->getTraceAsString() |
223 | 223 | ) |