| Conditions | 15 |
| Paths | 1250 |
| Total Lines | 74 |
| Code Lines | 42 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 46 | public function process( \Aimeos\MShop\Common\Item\Iface $item, \DOMNode $node ) : \Aimeos\MShop\Common\Item\Iface |
||
| 47 | { |
||
| 48 | \Aimeos\MW\Common\Base::checkClass( \Aimeos\MShop\Common\Item\ListRef\Iface::class, $item ); |
||
| 49 | |||
| 50 | $listItems = array_reverse( $item->getListItems( 'media', null, null, false ), true ); |
||
| 51 | $resource = $item->getResourceType(); |
||
| 52 | $context = $this->getContext(); |
||
| 53 | |||
| 54 | $mediacntl = \Aimeos\Controller\Common\Media\Factory::create( $context ); |
||
| 55 | $listManager = \Aimeos\MShop::create( $context, $resource . '/lists' ); |
||
| 56 | $manager = \Aimeos\MShop::create( $context, 'media' ); |
||
| 57 | |||
| 58 | foreach( $node->childNodes as $refNode ) |
||
| 59 | { |
||
| 60 | if( $refNode->nodeName !== 'mediaitem' ) { |
||
| 61 | continue; |
||
| 62 | } |
||
| 63 | |||
| 64 | if( ( $listItem = array_pop( $listItems ) ) === null ) { |
||
| 65 | $listItem = $listManager->createItem(); |
||
| 66 | } |
||
| 67 | |||
| 68 | if( ( $refItem = $listItem->getRefItem() ) === null ) { |
||
| 69 | $refItem = $manager->createItem(); |
||
| 70 | } |
||
| 71 | |||
| 72 | $list = []; |
||
| 73 | |||
| 74 | foreach( $refNode->childNodes as $tag ) |
||
| 75 | { |
||
| 76 | if( in_array( $tag->nodeName, ['lists', 'property'] ) ) { |
||
| 77 | $refItem = $this->getProcessor( $tag->nodeName )->process( $refItem, $tag ); |
||
| 78 | } else { |
||
| 79 | $list[$tag->nodeName] = $tag->nodeValue; |
||
| 80 | } |
||
| 81 | } |
||
| 82 | |||
| 83 | try |
||
| 84 | { |
||
| 85 | $refItem = $refItem->setUrl( $list['media.url'] ?? '' ); |
||
| 86 | |||
| 87 | if( isset( $list['media.previews'] ) && ( $map = json_decode( $list['media.previews'], true ) ) !== null ) { |
||
| 88 | $refItem->setPreviews( $map ); |
||
| 89 | } elseif( isset( $list['media.preview'] ) ) { |
||
| 90 | $refItem->setPreview( $list['media.preview'] ); |
||
| 91 | } elseif( $refItem->isModified() ) { |
||
| 92 | $refItem = $mediacntl->scale( $refItem ); |
||
| 93 | } |
||
| 94 | |||
| 95 | unset( $list['media.previews'], $list['media.preview'] ); |
||
| 96 | } |
||
| 97 | catch( \Aimeos\Controller\Common\Exception $e ) |
||
| 98 | { |
||
| 99 | $context->getLogger()->log( sprintf( 'Scaling image "%1$s" failed: %2$s', $refItem->getUrl(), $e->getMessage() ) ); |
||
| 100 | } |
||
| 101 | |||
| 102 | $refItem = $refItem->fromArray( $list ); |
||
| 103 | |||
| 104 | foreach( $refNode->attributes as $attrName => $attrNode ) { |
||
| 105 | $list[$resource . '.' . $attrName] = $attrNode->nodeValue; |
||
| 106 | } |
||
| 107 | |||
| 108 | $name = $resource . '.lists.config'; |
||
| 109 | $list[$name] = ( isset( $list[$name] ) ? (array) json_decode( $list[$name] ) : [] ); |
||
| 110 | $name = $resource . '.lists.type'; |
||
| 111 | $list[$name] = ( isset( $list[$name] ) ? $list[$name] : 'default' ); |
||
| 112 | |||
| 113 | $this->addType( $resource . '/lists/type', 'media', $list[$resource . '.lists.type'] ); |
||
| 114 | |||
| 115 | $listItem = $listItem->fromArray( $list ); |
||
| 116 | $item->addListItem( 'media', $listItem, $refItem ); |
||
| 117 | } |
||
| 118 | |||
| 119 | return $item->deleteListItems( $listItems ); |
||
| 120 | } |
||
| 122 |