Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 23 | class EED_Batch extends EED_Module |
||
| 24 | { |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Possibly value for $_REQUEST[ 'batch' ]. Indicates to run a job that |
||
| 28 | * processes data only |
||
| 29 | */ |
||
| 30 | const batch_job = 'job'; |
||
| 31 | /** |
||
| 32 | * Possibly value for $_REQUEST[ 'batch' ]. Indicates to run a job that |
||
| 33 | * produces a file for download |
||
| 34 | */ |
||
| 35 | const batch_file_job = 'file'; |
||
| 36 | /** |
||
| 37 | * Possibly value for $_REQUEST[ 'batch' ]. Indicates this request is NOT |
||
| 38 | * for a batch job. It's the same as not providing the $_REQUEST[ 'batch' ] |
||
| 39 | * at all |
||
| 40 | */ |
||
| 41 | const batch_not_job = 'none'; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * |
||
| 45 | * @var string 'file', or 'job', or false to indicate its not a batch request at all |
||
| 46 | */ |
||
| 47 | protected $_batch_request_type = null; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Because we want to use the response in both the localized JS and in the body |
||
| 51 | * we need to make this response available between method calls |
||
| 52 | * |
||
| 53 | * @var \EventEspressoBatchRequest\Helpers\JobStepResponse |
||
| 54 | */ |
||
| 55 | protected $_job_step_response = null; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Gets the batch instance |
||
| 59 | * |
||
| 60 | * @return EED_Batch |
||
| 61 | */ |
||
| 62 | public static function instance() |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Sets hooks to enable batch jobs on the frontend. Disabled by default |
||
| 69 | * because it's an attack vector and there are currently no implementations |
||
| 70 | */ |
||
| 71 | public static function set_hooks() |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Initializes some hooks for the admin in order to run batch jobs |
||
| 83 | */ |
||
| 84 | public static function set_hooks_admin() |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Enqueues batch scripts on the frontend or admin, and creates a job |
||
| 98 | */ |
||
| 99 | public function enqueue_scripts() |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Create a batch job, enqueues a script to run it, and localizes some data for it |
||
| 121 | */ |
||
| 122 | public function enqueue_scripts_styles_batch_create() |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Creates a batch job which will download a file, enqueues a script to run the job, and localizes some data for it |
||
| 144 | */ |
||
| 145 | public function enqueue_scripts_styles_batch_file_create() |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Enqueues scripts and styles common to any batch job, and creates |
||
| 173 | * a job from the request data, and stores the response in the |
||
| 174 | * $this->_job_step_response property |
||
| 175 | * |
||
| 176 | * @return \EventEspressoBatchRequest\Helpers\JobStepResponse |
||
| 177 | */ |
||
| 178 | protected function _enqueue_batch_job_scripts_and_styles_and_start_job() |
||
| 218 | |||
| 219 | /** |
||
| 220 | * If we are doing a frontend batch job, this makes it so WP shows our template's HTML |
||
| 221 | * |
||
| 222 | * @param string $template |
||
| 223 | * @return string |
||
| 224 | */ |
||
| 225 | public function override_template($template) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Adds an admin page which doesn't appear in the admin menu |
||
| 235 | */ |
||
| 236 | public function register_admin_pages() |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Renders the admin page, after most of the work was already done during enqueuing scripts |
||
| 250 | * of creating the job and localizing some data |
||
| 251 | */ |
||
| 252 | public function show_admin_page() |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Receives ajax calls for continuing a job |
||
| 262 | */ |
||
| 263 | View Code Duplication | public function batch_continue() |
|
| 270 | |||
| 271 | /** |
||
| 272 | * Receives the ajax call to cleanup a job |
||
| 273 | * |
||
| 274 | * @return type |
||
| 275 | */ |
||
| 276 | View Code Duplication | public function batch_cleanup() |
|
| 283 | |||
| 284 | |||
| 285 | /** |
||
| 286 | * Returns a json response |
||
| 287 | * |
||
| 288 | * @param array $data The data we want to send echo via in the JSON response's "data" element |
||
| 289 | * |
||
| 290 | * The returned json object is created from an array in the following format: |
||
| 291 | * array( |
||
| 292 | * 'notices' => '', // - contains any EE_Error formatted notices |
||
| 293 | * 'data' => array() //this can be any key/value pairs that a method returns for later json parsing by the js. |
||
| 294 | * We're also going to include the template args with every package (so js can pick out any specific template |
||
| 295 | * args that might be included in here) |
||
| 296 | * 'isEEajax' => true,//indicates this is a response from EE |
||
| 297 | * ) |
||
| 298 | */ |
||
| 299 | protected function _return_json($data) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Gets the job step response which was done during the enqueuing of scripts |
||
| 319 | * |
||
| 320 | * @return \EventEspressoBatchRequest\Helpers\JobStepResponse |
||
| 321 | */ |
||
| 322 | public function job_step_response() |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Gets the batch request type indicated in the $_REQUEST |
||
| 329 | * |
||
| 330 | * @return string: EED_Batch::batch_job, EED_Batch::batch_file_job, EED_Batch::batch_not_job |
||
| 331 | */ |
||
| 332 | public function batch_request_type() |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Unnecessary |
||
| 352 | * |
||
| 353 | * @param type $WP |
||
| 354 | */ |
||
| 355 | public function run($WP) |
||
| 358 | } |
||
| 359 |