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 | * Initializes the plugin with the application instance. |
||
| 76 | * |
||
| 77 | * @param \TechDivision\Import\ApplicationInterface $application The application instance |
||
| 78 | * @param \TechDivision\Import\Configuration\PluginConfigurationInterface $pluginConfiguration The plugin configuration instance |
||
| 79 | * @param \TechDivision\Import\Callbacks\CallbackVisitor $callbackVisitor The callback visitor instance |
||
| 80 | * @param \TechDivision\Import\Observers\ObserverVisitor $observerVisitor The observer visitor instance |
||
| 81 | */ |
||
| 82 | 2 | public function __construct( |
|
| 96 | |||
| 97 | |||
| 98 | /** |
||
| 99 | * Process the plugin functionality. |
||
| 100 | * |
||
| 101 | * @return void |
||
| 102 | * @throws \Exception Is thrown, if the plugin can not be processed |
||
| 103 | */ |
||
| 104 | public function process() |
||
| 105 | { |
||
| 106 | try { |
||
| 107 | // immediately add the PID to lock this import process |
||
| 108 | $this->lock(); |
||
| 109 | |||
| 110 | // load the plugin's subjects |
||
| 111 | $subjects = $this->getPluginConfiguration()->getSubjects(); |
||
| 112 | |||
| 113 | // initialize the array for the status |
||
| 114 | $status = array(); |
||
| 115 | |||
| 116 | // initialize the status information for the subjects |
||
| 117 | /** @var \TechDivision\Import\Configuration\SubjectConfigurationInterface $subject */ |
||
| 118 | foreach ($subjects as $subject) { |
||
| 119 | $status[$subject->getPrefix()] = array(); |
||
| 120 | } |
||
| 121 | |||
| 122 | // and update it in the registry |
||
| 123 | $this->getRegistryProcessor()->mergeAttributesRecursive($this->getSerial(), $status); |
||
| 124 | |||
| 125 | // process all the subjects found in the system configuration |
||
| 126 | /** @var \TechDivision\Import\Configuration\SubjectConfigurationInterface $subject */ |
||
| 127 | foreach ($subjects as $subject) { |
||
| 128 | $this->processSubject($subject); |
||
| 129 | } |
||
| 130 | |||
| 131 | // update the number of imported bunches |
||
| 132 | $this->getRegistryProcessor()->mergeAttributesRecursive( |
||
| 133 | $this->getSerial(), |
||
| 134 | array(RegistryKeys::BUNCHES => $this->bunches) |
||
| 135 | ); |
||
| 136 | |||
| 137 | // stop the application if we don't process ANY bunch |
||
| 138 | if ($this->bunches === 0) { |
||
| 139 | $this->getApplication()->stop( |
||
| 140 | sprintf( |
||
| 141 | 'Operation %s has been stopped by %s, because no import files has been found in directory %s', |
||
| 142 | $this->getConfiguration()->getOperationName(), |
||
| 143 | get_class($this), |
||
| 144 | $this->getConfiguration()->getSourceDir() |
||
| 145 | ) |
||
| 146 | ); |
||
| 147 | } |
||
| 148 | |||
| 149 | // finally, if a PID has been set (because CSV files has been found), |
||
| 150 | // remove it from the PID file to unlock the importer |
||
| 151 | $this->unlock(); |
||
| 152 | |||
|
|
|||
| 153 | } catch (\Exception $e) { |
||
| 154 | // finally, if a PID has been set (because CSV files has been found), |
||
| 155 | // remove it from the PID file to unlock the importer |
||
| 156 | $this->unlock(); |
||
| 157 | |||
| 158 | // re-throw the exception |
||
| 159 | throw $e; |
||
| 160 | } |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Process the subject with the passed name/identifier. |
||
| 165 | * |
||
| 166 | * We create a new, fresh and separate subject for EVERY file here, because this would be |
||
| 167 | * the starting point to parallelize the import process in a multithreaded/multiprocessed |
||
| 168 | * environment. |
||
| 169 | * |
||
| 170 | * @param \TechDivision\Import\Configuration\SubjectConfigurationInterface $subject The subject configuration |
||
| 171 | * |
||
| 172 | * @return void |
||
| 173 | * @throws \Exception Is thrown, if the subject can't be processed |
||
| 174 | */ |
||
| 175 | protected function processSubject(SubjectConfigurationInterface $subject) |
||
| 176 | { |
||
| 177 | |||
| 178 | // clear the filecache |
||
| 179 | clearstatcache(); |
||
| 180 | |||
| 181 | // load the actual status |
||
| 182 | $status = $this->getRegistryProcessor()->getAttribute($serial = $this->getSerial()); |
||
| 183 | |||
| 184 | // query whether or not the configured source directory is available |
||
| 185 | View Code Duplication | if (!is_dir($sourceDir = $status[RegistryKeys::SOURCE_DIRECTORY])) { |
|
| 186 | throw new \Exception(sprintf('Source directory %s for subject %s is not available!', $sourceDir, $subject->getId())); |
||
| 187 | } |
||
| 188 | |||
| 189 | // initialize the array with the CSV files found in the source directory |
||
| 190 | $files = glob(sprintf('%s/*.csv', $sourceDir)); |
||
| 191 | |||
| 192 | // sorting the files for the apropriate order |
||
| 193 | usort($files, function ($a, $b) { |
||
| 194 | return strcmp($a, $b); |
||
| 195 | }); |
||
| 196 | |||
| 197 | // log a debug message |
||
| 198 | $this->getSystemLogger()->debug(sprintf('Now checking directory %s for files to be imported', $sourceDir)); |
||
| 199 | |||
| 200 | // initialize the bunch number |
||
| 201 | $bunches = 0; |
||
| 202 | |||
| 203 | // iterate through all CSV files and process the subjects |
||
| 204 | foreach ($files as $pathname) { |
||
| 205 | // query whether or not that the file is part of the actual bunch |
||
| 206 | if ($this->isPartOfBunch($subject->getPrefix(), $pathname)) { |
||
| 207 | try { |
||
| 208 | // initialize the subject and import the bunch |
||
| 209 | $subjectInstance = $this->subjectFactory($subject); |
||
| 210 | |||
| 211 | // setup the subject instance |
||
| 212 | $subjectInstance->setUp($serial); |
||
| 213 | |||
| 214 | // initialize the callbacks/observers |
||
| 215 | $this->callbackVisitor->visit($subjectInstance); |
||
| 216 | $this->observerVisitor->visit($subjectInstance); |
||
| 217 | |||
| 218 | // query whether or not the subject needs an OK file, |
||
| 219 | // if yes remove the filename from the file |
||
| 220 | if ($subjectInstance->isOkFileNeeded()) { |
||
| 221 | $this->removeFromOkFile($pathname); |
||
| 222 | } |
||
| 223 | |||
| 224 | // finally import the CSV file |
||
| 225 | $subjectInstance->import($serial, $pathname); |
||
| 226 | |||
| 227 | // query whether or not, we've to export artefacts |
||
| 228 | if ($subjectInstance instanceof ExportableSubjectInterface) { |
||
| 229 | $subjectInstance->export( |
||
| 230 | $this->matches[BunchKeys::FILENAME], |
||
| 231 | $this->matches[BunchKeys::COUNTER] |
||
| 232 | ); |
||
| 233 | } |
||
| 234 | |||
| 235 | // raise the number of the imported bunches |
||
| 236 | $bunches++; |
||
| 237 | |||
| 238 | // tear down the subject instance |
||
| 239 | $subjectInstance->tearDown($serial); |
||
| 240 | |||
| 241 | } catch (\Exception $e) { |
||
| 242 | // query whether or not, we've to export artefacts |
||
| 243 | if ($subjectInstance instanceof ExportableSubjectInterface) { |
||
| 244 | // tear down the subject instance |
||
| 245 | $subjectInstance->tearDown($serial); |
||
| 246 | } |
||
| 247 | |||
| 248 | // re-throw the exception |
||
| 249 | throw $e; |
||
| 250 | } |
||
| 251 | } |
||
| 252 | } |
||
| 253 | |||
| 254 | // raise the bunch number by the imported bunches |
||
| 255 | $this->bunches = $this->bunches + $bunches; |
||
| 256 | |||
| 257 | // reset the matches, because the exported artefacts |
||
| 258 | $this->matches = array(); |
||
| 259 | |||
| 260 | // and and log a message that the subject has been processed |
||
| 261 | $this->getSystemLogger()->debug( |
||
| 262 | sprintf('Successfully processed subject %s with %d bunch(es)!', $subject->getId(), $bunches) |
||
| 263 | ); |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Factory method to create new handler instances. |
||
| 268 | * |
||
| 269 | * @param \TechDivision\Import\Configuration\SubjectConfigurationInterface $subjectConfiguration The subject configuration |
||
| 270 | * |
||
| 271 | * @return object The handler instance |
||
| 272 | */ |
||
| 273 | protected function subjectFactory(SubjectConfigurationInterface $subjectConfiguration) |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Queries whether or not, the passed filename is part of a bunch or not. |
||
| 281 | * |
||
| 282 | * @param string $prefix The prefix to query for |
||
| 283 | * @param string $filename The filename to query for |
||
| 284 | * |
||
| 285 | * @return boolean TRUE if the filename is part, else FALSE |
||
| 286 | */ |
||
| 287 | 2 | protected function isPartOfBunch($prefix, $filename) |
|
| 327 | |||
| 328 | /** |
||
| 329 | * Return's an array with the names of the expected OK files for the actual subject. |
||
| 330 | * |
||
| 331 | * @return array The array with the expected OK filenames |
||
| 332 | */ |
||
| 333 | protected function getOkFilenames() |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Query whether or not, the passed CSV filename is in the OK file. If the filename was found, |
||
| 363 | * it'll be returned and the method return TRUE. |
||
| 364 | * |
||
| 365 | * If the filename is NOT in the OK file, the method return's FALSE and the CSV should NOT be |
||
| 366 | * imported/moved. |
||
| 367 | * |
||
| 368 | * @param string $filename The CSV filename to query for |
||
| 369 | * |
||
| 370 | * @return void |
||
| 371 | * @throws \Exception Is thrown, if the passed filename is NOT in the OK file or it can NOT be removed from it |
||
| 372 | */ |
||
| 373 | protected function removeFromOkFile($filename) |
||
| 426 | } |
||
| 427 |