| Conditions | 8 |
| Paths | 48 |
| Total Lines | 90 |
| Code Lines | 40 |
| 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 |
||
| 101 | public function process( \Aimeos\MShop\Product\Item\Iface $product, array $data ) : array |
||
| 102 | { |
||
| 103 | $context = $this->getContext(); |
||
| 104 | $manager = \Aimeos\MShop::create( $context, 'supplier' ); |
||
| 105 | $listManager = \Aimeos\MShop::create( $context, 'supplier/lists' ); |
||
| 106 | |||
| 107 | /** controller/common/product/import/csv/separator |
||
| 108 | * Single separator character for multiple entries in one field of the import file |
||
| 109 | * |
||
| 110 | * The product importer is able split the content of a field from the import |
||
| 111 | * file into several entries based on the given separator character. Thus, |
||
| 112 | * you can create more compact import files and handle a variable range |
||
| 113 | * of entries better. The default separator character is a new line. |
||
| 114 | * |
||
| 115 | * '''Caution:''' The separator character must not be part of any entry |
||
| 116 | * in the field. Otherwise, you will get invalid entries and the importer |
||
| 117 | * may fail! |
||
| 118 | * |
||
| 119 | * @param string Single separator character |
||
| 120 | * @since 2020.07 |
||
| 121 | * @category User |
||
| 122 | * @category Developer |
||
| 123 | * @see controller/common/product/import/csv/domains |
||
| 124 | */ |
||
| 125 | $separator = $context->getConfig()->get( 'controller/common/product/import/csv/separator', "\n" ); |
||
| 126 | |||
| 127 | $manager->begin(); |
||
| 128 | |||
| 129 | try |
||
| 130 | { |
||
| 131 | $listMap = []; |
||
| 132 | $prodid = $product->getId(); |
||
| 133 | $map = $this->getMappedChunk( $data, $this->getMapping() ); |
||
| 134 | $listItems = $this->getListItems( $prodid, $this->listTypes ); |
||
|
|
|||
| 135 | |||
| 136 | foreach( $listItems as $listItem ) |
||
| 137 | { |
||
| 138 | $listMap[$listItem->getParentId()][$listItem->getType()] = $listItem; |
||
| 139 | } |
||
| 140 | |||
| 141 | foreach( $map as $pos => $list ) |
||
| 142 | { |
||
| 143 | if( $this->checkEntry( $list ) === false ) |
||
| 144 | { |
||
| 145 | continue; |
||
| 146 | } |
||
| 147 | |||
| 148 | $codes = explode( $separator, $this->getValue( $list, 'supplier.code', '' ) ); |
||
| 149 | $listtype = $this->getValue( $list, 'supplier.lists.type', 'default' ); |
||
| 150 | $this->addType( 'supplier/lists/type', 'product', $listtype ); |
||
| 151 | |||
| 152 | foreach( $codes as $code ) |
||
| 153 | { |
||
| 154 | $code = trim( $code ); |
||
| 155 | |||
| 156 | if( ($catid = $this->cache->get( $code )) === null ) |
||
| 157 | { |
||
| 158 | $msg = 'No supplier for code "%1$s" available when importing product with code "%2$s"'; |
||
| 159 | throw new \Aimeos\Controller\Jobs\Exception( sprintf( $msg, $code, $product->getCode() ) ); |
||
| 160 | } |
||
| 161 | |||
| 162 | $list['supplier.lists.parentid'] = $catid; |
||
| 163 | $list['supplier.lists.refid'] = $prodid; |
||
| 164 | $list['supplier.lists.domain'] = 'product'; |
||
| 165 | |||
| 166 | if( isset( $listMap[$catid][$listtype] ) ) |
||
| 167 | { |
||
| 168 | $listItem = $listMap[$catid][$listtype]; |
||
| 169 | unset( $listItems[$listItem->getId()] ); |
||
| 170 | } else |
||
| 171 | { |
||
| 172 | $listItem = $listManager->createItem()->setType( $listtype ); |
||
| 173 | } |
||
| 174 | |||
| 175 | $listItem = $listItem->setPosition( $pos++ )->fromArray( $list, true ); |
||
| 176 | $listManager->saveItem( $listItem, false ); |
||
| 177 | } |
||
| 178 | } |
||
| 179 | |||
| 180 | $listManager->deleteItems( $listItems->toArray() ); |
||
| 181 | $data = $this->getObject()->process( $product, $data ); |
||
| 182 | |||
| 183 | $manager->commit(); |
||
| 184 | } catch( \Exception $e ) |
||
| 185 | { |
||
| 186 | $manager->rollback(); |
||
| 187 | throw $e; |
||
| 188 | } |
||
| 189 | |||
| 190 | return $data; |
||
| 191 | } |
||
| 266 |