| Total Complexity | 40 |
| Total Lines | 479 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| 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\Base |
||
| 22 | implements \Aimeos\Controller\Jobs\Iface |
||
| 23 | { |
||
| 24 | /** controller/jobs/product/import/xml/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\Xml\Standard |
||
| 35 | * |
||
| 36 | * and you want to replace it with your own version named |
||
| 37 | * |
||
| 38 | * \Aimeos\Controller\Jobs\Product\Import\Xml\Myxml |
||
| 39 | * |
||
| 40 | * then you have to set the this configuration option: |
||
| 41 | * |
||
| 42 | * controller/jobs/product/import/xml/name = Myxml |
||
| 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 "MyXml"! |
||
| 52 | * |
||
| 53 | * @param string Last part of the class name |
||
| 54 | * @since 2019.04 |
||
| 55 | */ |
||
| 56 | |||
| 57 | /** controller/jobs/product/import/xml/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/xml/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 2019.04 |
||
| 77 | * @see controller/jobs/common/decorators/default |
||
| 78 | * @see controller/jobs/product/import/xml/decorators/global |
||
| 79 | * @see controller/jobs/product/import/xml/decorators/local |
||
| 80 | */ |
||
| 81 | |||
| 82 | /** controller/jobs/product/import/xml/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/xml/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 2019.04 |
||
| 100 | * @see controller/jobs/common/decorators/default |
||
| 101 | * @see controller/jobs/product/import/xml/decorators/excludes |
||
| 102 | * @see controller/jobs/product/import/xml/decorators/local |
||
| 103 | */ |
||
| 104 | |||
| 105 | /** controller/jobs/product/import/xml/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\Xml\Decorator\*") around the job |
||
| 115 | * controller. |
||
| 116 | * |
||
| 117 | * controller/jobs/product/import/xml/decorators/local = array( 'decorator2' ) |
||
| 118 | * |
||
| 119 | * This would add the decorator named "decorator2" defined by |
||
| 120 | * "\Aimeos\Controller\Jobs\Product\Import\Xml\Decorator\Decorator2" |
||
| 121 | * only to the job controller. |
||
| 122 | * |
||
| 123 | * @param array List of decorator names |
||
| 124 | * @since 2019.04 |
||
| 125 | * @see controller/jobs/common/decorators/default |
||
| 126 | * @see controller/jobs/product/import/xml/decorators/excludes |
||
| 127 | * @see controller/jobs/product/import/xml/decorators/global |
||
| 128 | */ |
||
| 129 | |||
| 130 | |||
| 131 | use \Aimeos\Controller\Jobs\Common\Types; |
||
| 132 | use \Aimeos\Controller\Jobs\Common\Import\Xml\Traits; |
||
| 133 | |||
| 134 | |||
| 135 | /** |
||
| 136 | * Returns the localized name of the job. |
||
| 137 | * |
||
| 138 | * @return string Name of the job |
||
| 139 | */ |
||
| 140 | public function getName() : string |
||
| 141 | { |
||
| 142 | return $this->context()->translate( 'controller/jobs', 'Product import XML' ); |
||
| 143 | } |
||
| 144 | |||
| 145 | |||
| 146 | /** |
||
| 147 | * Returns the localized description of the job. |
||
| 148 | * |
||
| 149 | * @return string Description of the job |
||
| 150 | */ |
||
| 151 | public function getDescription() : string |
||
| 154 | } |
||
| 155 | |||
| 156 | |||
| 157 | /** |
||
| 158 | * Executes the job. |
||
| 159 | * |
||
| 160 | * @throws \Aimeos\Controller\Jobs\Exception If an error occurs |
||
| 161 | */ |
||
| 162 | public function run() |
||
| 163 | { |
||
| 164 | $context = $this->context(); |
||
| 165 | $logger = $context->logger(); |
||
| 166 | $process = $context->process(); |
||
| 167 | |||
| 168 | try |
||
| 169 | { |
||
| 170 | $fs = $context->fs( 'fs-import' ); |
||
| 171 | $site = $context->locale()->getSiteItem()->getCode(); |
||
| 172 | $location = $this->location() . '/' . $site; |
||
| 173 | |||
| 174 | if( $fs->isDir( $location ) === false ) { |
||
| 175 | return; |
||
| 176 | } |
||
| 177 | |||
| 178 | $logger->info( sprintf( 'Started product import from "%1$s"', $location ), 'import/xml/product' ); |
||
| 179 | |||
| 180 | $fcn = function( \Aimeos\MShop\ContextIface $context, string $path ) { |
||
| 181 | $this->import( $context, $path ); |
||
| 182 | }; |
||
| 183 | |||
| 184 | foreach( map( $fs->scan( $location ) )->sort() as $filename ) |
||
| 185 | { |
||
| 186 | $path = $location . '/' . $filename; |
||
| 187 | |||
| 188 | if( $filename[0] === '.' || $fs instanceof \Aimeos\Base\Filesystem\DirIface && $fs->isDir( $path ) ) { |
||
| 189 | continue; |
||
| 190 | } |
||
| 191 | |||
| 192 | $process->start( $fcn, [$context, $path] ); |
||
| 193 | } |
||
| 194 | |||
| 195 | $process->wait(); |
||
| 196 | |||
| 197 | $logger->info( sprintf( 'Finished product import from "%1$s"', $location ), 'import/xml/product' ); |
||
| 198 | } |
||
| 199 | catch( \Exception $e ) |
||
| 200 | { |
||
| 201 | $logger->error( 'Product import error: ' . $e->getMessage() . "\n" . $e->getTraceAsString(), 'import/xml/product' ); |
||
| 202 | $this->mail( 'Product XML import error', $e->getMessage() . "\n" . $e->getTraceAsString() ); |
||
| 203 | throw $e; |
||
| 204 | } |
||
| 205 | } |
||
| 206 | |||
| 207 | |||
| 208 | /** |
||
| 209 | * Returns the directory for storing imported files |
||
| 210 | * |
||
| 211 | * @return string Directory for storing imported files |
||
| 212 | */ |
||
| 213 | protected function backup() : string |
||
| 214 | { |
||
| 215 | /** controller/jobs/product/import/xml/backup |
||
| 216 | * Name of the backup for sucessfully imported files |
||
| 217 | * |
||
| 218 | * After a XML file was imported successfully, you can move it to another |
||
| 219 | * location, so it won't be imported again and isn't overwritten by the |
||
| 220 | * next file that is stored at the same location in the file system. |
||
| 221 | * |
||
| 222 | * You should use an absolute path to be sure but can be relative path |
||
| 223 | * if you absolutely know from where the job will be executed from. The |
||
| 224 | * name of the new backup location can contain placeholders understood |
||
| 225 | * by the PHP DateTime::format() method (with percent signs prefix) to |
||
| 226 | * create dynamic paths, e.g. "backup/%Y-%m-%d" which would create |
||
| 227 | * "backup/2000-01-01". For more information about the date() placeholders, |
||
| 228 | * please have a look into the PHP documentation of the |
||
| 229 | * {@link https://www.php.net/manual/en/datetime.format.php format() method}. |
||
| 230 | * |
||
| 231 | * **Note:** If no backup name is configured, the file will be removed! |
||
| 232 | * |
||
| 233 | * @param integer Name of the backup file, optionally with date/time placeholders |
||
| 234 | * @since 2019.04 |
||
| 235 | * @see controller/jobs/product/import/xml/domains |
||
| 236 | * @see controller/jobs/product/import/xml/location |
||
| 237 | * @see controller/jobs/product/import/xml/max-query |
||
| 238 | */ |
||
| 239 | $backup = $this->context()->config()->get( 'controller/jobs/product/import/xml/backup' ); |
||
| 240 | return \Aimeos\Base\Str::strtime( (string) $backup ); |
||
| 241 | } |
||
| 242 | |||
| 243 | |||
| 244 | /** |
||
| 245 | * Returns the list of domain names that should be retrieved along with the attribute items |
||
| 246 | * |
||
| 247 | * @return array List of domain names |
||
| 248 | */ |
||
| 249 | protected function domains() : array |
||
| 250 | { |
||
| 251 | /** controller/jobs/product/import/xml/domains |
||
| 252 | * List of item domain names that should be retrieved along with the attribute items |
||
| 253 | * |
||
| 254 | * For efficient processing, the items associated to the products can be |
||
| 255 | * fetched to, minimizing the number of database queries required. To be |
||
| 256 | * most effective, the list of item domain names should be used in the |
||
| 257 | * mapping configuration too, so the retrieved items will be used during |
||
| 258 | * the import. |
||
| 259 | * |
||
| 260 | * @param array Associative list of MShop item domain names |
||
| 261 | * @since 2019.04 |
||
| 262 | * @see controller/jobs/product/import/xml/backup |
||
| 263 | * @see controller/jobs/product/import/xml/location |
||
| 264 | * @see controller/jobs/product/import/xml/max-query |
||
| 265 | */ |
||
| 266 | return $this->context()->config()->get( 'controller/jobs/product/import/xml/domains', [] ); |
||
| 267 | } |
||
| 268 | |||
| 269 | |||
| 270 | /** |
||
| 271 | * Imports the XML file given by its path |
||
| 272 | * |
||
| 273 | * @param \Aimeos\MShop\ContextIface $context Context object |
||
| 274 | * @param string $path Relative path to the XML file in the file system |
||
| 275 | */ |
||
| 276 | protected function import( \Aimeos\MShop\ContextIface $context, string $path ) |
||
| 334 | } |
||
| 335 | |||
| 336 | |||
| 337 | /** |
||
| 338 | * Imports the given DOM nodes |
||
| 339 | * |
||
| 340 | * @param \DomElement[] $nodes List of nodes to import |
||
| 341 | */ |
||
| 342 | protected function importNodes( array $nodes ) |
||
| 343 | { |
||
| 344 | $replace = $this->replace(); |
||
| 345 | $map = $replace ? map() : $this->products( $nodes ); |
||
| 346 | $manager = \Aimeos\MShop::create( $this->context(), 'index' ); |
||
| 347 | |||
| 348 | foreach( $nodes as $node ) |
||
| 349 | { |
||
| 350 | $ref = $node->attributes->getNamedItem( 'ref' )?->nodeValue; |
||
|
|
|||
| 351 | $item = $map->get( $ref ) ?: $manager->create(); |
||
| 352 | |||
| 353 | if( $ref && $replace ) { |
||
| 354 | $item->setId( $ref ); |
||
| 355 | } |
||
| 356 | |||
| 357 | $manager->save( $this->process( $item, $node ) ); |
||
| 358 | $this->addType( 'product/type', 'product', $item->getType() ); |
||
| 359 | } |
||
| 360 | } |
||
| 361 | |||
| 362 | |||
| 363 | /** |
||
| 364 | * Returns the path to the directory with the XML file |
||
| 365 | * |
||
| 366 | * @return string Path to the directory with the XML file |
||
| 367 | */ |
||
| 368 | protected function location() : string |
||
| 369 | { |
||
| 370 | /** controller/jobs/product/import/xml/location |
||
| 371 | * Directory where the CSV files are stored which should be imported |
||
| 372 | * |
||
| 373 | * It's the relative path inside the "fs-import" virtual file system |
||
| 374 | * configuration. The default location of the "fs-import" file system is: |
||
| 375 | * |
||
| 376 | * * Laravel: ./storage/import/ |
||
| 377 | * * TYPO3: /uploads/tx_aimeos/.secure/import/ |
||
| 378 | * |
||
| 379 | * @param string Relative path to the XML files |
||
| 380 | * @since 2019.04 |
||
| 381 | * @see controller/jobs/product/import/xml/backup |
||
| 382 | * @see controller/jobs/product/import/xml/domains |
||
| 383 | * @see controller/jobs/product/import/xml/max-query |
||
| 384 | */ |
||
| 385 | return (string) $this->context()->config()->get( 'controller/jobs/product/import/xml/location', 'product' ); |
||
| 386 | } |
||
| 387 | |||
| 388 | |||
| 389 | /** |
||
| 390 | * Returns the maximum number of XML nodes processed at once |
||
| 391 | * |
||
| 392 | * @return int Maximum number of XML nodes |
||
| 393 | */ |
||
| 394 | protected function max() : int |
||
| 395 | { |
||
| 396 | /** controller/jobs/product/import/xml/max-query |
||
| 397 | * Maximum number of XML nodes processed at once |
||
| 398 | * |
||
| 399 | * Processing and fetching several attribute items at once speeds up importing |
||
| 400 | * the XML files. The more items can be processed at once, the faster the |
||
| 401 | * import. More items also increases the memory usage of the importer and |
||
| 402 | * thus, this parameter should be low enough to avoid reaching the memory |
||
| 403 | * limit of the PHP process. |
||
| 404 | * |
||
| 405 | * @param integer Number of XML nodes |
||
| 406 | * @since 2019.04 |
||
| 407 | * @see controller/jobs/product/import/xml/domains |
||
| 408 | * @see controller/jobs/product/import/xml/location |
||
| 409 | * @see controller/jobs/product/import/xml/backup |
||
| 410 | */ |
||
| 411 | return $this->context()->config()->get( 'controller/jobs/product/import/xml/max-query', 100 ); |
||
| 412 | } |
||
| 413 | |||
| 414 | |||
| 415 | /** |
||
| 416 | * Updates the product item and its referenced items using the given DOM node |
||
| 417 | * |
||
| 418 | * @param \Aimeos\MShop\Product\Item\Iface $item Product item object to update |
||
| 419 | * @param \DomElement $node DOM node used for updateding the product item |
||
| 420 | * @return \Aimeos\MShop\Product\Item\Iface $item Updated product item object |
||
| 421 | */ |
||
| 422 | protected function process( \Aimeos\MShop\Product\Item\Iface $item, \DomElement $node ) : \Aimeos\MShop\Product\Item\Iface |
||
| 423 | { |
||
| 424 | try |
||
| 425 | { |
||
| 426 | $list = []; |
||
| 427 | |||
| 428 | foreach( $node->attributes as $attr ) { |
||
| 429 | $list[$attr->nodeName] = $attr->nodeValue; |
||
| 430 | } |
||
| 431 | |||
| 432 | foreach( $node->childNodes as $tag ) |
||
| 433 | { |
||
| 434 | if( in_array( $tag->nodeName, ['lists', 'property'] ) ) { |
||
| 435 | $item = $this->getProcessor( $tag->nodeName )->process( $item, $tag ); |
||
| 436 | } elseif( $tag->nodeName[0] !== '#' ) { |
||
| 437 | $list[$tag->nodeName] = $tag->nodeValue; |
||
| 438 | } |
||
| 439 | } |
||
| 440 | |||
| 441 | $list['product.config'] = isset( $list['product.config'] ) ? json_decode( $list['product.config'], true ) : []; |
||
| 442 | $item->fromArray( $list, true ); |
||
| 443 | } |
||
| 444 | catch( \Exception $e ) |
||
| 445 | { |
||
| 446 | $msg = 'Product import error: ' . $e->getMessage() . "\n" . $e->getTraceAsString(); |
||
| 447 | $this->context()->logger()->error( $msg, 'import/xml/product' ); |
||
| 448 | } |
||
| 449 | |||
| 450 | return $item; |
||
| 451 | } |
||
| 452 | |||
| 453 | |||
| 454 | /** |
||
| 455 | * Returns the products referenced in the given DOM nodes |
||
| 456 | * |
||
| 457 | * @param \DomElement[] $nodes List of nodes to import |
||
| 458 | */ |
||
| 459 | protected function products( array $nodes ) : \Aimeos\Map |
||
| 460 | { |
||
| 461 | $codes = []; |
||
| 462 | |||
| 463 | foreach( $nodes as $node ) |
||
| 464 | { |
||
| 465 | if( $attr = $node->attributes->getNamedItem( 'ref' ) ) { |
||
| 466 | $codes[$attr->nodeValue] = null; |
||
| 467 | } |
||
| 468 | } |
||
| 469 | |||
| 470 | $manager = \Aimeos\MShop::create( $this->context(), 'index' ); |
||
| 471 | $filter = $manager->filter()->slice( 0, count( $codes ) )->add( ['product.code' => array_keys( $codes )] ); |
||
| 472 | |||
| 473 | return $manager->search( $filter, $this->domains() )->col( null, 'product.code' ); |
||
| 474 | } |
||
| 475 | |||
| 476 | |||
| 477 | /** |
||
| 478 | * Returns the value for replacing existing products |
||
| 479 | * |
||
| 480 | * @return bool TRUE to replace products, FALSE to update products |
||
| 481 | */ |
||
| 482 | protected function replace() : bool |
||
| 499 | } |
||
| 500 | } |
||
| 501 |
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.