| Conditions | 11 |
| Paths | 33 |
| Total Lines | 82 |
| Code Lines | 47 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 83 | public function process( \Aimeos\MShop\Product\Item\Iface $product, array $data ) |
||
| 84 | { |
||
| 85 | $context = $this->getContext(); |
||
| 86 | $manager = \Aimeos\MShop\Factory::createManager( $context, 'attribute' ); |
||
| 87 | $listManager = \Aimeos\MShop\Factory::createManager( $context, 'product/lists' ); |
||
| 88 | $separator = $context->getConfig()->get( 'controller/common/product/import/csv/separator', "\n" ); |
||
| 89 | |||
| 90 | $manager->begin(); |
||
| 91 | |||
| 92 | try |
||
| 93 | { |
||
| 94 | $pos = 0; |
||
| 95 | $delete = array(); |
||
| 96 | $map = $this->getMappedChunk( $data ); |
||
| 97 | $listItems = $product->getListItems( 'attribute', $this->listTypes ); |
||
| 98 | |||
| 99 | foreach( $listItems as $listId => $listItem ) |
||
| 100 | { |
||
| 101 | if( isset( $map[$pos] ) ) |
||
| 102 | { |
||
| 103 | if( !isset( $map[$pos]['attribute.code'] ) || !isset( $map[$pos]['attribute.type'] ) ) |
||
| 104 | { |
||
| 105 | unset( $map[$pos] ); |
||
| 106 | continue; |
||
| 107 | } |
||
| 108 | |||
| 109 | if( $this->checkMatch( $listItem, $map[$pos] ) === true ) |
||
| 110 | { |
||
| 111 | $pos++; |
||
| 112 | continue; |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | $listItems[$listId] = null; |
||
| 117 | $delete[] = $listId; |
||
| 118 | $pos++; |
||
| 119 | } |
||
| 120 | |||
| 121 | $listManager->deleteItems( $delete ); |
||
| 122 | |||
| 123 | foreach( $map as $pos => $list ) |
||
| 124 | { |
||
| 125 | if( $this->checkEntry( $list ) === false ) { |
||
| 126 | continue; |
||
| 127 | } |
||
| 128 | |||
| 129 | $codes = explode( $separator, $list['attribute.code'] ); |
||
| 130 | |||
| 131 | foreach( $codes as $code ) |
||
| 132 | { |
||
| 133 | $attrItem = $this->getAttributeItem( $code, $list['attribute.type'] ); |
||
| 134 | $attrItem->fromArray( $list ); |
||
| 135 | $attrItem->setCode( $code ); |
||
| 136 | $manager->saveItem( $attrItem ); |
||
| 137 | |||
| 138 | if( ( $listItem = array_shift( $listItems ) ) === null ) { |
||
| 139 | $listItem = $listManager->createItem(); |
||
| 140 | } |
||
| 141 | |||
| 142 | $typecode = $this->getValue( $list, 'product.lists.type', 'default' ); |
||
| 143 | $list['product.lists.typeid'] = $this->getTypeId( 'product/lists/type', 'attribute', $typecode ); |
||
| 144 | $list['product.lists.refid'] = $attrItem->getId(); |
||
| 145 | $list['product.lists.parentid'] = $product->getId(); |
||
| 146 | $list['product.lists.domain'] = 'attribute'; |
||
| 147 | |||
| 148 | $listItem->fromArray( $this->addListItemDefaults( $list, $pos ) ); |
||
| 149 | $listManager->saveItem( $listItem ); |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | $remaining = $this->getObject()->process( $product, $data ); |
||
| 154 | |||
| 155 | $manager->commit(); |
||
| 156 | } |
||
| 157 | catch( \Exception $e ) |
||
| 158 | { |
||
| 159 | $manager->rollback(); |
||
| 160 | throw $e; |
||
| 161 | } |
||
| 162 | |||
| 163 | return $remaining; |
||
| 164 | } |
||
| 165 | |||
| 236 |