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 |
||
| 37 | class SubjectPlugin extends AbstractPlugin |
||
| 38 | { |
||
| 39 | |||
| 40 | /** |
||
| 41 | * The matches for the last processed CSV filename. |
||
| 42 | * |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | protected $matches = array(); |
||
| 46 | |||
| 47 | /** |
||
| 48 | * The number of imported bunches. |
||
| 49 | * |
||
| 50 | * @var integer |
||
| 51 | */ |
||
| 52 | protected $bunches = 0; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Process the plugin functionality. |
||
| 56 | * |
||
| 57 | * @return void |
||
| 58 | * @throws \Exception Is thrown, if the plugin can not be processed |
||
| 59 | */ |
||
| 60 | public function process() |
||
| 61 | { |
||
| 62 | try { |
||
| 63 | // immediately add the PID to lock this import process |
||
| 64 | $this->lock(); |
||
| 65 | |||
| 66 | // load system logger and registry |
||
| 67 | $importProcessor = $this->getImportProcessor(); |
||
| 68 | |||
| 69 | // start the transaction |
||
| 70 | $importProcessor->getConnection()->beginTransaction(); |
||
| 71 | |||
| 72 | // load the plugin's subjects |
||
| 73 | $subjects = $this->getPluginConfiguration()->getSubjects(); |
||
| 74 | |||
| 75 | // initialize the array for the status |
||
| 76 | $status = array(); |
||
| 77 | |||
| 78 | // initialize the status information for the subjects |
||
| 79 | /** @var \TechDivision\Import\Configuration\SubjectConfigurationInterface $subject */ |
||
| 80 | foreach ($subjects as $subject) { |
||
| 81 | $status[$subject->getPrefix()] = array(); |
||
| 82 | } |
||
| 83 | |||
| 84 | // and update it in the registry |
||
| 85 | $this->getRegistryProcessor()->mergeAttributesRecursive($this->getSerial(), $status); |
||
| 86 | |||
| 87 | // process all the subjects found in the system configuration |
||
| 88 | /** @var \TechDivision\Import\Configuration\SubjectConfigurationInterface $subject */ |
||
| 89 | foreach ($subjects as $subject) { |
||
| 90 | $this->processSubject($subject); |
||
| 91 | } |
||
| 92 | |||
| 93 | // update the number of imported bunches |
||
| 94 | $this->getRegistryProcessor()->mergeAttributesRecursive( |
||
| 95 | $this->getSerial(), |
||
| 96 | array(RegistryKeys::BUNCHES => $this->bunches) |
||
| 97 | ); |
||
| 98 | |||
| 99 | // finally, if a PID has been set (because CSV files has been found), |
||
| 100 | // remove it from the PID file to unlock the importer |
||
| 101 | $this->unlock(); |
||
| 102 | |||
| 103 | // commit the transaction |
||
| 104 | $importProcessor->getConnection()->commit(); |
||
| 105 | |||
|
|
|||
| 106 | } catch (\Exception $e) { |
||
| 107 | // finally, if a PID has been set (because CSV files has been found), |
||
| 108 | // remove it from the PID file to unlock the importer |
||
| 109 | $this->unlock(); |
||
| 110 | |||
| 111 | // rollback the transaction |
||
| 112 | $importProcessor->getConnection()->rollBack(); |
||
| 113 | |||
| 114 | // re-throw the exception |
||
| 115 | throw $e; |
||
| 116 | } |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Process the subject with the passed name/identifier. |
||
| 121 | * |
||
| 122 | * We create a new, fresh and separate subject for EVERY file here, because this would be |
||
| 123 | * the starting point to parallelize the import process in a multithreaded/multiprocessed |
||
| 124 | * environment. |
||
| 125 | * |
||
| 126 | * @param \TechDivision\Import\Configuration\SubjectConfigurationInterface $subject The subject configuration |
||
| 127 | * |
||
| 128 | * @return void |
||
| 129 | * @throws \Exception Is thrown, if the subject can't be processed |
||
| 130 | */ |
||
| 131 | protected function processSubject(SubjectConfigurationInterface $subject) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Factory method to create new handler instances. |
||
| 202 | * |
||
| 203 | * @param \TechDivision\Import\Configuration\SubjectConfigurationInterface $subjectConfiguration The subject configuration |
||
| 204 | * |
||
| 205 | * @return object The handler instance |
||
| 206 | */ |
||
| 207 | protected function subjectFactory(SubjectConfigurationInterface $subjectConfiguration) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Queries whether or not, the passed filename is part of a bunch or not. |
||
| 237 | * |
||
| 238 | * @param string $prefix The prefix to query for |
||
| 239 | * @param string $filename The filename to query for |
||
| 240 | * |
||
| 241 | * @return boolean TRUE if the filename is part, else FALSE |
||
| 242 | */ |
||
| 243 | 2 | protected function isPartOfBunch($prefix, $filename) |
|
| 283 | |||
| 284 | /** |
||
| 285 | * Return's an array with the names of the expected OK files for the actual subject. |
||
| 286 | * |
||
| 287 | * @return array The array with the expected OK filenames |
||
| 288 | */ |
||
| 289 | protected function getOkFilenames() |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Query whether or not, the passed CSV filename is in the OK file. If the filename was found, |
||
| 319 | * it'll be returned and the method return TRUE. |
||
| 320 | * |
||
| 321 | * If the filename is NOT in the OK file, the method return's FALSE and the CSV should NOT be |
||
| 322 | * imported/moved. |
||
| 323 | * |
||
| 324 | * @param string $filename The CSV filename to query for |
||
| 325 | * |
||
| 326 | * @return void |
||
| 327 | * @throws \Exception Is thrown, if the passed filename is NOT in the OK file or it can NOT be removed from it |
||
| 328 | */ |
||
| 329 | protected function removeFromOkFile($filename) |
||
| 372 | } |
||
| 373 |