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