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 |
||
| 43 | class SubjectPlugin extends AbstractPlugin |
||
| 44 | { |
||
| 45 | |||
| 46 | /** |
||
| 47 | * The matches for the last processed CSV filename. |
||
| 48 | * |
||
| 49 | * @var array |
||
| 50 | */ |
||
| 51 | protected $matches = array(); |
||
| 52 | |||
| 53 | /** |
||
| 54 | * The number of imported bunches. |
||
| 55 | * |
||
| 56 | * @var integer |
||
| 57 | */ |
||
| 58 | protected $bunches = 0; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * The callback visitor instance. |
||
| 62 | * |
||
| 63 | * @var \TechDivision\Import\Callbacks\CallbackVisitor |
||
| 64 | */ |
||
| 65 | protected $callbackVisitor; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * The observer visitor instance. |
||
| 69 | * |
||
| 70 | * @var \TechDivision\Import\Observers\ObserverVisitor |
||
| 71 | */ |
||
| 72 | protected $observerVisitor; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * The subject factory instance. |
||
| 76 | * |
||
| 77 | * @var \TechDivision\Import\Subjects\SubjectFactoryInterface |
||
| 78 | */ |
||
| 79 | protected $subjectFactory; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Initializes the plugin with the application instance. |
||
| 83 | * |
||
| 84 | * @param \TechDivision\Import\ApplicationInterface $application The application instance |
||
| 85 | * @param \TechDivision\Import\Callbacks\CallbackVisitor $callbackVisitor The callback visitor instance |
||
| 86 | * @param \TechDivision\Import\Observers\ObserverVisitor $observerVisitor The observer visitor instance |
||
| 87 | * @param \TechDivision\Import\Subjects\SubjectFactoryInterface $subjectFactory The subject factory instance |
||
| 88 | */ |
||
| 89 | 6 | public function __construct( |
|
| 104 | |||
| 105 | |||
| 106 | /** |
||
| 107 | * Process the plugin functionality. |
||
| 108 | * |
||
| 109 | * @return void |
||
| 110 | * @throws \Exception Is thrown, if the plugin can not be processed |
||
| 111 | */ |
||
| 112 | 4 | public function process() |
|
| 113 | { |
||
| 114 | try { |
||
| 115 | // immediately add the PID to lock this import process |
||
| 116 | 4 | $this->lock(); |
|
| 117 | |||
| 118 | // load the plugin's subjects |
||
| 119 | 4 | $subjects = $this->getPluginConfiguration()->getSubjects(); |
|
| 120 | |||
| 121 | // initialize the array for the status |
||
| 122 | 4 | $status = array(); |
|
| 123 | |||
| 124 | // initialize the status information for the subjects |
||
| 125 | /** @var \TechDivision\Import\Configuration\SubjectConfigurationInterface $subject */ |
||
| 126 | 4 | foreach ($subjects as $subject) { |
|
| 127 | 3 | $status[$subject->getPrefix()] = array(); |
|
| 128 | } |
||
| 129 | |||
| 130 | // and update it in the registry |
||
| 131 | 4 | $this->getRegistryProcessor()->mergeAttributesRecursive($this->getSerial(), $status); |
|
| 132 | |||
| 133 | // process all the subjects found in the system configuration |
||
| 134 | /** @var \TechDivision\Import\Configuration\SubjectConfigurationInterface $subject */ |
||
| 135 | 4 | foreach ($subjects as $subject) { |
|
| 136 | 3 | $this->processSubject($subject); |
|
| 137 | } |
||
| 138 | |||
| 139 | // update the number of imported bunches |
||
| 140 | 2 | $this->getRegistryProcessor()->mergeAttributesRecursive( |
|
| 141 | 2 | $this->getSerial(), |
|
| 142 | 2 | array(RegistryKeys::BUNCHES => $this->bunches) |
|
| 143 | ); |
||
| 144 | |||
| 145 | // stop the application if we don't process ANY bunch |
||
| 146 | 2 | if ($this->bunches === 0) { |
|
| 147 | 1 | $this->getApplication()->stop( |
|
| 148 | 1 | sprintf( |
|
| 149 | 1 | 'Operation %s has been stopped by %s, because no import files can be found in directory %s', |
|
| 150 | 1 | $this->getConfiguration()->getOperationName(), |
|
| 151 | 1 | get_class($this), |
|
| 152 | 1 | $this->getConfiguration()->getSourceDir() |
|
| 153 | ) |
||
| 154 | ); |
||
| 155 | } |
||
| 156 | |||
| 157 | // finally, if a PID has been set (because CSV files has been found), |
||
| 158 | // remove it from the PID file to unlock the importer |
||
| 159 | 2 | $this->unlock(); |
|
| 160 | |||
|
|
|||
| 161 | 2 | } catch (\Exception $e) { |
|
| 162 | // finally, if a PID has been set (because CSV files has been found), |
||
| 163 | // remove it from the PID file to unlock the importer |
||
| 164 | 2 | $this->unlock(); |
|
| 165 | |||
| 166 | // re-throw the exception |
||
| 167 | 2 | throw $e; |
|
| 168 | } |
||
| 169 | 2 | } |
|
| 170 | |||
| 171 | /** |
||
| 172 | * Loads the files from the source directory and return's them sorted. |
||
| 173 | * |
||
| 174 | * @param \TechDivision\Import\Configuration\SubjectConfigurationInterface $subject The source directory to parse for files |
||
| 175 | * |
||
| 176 | * @return array The array with the files matching the subjects suffix |
||
| 177 | * @throws \Exception Is thrown, when the source directory is NOT available |
||
| 178 | */ |
||
| 179 | 3 | protected function loadFiles(SubjectConfigurationInterface $subject) |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Process the subject with the passed name/identifier. |
||
| 207 | * |
||
| 208 | * We create a new, fresh and separate subject for EVERY file here, because this would be |
||
| 209 | * the starting point to parallelize the import process in a multithreaded/multiprocessed |
||
| 210 | * environment. |
||
| 211 | * |
||
| 212 | * @param \TechDivision\Import\Configuration\SubjectConfigurationInterface $subject The subject configuration |
||
| 213 | * |
||
| 214 | * @return void |
||
| 215 | * @throws \Exception Is thrown, if the subject can't be processed |
||
| 216 | */ |
||
| 217 | 3 | protected function processSubject(SubjectConfigurationInterface $subject) |
|
| 289 | |||
| 290 | /** |
||
| 291 | * Queries whether or not, the passed filename is part of a bunch or not. |
||
| 292 | * |
||
| 293 | * @param string $prefix The prefix to query for |
||
| 294 | * @param string $suffix The suffix to query for |
||
| 295 | * @param string $filename The filename to query for |
||
| 296 | * |
||
| 297 | * @return boolean TRUE if the filename is part, else FALSE |
||
| 298 | */ |
||
| 299 | 4 | protected function isPartOfBunch($prefix, $suffix, $filename) |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Return's an array with the names of the expected OK files for the actual subject. |
||
| 344 | * |
||
| 345 | * @return array The array with the expected OK filenames |
||
| 346 | */ |
||
| 347 | 2 | protected function getOkFilenames() |
|
| 374 | |||
| 375 | /** |
||
| 376 | * Query whether or not, the passed CSV filename is in the OK file. If the filename was found, |
||
| 377 | * it'll be returned and the method return TRUE. |
||
| 378 | * |
||
| 379 | * If the filename is NOT in the OK file, the method return's FALSE and the CSV should NOT be |
||
| 380 | * imported/moved. |
||
| 381 | * |
||
| 382 | * @param string $filename The CSV filename to query for |
||
| 383 | * @param string $suffix The CSF filename suffix, csv by default |
||
| 384 | * |
||
| 385 | * @return void |
||
| 386 | * @throws \Exception Is thrown, if the passed filename is NOT in the OK file or it can NOT be removed from it |
||
| 387 | */ |
||
| 388 | 2 | protected function removeFromOkFile($filename, $suffix) |
|
| 441 | } |
||
| 442 |