| Total Complexity | 41 |
| Total Lines | 625 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Standard often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Standard, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class Standard |
||
| 21 | extends \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Base |
||
| 22 | implements \Aimeos\Controller\Jobs\Iface |
||
| 23 | { |
||
| 24 | /** controller/jobs/product/import/csv/name |
||
| 25 | * Class name of the used product suggestions scheduler controller implementation |
||
| 26 | * |
||
| 27 | * Each default job controller can be replace by an alternative imlementation. |
||
| 28 | * To use this implementation, you have to set the last part of the class |
||
| 29 | * name as configuration value so the controller factory knows which class it |
||
| 30 | * has to instantiate. |
||
| 31 | * |
||
| 32 | * For example, if the name of the default class is |
||
| 33 | * |
||
| 34 | * \Aimeos\Controller\Jobs\Product\Import\Csv\Standard |
||
| 35 | * |
||
| 36 | * and you want to replace it with your own version named |
||
| 37 | * |
||
| 38 | * \Aimeos\Controller\Jobs\Product\Import\Csv\Mycsv |
||
| 39 | * |
||
| 40 | * then you have to set the this configuration option: |
||
| 41 | * |
||
| 42 | * controller/jobs/product/import/csv/name = Mycsv |
||
| 43 | * |
||
| 44 | * The value is the last part of your own class name and it's case sensitive, |
||
| 45 | * so take care that the configuration value is exactly named like the last |
||
| 46 | * part of the class name. |
||
| 47 | * |
||
| 48 | * The allowed characters of the class name are A-Z, a-z and 0-9. No other |
||
| 49 | * characters are possible! You should always start the last part of the class |
||
| 50 | * name with an upper case character and continue only with lower case characters |
||
| 51 | * or numbers. Avoid chamel case names like "MyCsv"! |
||
| 52 | * |
||
| 53 | * @param string Last part of the class name |
||
| 54 | * @since 2015.01 |
||
| 55 | */ |
||
| 56 | |||
| 57 | /** controller/jobs/product/import/csv/decorators/excludes |
||
| 58 | * Excludes decorators added by the "common" option from the product import CSV job controller |
||
| 59 | * |
||
| 60 | * Decorators extend the functionality of a class by adding new aspects |
||
| 61 | * (e.g. log what is currently done), executing the methods of the underlying |
||
| 62 | * class only in certain conditions (e.g. only for logged in users) or |
||
| 63 | * modify what is returned to the caller. |
||
| 64 | * |
||
| 65 | * This option allows you to remove a decorator added via |
||
| 66 | * "controller/jobs/common/decorators/default" before they are wrapped |
||
| 67 | * around the job controller. |
||
| 68 | * |
||
| 69 | * controller/jobs/product/import/csv/decorators/excludes = array( 'decorator1' ) |
||
| 70 | * |
||
| 71 | * This would remove the decorator named "decorator1" from the list of |
||
| 72 | * common decorators ("\Aimeos\Controller\Jobs\Common\Decorator\*") added via |
||
| 73 | * "controller/jobs/common/decorators/default" to the job controller. |
||
| 74 | * |
||
| 75 | * @param array List of decorator names |
||
| 76 | * @since 2015.01 |
||
| 77 | * @see controller/jobs/common/decorators/default |
||
| 78 | * @see controller/jobs/product/import/csv/decorators/global |
||
| 79 | * @see controller/jobs/product/import/csv/decorators/local |
||
| 80 | */ |
||
| 81 | |||
| 82 | /** controller/jobs/product/import/csv/decorators/global |
||
| 83 | * Adds a list of globally available decorators only to the product import CSV job controller |
||
| 84 | * |
||
| 85 | * Decorators extend the functionality of a class by adding new aspects |
||
| 86 | * (e.g. log what is currently done), executing the methods of the underlying |
||
| 87 | * class only in certain conditions (e.g. only for logged in users) or |
||
| 88 | * modify what is returned to the caller. |
||
| 89 | * |
||
| 90 | * This option allows you to wrap global decorators |
||
| 91 | * ("\Aimeos\Controller\Jobs\Common\Decorator\*") around the job controller. |
||
| 92 | * |
||
| 93 | * controller/jobs/product/import/csv/decorators/global = array( 'decorator1' ) |
||
| 94 | * |
||
| 95 | * This would add the decorator named "decorator1" defined by |
||
| 96 | * "\Aimeos\Controller\Jobs\Common\Decorator\Decorator1" only to the job controller. |
||
| 97 | * |
||
| 98 | * @param array List of decorator names |
||
| 99 | * @since 2015.01 |
||
| 100 | * @see controller/jobs/common/decorators/default |
||
| 101 | * @see controller/jobs/product/import/csv/decorators/excludes |
||
| 102 | * @see controller/jobs/product/import/csv/decorators/local |
||
| 103 | */ |
||
| 104 | |||
| 105 | /** controller/jobs/product/import/csv/decorators/local |
||
| 106 | * Adds a list of local decorators only to the product import CSV job controller |
||
| 107 | * |
||
| 108 | * Decorators extend the functionality of a class by adding new aspects |
||
| 109 | * (e.g. log what is currently done), executing the methods of the underlying |
||
| 110 | * class only in certain conditions (e.g. only for logged in users) or |
||
| 111 | * modify what is returned to the caller. |
||
| 112 | * |
||
| 113 | * This option allows you to wrap local decorators |
||
| 114 | * ("\Aimeos\Controller\Jobs\Product\Import\Csv\Decorator\*") around the job |
||
| 115 | * controller. |
||
| 116 | * |
||
| 117 | * controller/jobs/product/import/csv/decorators/local = array( 'decorator2' ) |
||
| 118 | * |
||
| 119 | * This would add the decorator named "decorator2" defined by |
||
| 120 | * "\Aimeos\Controller\Jobs\Product\Import\Csv\Decorator\Decorator2" |
||
| 121 | * only to the job controller. |
||
| 122 | * |
||
| 123 | * @param array List of decorator names |
||
| 124 | * @since 2015.01 |
||
| 125 | * @see controller/jobs/common/decorators/default |
||
| 126 | * @see controller/jobs/product/import/csv/decorators/excludes |
||
| 127 | * @see controller/jobs/product/import/csv/decorators/global |
||
| 128 | */ |
||
| 129 | |||
| 130 | |||
| 131 | private ?array $types = null; |
||
| 132 | |||
| 133 | |||
| 134 | /** |
||
| 135 | * Returns the localized name of the job. |
||
| 136 | * |
||
| 137 | * @return string Name of the job |
||
| 138 | */ |
||
| 139 | public function getName() : string |
||
| 142 | } |
||
| 143 | |||
| 144 | |||
| 145 | /** |
||
| 146 | * Returns the localized description of the job. |
||
| 147 | * |
||
| 148 | * @return string Description of the job |
||
| 149 | */ |
||
| 150 | public function getDescription() : string |
||
| 153 | } |
||
| 154 | |||
| 155 | |||
| 156 | /** |
||
| 157 | * Executes the job. |
||
| 158 | * |
||
| 159 | * @throws \Aimeos\Controller\Jobs\Exception If an error occurs |
||
| 160 | */ |
||
| 161 | public function run() |
||
| 226 | } |
||
| 227 | } |
||
| 228 | |||
| 229 | |||
| 230 | /** |
||
| 231 | * Returns the directory for storing imported files |
||
| 232 | * |
||
| 233 | * @return string Directory for storing imported files |
||
| 234 | */ |
||
| 235 | protected function backup() : string |
||
| 266 | } |
||
| 267 | |||
| 268 | |||
| 269 | /** |
||
| 270 | * Checks the given product type for validity |
||
| 271 | * |
||
| 272 | * @param string|null $type Product type or null for no type |
||
| 273 | * @return string New product type |
||
| 274 | */ |
||
| 275 | protected function checkType( string $type = null ) : string |
||
| 290 | } |
||
| 291 | |||
| 292 | |||
| 293 | /** |
||
| 294 | * Adds conditions to the filter for fetching products that should be removed |
||
| 295 | * |
||
| 296 | * @param \Aimeos\Base\Criteria\Iface $filter Criteria object |
||
| 297 | * @return \Aimeos\Base\Criteria\Iface Modified criteria object |
||
| 298 | */ |
||
| 299 | protected function cleaner( \Aimeos\Base\Criteria\Iface $filter ) : \Aimeos\Base\Criteria\Iface |
||
| 300 | { |
||
| 301 | return $filter; |
||
| 302 | } |
||
| 303 | |||
| 304 | |||
| 305 | /** |
||
| 306 | * Removes all products which have been updated before the given date/time |
||
| 307 | * |
||
| 308 | * @param string $datetime Date and time in ISO format |
||
| 309 | * @return int Number of removed products |
||
| 310 | */ |
||
| 311 | protected function cleanup( string $datetime ) : int |
||
| 312 | { |
||
| 313 | $count = 0; |
||
| 314 | |||
| 315 | $manager = \Aimeos\MShop::create( $this->context(), 'product' ); |
||
| 316 | $filter = $manager->filter(); |
||
| 317 | $filter->add( 'product.mtime', '<', $datetime )->add( $filter->make( 'product:has', ['catalog', null] ), '!=', null ); |
||
| 318 | $cursor = $manager->cursor( $this->call( 'cleaner', $filter ) ); |
||
| 319 | |||
| 320 | while( $items = $manager->iterate( $cursor, ['product' => ['default']] ) ) |
||
| 321 | { |
||
| 322 | $articles = $items->filter( fn( $item ) => $item->getType() === 'select' ) |
||
| 323 | ->getRefItems( 'product', null, 'default' )->flat( 1 ); |
||
| 324 | $count = count( $articles ) + count( $items ); |
||
| 325 | |||
| 326 | $manager->delete( $articles ); |
||
| 327 | $manager->delete( $items ); |
||
| 328 | } |
||
| 329 | |||
| 330 | return $count; |
||
| 331 | } |
||
| 332 | |||
| 333 | |||
| 334 | /** |
||
| 335 | * Returns the list of domain names that should be retrieved along with the attribute items |
||
| 336 | * |
||
| 337 | * @return array List of domain names |
||
| 338 | */ |
||
| 339 | protected function domains() : array |
||
| 340 | { |
||
| 341 | /** controller/jobs/product/import/csv/domains |
||
| 342 | * List of item domain names that should be retrieved along with the product items |
||
| 343 | * |
||
| 344 | * For efficient processing, the items associated to the products can be |
||
| 345 | * fetched to, minimizing the number of database queries required. To be |
||
| 346 | * most effective, the list of item domain names should be used in the |
||
| 347 | * mapping configuration too, so the retrieved items will be used during |
||
| 348 | * the import. |
||
| 349 | * |
||
| 350 | * @param array Associative list of MShop item domain names |
||
| 351 | * @since 2018.04 |
||
| 352 | * @see controller/jobs/product/import/csv/backup |
||
| 353 | * @see controller/jobs/product/import/csv/destructive |
||
| 354 | * @see controller/jobs/product/import/csv/location |
||
| 355 | * @see controller/jobs/product/import/csv/mapping |
||
| 356 | * @see controller/jobs/product/import/csv/max-size |
||
| 357 | * @see controller/jobs/product/import/csv/skip-lines |
||
| 358 | */ |
||
| 359 | return $this->context()->config()->get( 'controller/jobs/product/import/csv/domains', [] ); |
||
| 360 | } |
||
| 361 | |||
| 362 | |||
| 363 | /** |
||
| 364 | * Returns the position of the "product.code" column from the product item mapping |
||
| 365 | * |
||
| 366 | * @param array $mapping Mapping of the "item" columns with position as key and code as value |
||
| 367 | * @return int Position of the "product.code" column |
||
| 368 | * @throws \Aimeos\Controller\Jobs\Exception If no mapping for "product.code" is found |
||
| 369 | */ |
||
| 370 | protected function getCodePosition( array $mapping ) : int |
||
| 371 | { |
||
| 372 | foreach( $mapping as $pos => $key ) |
||
| 373 | { |
||
| 374 | if( $key === 'product.code' ) { |
||
| 375 | return $pos; |
||
| 376 | } |
||
| 377 | } |
||
| 378 | |||
| 379 | throw new \Aimeos\Controller\Jobs\Exception( sprintf( 'No "product.code" column in CSV mapping found' ) ); |
||
| 380 | } |
||
| 381 | |||
| 382 | |||
| 383 | /** |
||
| 384 | * Returns the product items for the given codes |
||
| 385 | * |
||
| 386 | * @param array $codes List of product codes |
||
| 387 | * @param array $domains List of domains whose items should be fetched too |
||
| 388 | * @return \Aimeos\Map Associative list of product codes as key and product items as value |
||
| 389 | */ |
||
| 390 | protected function getProducts( array $codes, array $domains ) : \Aimeos\Map |
||
| 391 | { |
||
| 392 | $manager = \Aimeos\MShop::create( $this->context(), 'product' ); |
||
| 393 | $search = $manager->filter()->add( ['product.code' => $codes] )->slice( 0, count( $codes ) ); |
||
| 394 | |||
| 395 | return $manager->search( $search, $domains )->col( null, 'product.code' ); |
||
| 396 | } |
||
| 397 | |||
| 398 | |||
| 399 | /** |
||
| 400 | * Imports the CSV file from the given path |
||
| 401 | * |
||
| 402 | * @param string $path Relative path to the CSV file |
||
| 403 | * @return int Number of lines which couldn't be imported |
||
| 404 | */ |
||
| 405 | protected function import( string $path ) : int |
||
| 406 | { |
||
| 407 | $context = $this->context(); |
||
| 408 | $logger = $context->logger(); |
||
| 409 | |||
| 410 | $logger->info( sprintf( 'Started product import from "%1$s"', $path ), 'import/csv/product' ); |
||
| 411 | |||
| 412 | $maxcnt = $this->max(); |
||
| 413 | $skiplines = $this->skip(); |
||
| 414 | $domains = $this->domains(); |
||
| 415 | |||
| 416 | $mappings = $this->mapping(); |
||
| 417 | $processor = $this->getProcessors( $mappings ); |
||
| 418 | $codePos = $this->getCodePosition( $mappings['item'] ); |
||
| 419 | |||
| 420 | $fs = $context->fs( 'fs-import' ); |
||
| 421 | $fh = $fs->reads( $path ); |
||
| 422 | $total = $errors = 0; |
||
| 423 | |||
| 424 | for( $i = 0; $i < $skiplines; $i++ ) { |
||
| 425 | fgetcsv( $fh ); |
||
| 426 | } |
||
| 427 | |||
| 428 | while( ( $data = $this->getData( $fh, $maxcnt, $codePos ) ) !== [] ) |
||
| 429 | { |
||
| 430 | $products = $this->getProducts( array_keys( $data ), $domains ); |
||
| 431 | $errors += $this->importProducts( $products, $data, $mappings['item'], [], $processor ); |
||
| 432 | |||
| 433 | $total += count( $data ); |
||
| 434 | unset( $products, $data ); |
||
| 435 | } |
||
| 436 | |||
| 437 | $processor->finish(); |
||
| 438 | fclose( $fh ); |
||
| 439 | |||
| 440 | if( !empty( $backup = $this->backup() ) ) { |
||
| 441 | $fs->move( $path, $backup ); |
||
| 442 | } else { |
||
| 443 | $fs->rm( $path ); |
||
| 444 | } |
||
| 445 | |||
| 446 | $str = sprintf( 'Finished product import from "%1$s" (%2$d/%3$d)', $path, $errors, $total ); |
||
| 447 | $logger->info( $str, 'import/csv/product' ); |
||
| 448 | |||
| 449 | return $errors; |
||
| 450 | } |
||
| 451 | |||
| 452 | |||
| 453 | /** |
||
| 454 | * Imports the CSV data and creates new products or updates existing ones |
||
| 455 | * |
||
| 456 | * @param \Aimeos\Map $products List of products items implementing \Aimeos\MShop\Product\Item\Iface |
||
| 457 | * @param array $data Associative list of import data as index/value pairs |
||
| 458 | * @param array $mapping Associative list of positions and domain item keys |
||
| 459 | * @param array $types List of allowed product type codes |
||
| 460 | * @param \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Iface $processor Processor object |
||
| 461 | * @return int Number of products that couldn't be imported |
||
| 462 | * @throws \Aimeos\Controller\Jobs\Exception |
||
| 463 | */ |
||
| 464 | protected function importProducts( \Aimeos\Map $products, array $data, array $mapping, array $types, |
||
| 513 | } |
||
| 514 | |||
| 515 | |||
| 516 | /** |
||
| 517 | * Returns the path to the directory with the CSV file |
||
| 518 | * |
||
| 519 | * @return string Path to the directory with the CSV file |
||
| 520 | */ |
||
| 521 | protected function location() : string |
||
| 543 | } |
||
| 544 | |||
| 545 | |||
| 546 | /** |
||
| 547 | * Returns the CSV column mapping |
||
| 548 | * |
||
| 549 | * @return array CSV column mapping |
||
| 550 | */ |
||
| 551 | protected function mapping() : array |
||
| 552 | { |
||
| 553 | /** controller/jobs/product/import/csv/mapping |
||
| 554 | * List of mappings between the position in the CSV file and item keys |
||
| 555 | * |
||
| 556 | * The importer have to know which data is at which position in the CSV |
||
| 557 | * file. Therefore, you need to specify a mapping between each position |
||
| 558 | * and the MShop domain item key (e.g. "product.code") it represents. |
||
| 559 | * |
||
| 560 | * You can use all domain item keys which are used in the fromArray() |
||
| 561 | * methods of the item classes. |
||
| 562 | * |
||
| 563 | * These mappings are grouped together by their processor names, which |
||
| 564 | * are responsible for importing the data, e.g. all mappings in "item" |
||
| 565 | * will be processed by the base product importer while the mappings in |
||
| 566 | * "text" will be imported by the text processor. |
||
| 567 | * |
||
| 568 | * @param array Associative list of processor names and lists of key/position pairs |
||
| 569 | * @since 2018.04 |
||
| 570 | * @see controller/jobs/product/import/csv/backup |
||
| 571 | * @see controller/jobs/product/import/csv/destructive |
||
| 572 | * @see controller/jobs/product/import/csv/domains |
||
| 573 | * @see controller/jobs/product/import/csv/location |
||
| 574 | * @see controller/jobs/product/import/csv/max-size |
||
| 575 | * @see controller/jobs/product/import/csv/skip-lines |
||
| 576 | */ |
||
| 577 | $map = (array) $this->context()->config()->get( 'controller/jobs/product/import/csv/mapping', $this->getDefaultMapping() ); |
||
| 578 | |||
| 579 | if( !isset( $map['item'] ) || !is_array( $map['item'] ) ) |
||
| 580 | { |
||
| 581 | $msg = sprintf( 'Required mapping key "%1$s" is missing or contains no array', 'item' ); |
||
| 582 | throw new \Aimeos\Controller\Jobs\Exception( $msg ); |
||
| 583 | } |
||
| 584 | |||
| 585 | return $map; |
||
| 586 | } |
||
| 587 | |||
| 588 | |||
| 589 | /** |
||
| 590 | * Returns the maximum number of CSV rows to import at once |
||
| 591 | * |
||
| 592 | * @return int Maximum number of CSV rows to import at once |
||
| 593 | */ |
||
| 594 | protected function max() : int |
||
| 595 | { |
||
| 596 | /** controller/jobs/product/import/csv/max-size |
||
| 597 | * Maximum number of CSV rows to import at once |
||
| 598 | * |
||
| 599 | * It's more efficient to read and import more than one row at a time |
||
| 600 | * to speed up the import. Usually, the bigger the chunk that is imported |
||
| 601 | * at once, the less time the importer will need. The downside is that |
||
| 602 | * the amount of memory required by the import process will increase as |
||
| 603 | * well. Therefore, it's a trade-off between memory consumption and |
||
| 604 | * import speed. |
||
| 605 | * |
||
| 606 | * @param integer Number of rows |
||
| 607 | * @since 2018.04 |
||
| 608 | * @see controller/jobs/product/import/csv/backup |
||
| 609 | * @see controller/jobs/product/import/csv/destructive |
||
| 610 | * @see controller/jobs/product/import/csv/domains |
||
| 611 | * @see controller/jobs/product/import/csv/location |
||
| 612 | * @see controller/jobs/product/import/csv/mapping |
||
| 613 | * @see controller/jobs/product/import/csv/skip-lines |
||
| 614 | */ |
||
| 615 | return (int) $this->context()->config()->get( 'controller/jobs/product/import/csv/max-size', 1000 ); |
||
| 616 | } |
||
| 617 | |||
| 618 | |||
| 619 | /** |
||
| 620 | * Returns the number of rows skipped in front of each CSV files |
||
| 621 | * |
||
| 622 | * @return int Number of rows skipped in front of each CSV files |
||
| 623 | */ |
||
| 624 | protected function skip() : int |
||
| 645 | } |
||
| 646 | } |
||
| 647 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.