| Conditions | 10 |
| Paths | 20 |
| Total Lines | 322 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 73 | public function run() |
||
| 74 | { |
||
| 75 | $total = $errors = 0; |
||
| 76 | $context = $this->getContext(); |
||
| 77 | $config = $context->getConfig(); |
||
| 78 | $logger = $context->getLogger(); |
||
| 79 | $domains = array( 'attribute', 'media', 'price', 'product', 'product/property', 'text' ); |
||
| 80 | $mappings = $this->getDefaultMapping(); |
||
| 81 | |||
| 82 | |||
| 83 | /** controller/common/product/import/csv/domains |
||
| 84 | * List of item domain names that should be retrieved along with the product items |
||
| 85 | * |
||
| 86 | * For efficient processing, the items associated to the products can be |
||
| 87 | * fetched to, minimizing the number of database queries required. To be |
||
| 88 | * most effective, the list of item domain names should be used in the |
||
| 89 | * mapping configuration too, so the retrieved items will be used during |
||
| 90 | * the import. |
||
| 91 | * |
||
| 92 | * @param array Associative list of MShop item domain names |
||
| 93 | * @since 2015.05 |
||
| 94 | * @category Developer |
||
| 95 | * @see controller/common/product/import/csv/mapping |
||
| 96 | * @see controller/common/product/import/csv/converter |
||
| 97 | * @see controller/common/product/import/csv/max-size |
||
| 98 | */ |
||
| 99 | $domains = $config->get( 'controller/common/product/import/csv/domains', $domains ); |
||
| 100 | |||
| 101 | /** controller/jobs/product/import/csv/domains |
||
| 102 | * List of item domain names that should be retrieved along with the product items |
||
| 103 | * |
||
| 104 | * This configuration setting overwrites the shared option |
||
| 105 | * "controller/common/product/import/csv/domains" if you need a |
||
| 106 | * specific setting for the job controller. Otherwise, you should |
||
| 107 | * use the shared option for consistency. |
||
| 108 | * |
||
| 109 | * @param array Associative list of MShop item domain names |
||
| 110 | * @since 2015.05 |
||
| 111 | * @category Developer |
||
| 112 | * @see controller/jobs/product/import/csv/mapping |
||
| 113 | * @see controller/jobs/product/import/csv/skip-lines |
||
| 114 | * @see controller/jobs/product/import/csv/converter |
||
| 115 | * @see controller/jobs/product/import/csv/strict |
||
| 116 | * @see controller/jobs/product/import/csv/backup |
||
| 117 | * @see controller/common/product/import/csv/max-size |
||
| 118 | */ |
||
| 119 | $domains = $config->get( 'controller/jobs/product/import/csv/domains', $domains ); |
||
| 120 | |||
| 121 | |||
| 122 | /** controller/common/product/import/csv/mapping |
||
| 123 | * List of mappings between the position in the CSV file and item keys |
||
| 124 | * |
||
| 125 | * The importer have to know which data is at which position in the CSV |
||
| 126 | * file. Therefore, you need to specify a mapping between each position |
||
| 127 | * and the MShop domain item key (e.g. "product.code") it represents. |
||
| 128 | * |
||
| 129 | * You can use all domain item keys which are used in the fromArray() |
||
| 130 | * methods of the item classes. |
||
| 131 | * |
||
| 132 | * These mappings are grouped together by their processor names, which |
||
| 133 | * are responsible for importing the data, e.g. all mappings in "item" |
||
| 134 | * will be processed by the base product importer while the mappings in |
||
| 135 | * "text" will be imported by the text processor. |
||
| 136 | * |
||
| 137 | * @param array Associative list of processor names and lists of key/position pairs |
||
| 138 | * @since 2015.05 |
||
| 139 | * @category Developer |
||
| 140 | * @see controller/common/product/import/csv/domains |
||
| 141 | * @see controller/common/product/import/csv/converter |
||
| 142 | * @see controller/common/product/import/csv/max-size |
||
| 143 | */ |
||
| 144 | $mappings = $config->get( 'controller/common/product/import/csv/mapping', $mappings ); |
||
| 145 | |||
| 146 | /** controller/jobs/product/import/csv/mapping |
||
| 147 | * List of mappings between the position in the CSV file and item keys |
||
| 148 | * |
||
| 149 | * This configuration setting overwrites the shared option |
||
| 150 | * "controller/common/product/import/csv/mapping" if you need a |
||
| 151 | * specific setting for the job controller. Otherwise, you should |
||
| 152 | * use the shared option for consistency. |
||
| 153 | * |
||
| 154 | * @param array Associative list of processor names and lists of key/position pairs |
||
| 155 | * @since 2015.05 |
||
| 156 | * @category Developer |
||
| 157 | * @see controller/jobs/product/import/csv/domains |
||
| 158 | * @see controller/jobs/product/import/csv/skip-lines |
||
| 159 | * @see controller/jobs/product/import/csv/converter |
||
| 160 | * @see controller/jobs/product/import/csv/strict |
||
| 161 | * @see controller/jobs/product/import/csv/backup |
||
| 162 | * @see controller/common/product/import/csv/max-size |
||
| 163 | */ |
||
| 164 | $mappings = $config->get( 'controller/jobs/product/import/csv/mapping', $mappings ); |
||
| 165 | |||
| 166 | |||
| 167 | /** controller/common/product/import/csv/converter |
||
| 168 | * List of converter names for the values at the position in the CSV file |
||
| 169 | * |
||
| 170 | * Not all data in the CSV file is already in the required format. Maybe |
||
| 171 | * the text encoding isn't UTF-8, the date is not in ISO format or something |
||
| 172 | * similar. In order to convert the data before it's imported, you can |
||
| 173 | * specify a list of converter objects that should be applied to the data |
||
| 174 | * from the CSV file. |
||
| 175 | * |
||
| 176 | * To each field in the CSV file, you can apply one or more converters, |
||
| 177 | * e.g. to encode a Latin text to UTF8 for the second CSV field: |
||
| 178 | * |
||
| 179 | * array( 1 => 'Text/LatinUTF8' ) |
||
| 180 | * |
||
| 181 | * Similarly, you can also apply several converters at once to the same |
||
| 182 | * field: |
||
| 183 | * |
||
| 184 | * array( 1 => array( 'Text/LatinUTF8', 'DateTime/EnglishISO' ) ) |
||
| 185 | * |
||
| 186 | * It would convert the data of the second CSV field first to UTF-8 and |
||
| 187 | * afterwards try to translate it to an ISO date format. |
||
| 188 | * |
||
| 189 | * The available converter objects are named "\Aimeos\MW\Convert\<type>_<conversion>" |
||
| 190 | * where <type> is the data type and <conversion> the way of the conversion. |
||
| 191 | * In the configuration, the type and conversion must be separated by a |
||
| 192 | * slash (<type>/<conversion>). |
||
| 193 | * |
||
| 194 | * '''Note:''' Keep in mind that the position of the CSV fields start at |
||
| 195 | * zero (0). If you only need to convert a few fields, you don't have to |
||
| 196 | * configure all fields. Only specify the positions in the array you |
||
| 197 | * really need! |
||
| 198 | * |
||
| 199 | * @param array Associative list of position/converter name (or list of names) pairs |
||
| 200 | * @since 2015.05 |
||
| 201 | * @category Developer |
||
| 202 | * @see controller/common/product/import/csv/domains |
||
| 203 | * @see controller/common/product/import/csv/mapping |
||
| 204 | * @see controller/common/product/import/csv/max-size |
||
| 205 | */ |
||
| 206 | $converters = $config->get( 'controller/common/product/import/csv/converter', [] ); |
||
| 207 | |||
| 208 | /** controller/jobs/product/import/csv/converter |
||
| 209 | * List of converter names for the values at the position in the CSV file |
||
| 210 | * |
||
| 211 | * This configuration setting overwrites the shared option |
||
| 212 | * "controller/common/product/import/csv/converter" if you need a |
||
| 213 | * specific setting for the job controller. Otherwise, you should |
||
| 214 | * use the shared option for consistency. |
||
| 215 | * |
||
| 216 | * @param array Associative list of position/converter name (or list of names) pairs |
||
| 217 | * @since 2015.05 |
||
| 218 | * @category Developer |
||
| 219 | * @see controller/jobs/product/import/csv/domains |
||
| 220 | * @see controller/jobs/product/import/csv/mapping |
||
| 221 | * @see controller/jobs/product/import/csv/skip-lines |
||
| 222 | * @see controller/jobs/product/import/csv/strict |
||
| 223 | * @see controller/jobs/product/import/csv/backup |
||
| 224 | * @see controller/common/product/import/csv/max-size |
||
| 225 | */ |
||
| 226 | $converters = $config->get( 'controller/jobs/product/import/csv/converter', $converters ); |
||
| 227 | |||
| 228 | |||
| 229 | /** controller/common/product/import/csv/max-size |
||
| 230 | * Maximum number of CSV rows to import at once |
||
| 231 | * |
||
| 232 | * It's more efficient to read and import more than one row at a time |
||
| 233 | * to speed up the import. Usually, the bigger the chunk that is imported |
||
| 234 | * at once, the less time the importer will need. The downside is that |
||
| 235 | * the amount of memory required by the import process will increase as |
||
| 236 | * well. Therefore, it's a trade-off between memory consumption and |
||
| 237 | * import speed. |
||
| 238 | * |
||
| 239 | * @param integer Number of rows |
||
| 240 | * @since 2015.05 |
||
| 241 | * @category Developer |
||
| 242 | * @see controller/common/product/import/csv/domains |
||
| 243 | * @see controller/common/product/import/csv/mapping |
||
| 244 | * @see controller/common/product/import/csv/converter |
||
| 245 | */ |
||
| 246 | $maxcnt = (int) $config->get( 'controller/common/product/import/csv/max-size', 1000 ); |
||
| 247 | |||
| 248 | |||
| 249 | /** controller/jobs/product/import/csv/skip-lines |
||
| 250 | * Number of rows skipped in front of each CSV files |
||
| 251 | * |
||
| 252 | * Some CSV files contain header information describing the content of |
||
| 253 | * the column values. These data is for informational purpose only and |
||
| 254 | * can't be imported into the database. Using this option, you can |
||
| 255 | * define the number of lines that should be left out before the import |
||
| 256 | * begins. |
||
| 257 | * |
||
| 258 | * @param integer Number of rows |
||
| 259 | * @since 2015.08 |
||
| 260 | * @category Developer |
||
| 261 | * @see controller/jobs/product/import/csv/domains |
||
| 262 | * @see controller/jobs/product/import/csv/mapping |
||
| 263 | * @see controller/jobs/product/import/csv/converter |
||
| 264 | * @see controller/jobs/product/import/csv/strict |
||
| 265 | * @see controller/jobs/product/import/csv/backup |
||
| 266 | * @see controller/common/product/import/csv/max-size |
||
| 267 | */ |
||
| 268 | $skiplines = (int) $config->get( 'controller/jobs/product/import/csv/skip-lines', 0 ); |
||
| 269 | |||
| 270 | |||
| 271 | /** controller/jobs/product/import/csv/strict |
||
| 272 | * Log all columns from the file that are not mapped and therefore not imported |
||
| 273 | * |
||
| 274 | * Depending on the mapping, there can be more columns in the CSV file |
||
| 275 | * than those which will be imported. This can be by purpose if you want |
||
| 276 | * to import only selected columns or if you've missed to configure one |
||
| 277 | * or more columns. This configuration option will log all columns that |
||
| 278 | * have not been imported if set to true. Otherwise, the left over fields |
||
| 279 | * in the imported line will be silently ignored. |
||
| 280 | * |
||
| 281 | * @param boolen True if not imported columns should be logged, false if not |
||
| 282 | * @since 2015.08 |
||
| 283 | * @category User |
||
| 284 | * @category Developer |
||
| 285 | * @see controller/jobs/product/import/csv/domains |
||
| 286 | * @see controller/jobs/product/import/csv/mapping |
||
| 287 | * @see controller/jobs/product/import/csv/skip-lines |
||
| 288 | * @see controller/jobs/product/import/csv/converter |
||
| 289 | * @see controller/jobs/product/import/csv/backup |
||
| 290 | * @see controller/common/product/import/csv/max-size |
||
| 291 | */ |
||
| 292 | $strict = (bool) $config->get( 'controller/jobs/product/import/csv/strict', true ); |
||
| 293 | |||
| 294 | |||
| 295 | /** controller/jobs/product/import/csv/backup |
||
| 296 | * Name of the backup for sucessfully imported files |
||
| 297 | * |
||
| 298 | * After a CSV file was imported successfully, you can move it to another |
||
| 299 | * location, so it won't be imported again and isn't overwritten by the |
||
| 300 | * next file that is stored at the same location in the file system. |
||
| 301 | * |
||
| 302 | * You should use an absolute path to be sure but can be relative path |
||
| 303 | * if you absolutely know from where the job will be executed from. The |
||
| 304 | * name of the new backup location can contain placeholders understood |
||
| 305 | * by the PHP strftime() function to create dynamic paths, e.g. "backup/%Y-%m-%d" |
||
| 306 | * which would create "backup/2000-01-01". For more information about the |
||
| 307 | * strftime() placeholders, please have a look into the PHP documentation of |
||
| 308 | * the {@link http://php.net/manual/en/function.strftime.php strftime() function}. |
||
| 309 | * |
||
| 310 | * '''Note:''' If no backup name is configured, the file or directory |
||
| 311 | * won't be moved away. Please make also sure that the parent directory |
||
| 312 | * and the new directory are writable so the file or directory could be |
||
| 313 | * moved. |
||
| 314 | * |
||
| 315 | * @param integer Name of the backup file, optionally with date/time placeholders |
||
| 316 | * @since 2015.05 |
||
| 317 | * @category Developer |
||
| 318 | * @see controller/jobs/product/import/csv/domains |
||
| 319 | * @see controller/jobs/product/import/csv/mapping |
||
| 320 | * @see controller/jobs/product/import/csv/skip-lines |
||
| 321 | * @see controller/jobs/product/import/csv/converter |
||
| 322 | * @see controller/jobs/product/import/csv/strict |
||
| 323 | * @see controller/common/product/import/csv/max-size |
||
| 324 | */ |
||
| 325 | $backup = $config->get( 'controller/jobs/product/import/csv/backup' ); |
||
| 326 | |||
| 327 | |||
| 328 | if( !isset( $mappings['item'] ) || !is_array( $mappings['item'] ) ) |
||
| 329 | { |
||
| 330 | $msg = sprintf( 'Required mapping key "%1$s" is missing or contains no array', 'item' ); |
||
| 331 | throw new \Aimeos\Controller\Jobs\Exception( $msg ); |
||
| 332 | } |
||
| 333 | |||
| 334 | try |
||
| 335 | { |
||
| 336 | $procMappings = $mappings; |
||
| 337 | unset( $procMappings['item'] ); |
||
| 338 | |||
| 339 | $codePos = $this->getCodePosition( $mappings['item'] ); |
||
| 340 | $convlist = $this->getConverterList( $converters ); |
||
| 341 | $processor = $this->getProcessors( $procMappings ); |
||
| 342 | $container = $this->getContainer(); |
||
| 343 | $path = $container->getName(); |
||
| 344 | |||
| 345 | $msg = sprintf( 'Started product import from "%1$s" (%2$s)', $path, __CLASS__ ); |
||
| 346 | $logger->log( $msg, \Aimeos\MW\Logger\Base::NOTICE ); |
||
| 347 | |||
| 348 | foreach( $container as $content ) |
||
| 349 | { |
||
| 350 | $name = $content->getName(); |
||
| 351 | |||
| 352 | for( $i = 0; $i < $skiplines; $i++ ) { |
||
| 353 | $content->next(); |
||
| 354 | } |
||
| 355 | |||
| 356 | while( ( $data = $this->getData( $content, $maxcnt, $codePos ) ) !== [] ) |
||
| 357 | { |
||
| 358 | $data = $this->convertData( $convlist, $data ); |
||
| 359 | $products = $this->getProducts( array_keys( $data ), $domains ); |
||
| 360 | $errcnt = $this->import( $products, $data, $mappings['item'], $processor, $strict ); |
||
| 361 | $chunkcnt = count( $data ); |
||
| 362 | |||
| 363 | $msg = 'Imported product lines from "%1$s": %2$d/%3$d (%4$s)'; |
||
| 364 | $logger->log( sprintf( $msg, $name, $chunkcnt - $errcnt, $chunkcnt, __CLASS__ ), \Aimeos\MW\Logger\Base::NOTICE ); |
||
| 365 | |||
| 366 | $errors += $errcnt; |
||
| 367 | $total += $chunkcnt; |
||
| 368 | unset( $products, $data ); |
||
| 369 | } |
||
| 370 | } |
||
| 371 | |||
| 372 | $container->close(); |
||
| 373 | } |
||
| 374 | catch( \Exception $e ) |
||
| 375 | { |
||
| 376 | $logger->log( 'Product import error: ' . $e->getMessage() ); |
||
| 377 | $logger->log( $e->getTraceAsString() ); |
||
| 378 | |||
| 379 | throw new \Aimeos\Controller\Jobs\Exception( $e->getMessage() ); |
||
| 380 | } |
||
| 381 | |||
| 382 | $msg = 'Finished product import from "%1$s": %2$d successful, %3$s errors, %4$s total (%5$s)'; |
||
| 383 | $logger->log( sprintf( $msg, $path, $total - $errors, $errors, $total, __CLASS__ ), \Aimeos\MW\Logger\Base::NOTICE ); |
||
| 384 | |||
| 385 | if( $errors > 0 ) |
||
| 386 | { |
||
| 387 | $msg = sprintf( 'Invalid product lines in "%1$s": %2$d/%3$d', $path, $errors, $total ); |
||
| 388 | throw new \Aimeos\Controller\Jobs\Exception( $msg ); |
||
| 389 | } |
||
| 390 | |||
| 391 | if( !empty( $backup ) && @rename( $path, strftime( $backup ) ) === false ) { |
||
| 392 | throw new \Aimeos\Controller\Jobs\Exception( sprintf( 'Unable to move imported file' ) ); |
||
| 393 | } |
||
| 394 | } |
||
| 395 | |||
| 616 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.