| Conditions | 8 |
| Paths | 88 |
| Total Lines | 73 |
| Code Lines | 44 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| 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 |
||
| 80 | public function process( \Aimeos\MShop\Product\Item\Iface $product, array $data ) |
||
| 81 | { |
||
| 82 | $context = $this->getContext(); |
||
| 83 | $manager = \Aimeos\MShop\Factory::createManager( $context, 'media' ); |
||
| 84 | $listManager = \Aimeos\MShop\Factory::createManager( $context, 'product/lists' ); |
||
| 85 | $separator = $context->getConfig()->get( 'controller/common/product/import/csv/separator', "\n" ); |
||
| 86 | |||
| 87 | $manager->begin(); |
||
| 88 | |||
| 89 | try |
||
| 90 | { |
||
| 91 | $map = $this->getMappedChunk( $data ); |
||
| 92 | $listItems = $product->getListItems( 'media' ); |
||
| 93 | |||
| 94 | foreach( $map as $pos => $list ) |
||
| 95 | { |
||
| 96 | if( $this->checkEntry( $list ) === false ) { |
||
| 97 | continue; |
||
| 98 | } |
||
| 99 | |||
| 100 | $urls = explode( $separator, $list['media.url'] ); |
||
| 101 | $type = $this->getValue( $list, 'media.type', 'default' ); |
||
| 102 | $typecode = $this->getValue( $list, 'product.lists.type', 'default' ); |
||
| 103 | |||
| 104 | if( ( $langid = $this->getValue( $list, 'media.languageid', null ) ) === '' ) { |
||
| 105 | $langid = null; |
||
| 106 | } |
||
| 107 | |||
| 108 | foreach( $urls as $url ) |
||
| 109 | { |
||
| 110 | if( ( $listItem = array_shift( $listItems ) ) !== null ) { |
||
| 111 | $refItem = $listItem->getRefItem(); |
||
| 112 | } else { |
||
| 113 | $listItem = $listManager->createItem(); |
||
| 114 | $refItem = $manager->createItem(); |
||
| 115 | } |
||
| 116 | |||
| 117 | $list['media.typeid'] = $this->getTypeId( 'media/type', 'product', $type ); |
||
| 118 | $list['media.languageid'] = $langid; |
||
| 119 | $list['media.domain'] = 'product'; |
||
| 120 | $list['media.url'] = $url; |
||
| 121 | |||
| 122 | $refItem->fromArray( $this->addItemDefaults( $list ) ); |
||
| 123 | $manager->saveItem( $refItem ); |
||
| 124 | |||
| 125 | $list['product.lists.typeid'] = $this->getTypeId( 'product/lists/type', 'media', $typecode ); |
||
| 126 | $list['product.lists.parentid'] = $product->getId(); |
||
| 127 | $list['product.lists.refid'] = $refItem->getId(); |
||
| 128 | $list['product.lists.domain'] = 'media'; |
||
| 129 | |||
| 130 | $listItem->fromArray( $this->addListItemDefaults( $list, $pos++ ) ); |
||
| 131 | $listManager->saveItem( $listItem ); |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | foreach( $listItems as $listItem ) |
||
| 136 | { |
||
| 137 | $manager->deleteItem( $listItem->getRefItem()->getId() ); |
||
| 138 | $listManager->deleteItem( $listItem->getId() ); |
||
| 139 | } |
||
| 140 | |||
| 141 | $remaining = $this->getObject()->process( $product, $data ); |
||
| 142 | |||
| 143 | $manager->commit(); |
||
| 144 | } |
||
| 145 | catch( \Exception $e ) |
||
| 146 | { |
||
| 147 | $manager->rollback(); |
||
| 148 | throw $e; |
||
| 149 | } |
||
| 150 | |||
| 151 | return $remaining; |
||
| 152 | } |
||
| 153 | |||
| 196 |