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 | 3 | 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 | 1 | public function process() |
|
| 113 | { |
||
| 114 | try { |
||
| 115 | // immediately add the PID to lock this import process |
||
| 116 | 1 | $this->lock(); |
|
| 117 | |||
| 118 | // load the plugin's subjects |
||
| 119 | 1 | $subjects = $this->getPluginConfiguration()->getSubjects(); |
|
| 120 | |||
| 121 | // initialize the array for the status |
||
| 122 | 1 | $status = array(); |
|
| 123 | |||
| 124 | // initialize the status information for the subjects |
||
| 125 | /** @var \TechDivision\Import\Configuration\SubjectConfigurationInterface $subject */ |
||
| 126 | 1 | foreach ($subjects as $subject) { |
|
| 127 | $status[$subject->getPrefix()] = array(); |
||
| 128 | } |
||
| 129 | |||
| 130 | // and update it in the registry |
||
| 131 | 1 | $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 | 1 | foreach ($subjects as $subject) { |
|
| 136 | $this->processSubject($subject); |
||
| 137 | } |
||
| 138 | |||
| 139 | // update the number of imported bunches |
||
| 140 | 1 | $this->getRegistryProcessor()->mergeAttributesRecursive( |
|
| 141 | 1 | $this->getSerial(), |
|
| 142 | 1 | array(RegistryKeys::BUNCHES => $this->bunches) |
|
| 143 | ); |
||
| 144 | |||
| 145 | // stop the application if we don't process ANY bunch |
||
| 146 | 1 | 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 has been 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 | 1 | $this->unlock(); |
|
| 160 | |||
|
|
|||
| 161 | } 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 | $this->unlock(); |
||
| 165 | |||
| 166 | // re-throw the exception |
||
| 167 | throw $e; |
||
| 168 | } |
||
| 169 | 1 | } |
|
| 170 | |||
| 171 | /** |
||
| 172 | * Process the subject with the passed name/identifier. |
||
| 173 | * |
||
| 174 | * We create a new, fresh and separate subject for EVERY file here, because this would be |
||
| 175 | * the starting point to parallelize the import process in a multithreaded/multiprocessed |
||
| 176 | * environment. |
||
| 177 | * |
||
| 178 | * @param \TechDivision\Import\Configuration\SubjectConfigurationInterface $subject The subject configuration |
||
| 179 | * |
||
| 180 | * @return void |
||
| 181 | * @throws \Exception Is thrown, if the subject can't be processed |
||
| 182 | */ |
||
| 183 | protected function processSubject(SubjectConfigurationInterface $subject) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Queries whether or not, the passed filename is part of a bunch or not. |
||
| 279 | * |
||
| 280 | * @param string $prefix The prefix to query for |
||
| 281 | * @param string $filename The filename to query for |
||
| 282 | * |
||
| 283 | * @return boolean TRUE if the filename is part, else FALSE |
||
| 284 | */ |
||
| 285 | 2 | protected function isPartOfBunch($prefix, $filename) |
|
| 325 | |||
| 326 | /** |
||
| 327 | * Return's an array with the names of the expected OK files for the actual subject. |
||
| 328 | * |
||
| 329 | * @return array The array with the expected OK filenames |
||
| 330 | */ |
||
| 331 | protected function getOkFilenames() |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Query whether or not, the passed CSV filename is in the OK file. If the filename was found, |
||
| 361 | * it'll be returned and the method return TRUE. |
||
| 362 | * |
||
| 363 | * If the filename is NOT in the OK file, the method return's FALSE and the CSV should NOT be |
||
| 364 | * imported/moved. |
||
| 365 | * |
||
| 366 | * @param string $filename The CSV filename to query for |
||
| 367 | * |
||
| 368 | * @return void |
||
| 369 | * @throws \Exception Is thrown, if the passed filename is NOT in the OK file or it can NOT be removed from it |
||
| 370 | */ |
||
| 371 | protected function removeFromOkFile($filename) |
||
| 424 | } |
||
| 425 |