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 |
||
| 28 | class EED_Batch extends EED_Module |
||
| 29 | { |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Possibly value for $_REQUEST[ 'batch' ]. Indicates to run a job that |
||
| 33 | * processes data only |
||
| 34 | */ |
||
| 35 | const batch_job = 'job'; |
||
| 36 | /** |
||
| 37 | * Possibly value for $_REQUEST[ 'batch' ]. Indicates to run a job that |
||
| 38 | * produces a file for download |
||
| 39 | */ |
||
| 40 | const batch_file_job = 'file'; |
||
| 41 | /** |
||
| 42 | * Possibly value for $_REQUEST[ 'batch' ]. Indicates this request is NOT |
||
| 43 | * for a batch job. It's the same as not providing the $_REQUEST[ 'batch' ] |
||
| 44 | * at all |
||
| 45 | */ |
||
| 46 | const batch_not_job = 'none'; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * |
||
| 50 | * @var string 'file', or 'job', or false to indicate its not a batch request at all |
||
| 51 | */ |
||
| 52 | protected $_batch_request_type = null; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Because we want to use the response in both the localized JS and in the body |
||
| 56 | * we need to make this response available between method calls |
||
| 57 | * |
||
| 58 | * @var \EventEspressoBatchRequest\Helpers\JobStepResponse |
||
| 59 | */ |
||
| 60 | protected $_job_step_response = null; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var LoaderInterface |
||
| 64 | */ |
||
| 65 | protected $loader; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Gets the batch instance |
||
| 69 | * |
||
| 70 | * @return EED_Batch |
||
| 71 | */ |
||
| 72 | public static function instance() |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Sets hooks to enable batch jobs on the frontend. Disabled by default |
||
| 79 | * because it's an attack vector and there are currently no implementations |
||
| 80 | */ |
||
| 81 | public static function set_hooks() |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Initializes some hooks for the admin in order to run batch jobs |
||
| 93 | */ |
||
| 94 | public static function set_hooks_admin() |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @since $VID:$ |
||
| 108 | * @return LoaderInterface |
||
| 109 | * @throws InvalidArgumentException |
||
| 110 | * @throws InvalidDataTypeException |
||
| 111 | * @throws InvalidInterfaceException |
||
| 112 | */ |
||
| 113 | protected function getLoader() |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Enqueues batch scripts on the frontend or admin, and creates a job |
||
| 123 | */ |
||
| 124 | public function enqueue_scripts() |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Create a batch job, enqueues a script to run it, and localizes some data for it |
||
| 146 | */ |
||
| 147 | public function enqueue_scripts_styles_batch_create() |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Creates a batch job which will download a file, enqueues a script to run the job, and localizes some data for it |
||
| 169 | */ |
||
| 170 | public function enqueue_scripts_styles_batch_file_create() |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Enqueues scripts and styles common to any batch job, and creates |
||
| 198 | * a job from the request data, and stores the response in the |
||
| 199 | * $this->_job_step_response property |
||
| 200 | * |
||
| 201 | * @return \EventEspressoBatchRequest\Helpers\JobStepResponse |
||
| 202 | */ |
||
| 203 | protected function _enqueue_batch_job_scripts_and_styles_and_start_job() |
||
| 243 | |||
| 244 | /** |
||
| 245 | * If we are doing a frontend batch job, this makes it so WP shows our template's HTML |
||
| 246 | * |
||
| 247 | * @param string $template |
||
| 248 | * @return string |
||
| 249 | */ |
||
| 250 | public function override_template($template) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Adds an admin page which doesn't appear in the admin menu |
||
| 260 | */ |
||
| 261 | public function register_admin_pages() |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Renders the admin page, after most of the work was already done during enqueuing scripts |
||
| 275 | * of creating the job and localizing some data |
||
| 276 | */ |
||
| 277 | public function show_admin_page() |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Receives ajax calls for continuing a job |
||
| 287 | */ |
||
| 288 | View Code Duplication | public function batch_continue() |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Receives the ajax call to cleanup a job |
||
| 298 | * |
||
| 299 | * @return type |
||
| 300 | */ |
||
| 301 | View Code Duplication | public function batch_cleanup() |
|
| 308 | |||
| 309 | |||
| 310 | /** |
||
| 311 | * Returns a json response |
||
| 312 | * |
||
| 313 | * @param array $data The data we want to send echo via in the JSON response's "data" element |
||
| 314 | * |
||
| 315 | * The returned json object is created from an array in the following format: |
||
| 316 | * array( |
||
| 317 | * 'notices' => '', // - contains any EE_Error formatted notices |
||
| 318 | * 'data' => array() //this can be any key/value pairs that a method returns for later json parsing by the js. |
||
| 319 | * We're also going to include the template args with every package (so js can pick out any specific template |
||
| 320 | * args that might be included in here) |
||
| 321 | * 'isEEajax' => true,//indicates this is a response from EE |
||
| 322 | * ) |
||
| 323 | */ |
||
| 324 | protected function _return_json($data) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Gets the job step response which was done during the enqueuing of scripts |
||
| 344 | * |
||
| 345 | * @return \EventEspressoBatchRequest\Helpers\JobStepResponse |
||
| 346 | */ |
||
| 347 | public function job_step_response() |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Gets the batch request type indicated in the $_REQUEST |
||
| 354 | * |
||
| 355 | * @return string: EED_Batch::batch_job, EED_Batch::batch_file_job, EED_Batch::batch_not_job |
||
| 356 | */ |
||
| 357 | public function batch_request_type() |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Unnecessary |
||
| 377 | * |
||
| 378 | * @param type $WP |
||
| 379 | */ |
||
| 380 | public function run($WP) |
||
| 383 | } |
||
| 384 |