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 | $listMap[$listItem->getParentId()][$listItem->getType()] = $listItem; |
||
138 | } |
||
139 | |||
140 | foreach( $map as $pos => $list ) |
||
141 | { |
||
142 | if( $this->checkEntry( $list ) === false ) { |
||
143 | continue; |
||
144 | } |
||
145 | |||
146 | $codes = explode( $separator, $this->getValue( $list, 'supplier.code', '' ) ); |
||
147 | $listtype = $this->getValue( $list, 'supplier.lists.type', 'default' ); |
||
148 | $this->addType( 'supplier/lists/type', 'product', $listtype ); |
||
149 | |||
150 | foreach( $codes as $code ) |
||
151 | { |
||
152 | $code = trim( $code ); |
||
153 | |||
154 | if( ( $catid = $this->cache->get( $code ) ) === null ) |
||
155 | { |
||
156 | $msg = 'No supplier for code "%1$s" available when importing product with code "%2$s"'; |
||
157 | throw new \Aimeos\Controller\Jobs\Exception( sprintf( $msg, $code, $product->getCode() ) ); |
||
158 | } |
||
159 | |||
160 | $list['supplier.lists.parentid'] = $catid; |
||
161 | $list['supplier.lists.refid'] = $prodid; |
||
162 | $list['supplier.lists.domain'] = 'product'; |
||
163 | |||
164 | if( isset( $listMap[$catid][$listtype] ) ) |
||
165 | { |
||
166 | $listItem = $listMap[$catid][$listtype]; |
||
167 | unset( $listItems[$listItem->getId()] ); |
||
168 | } |
||
169 | else |
||
170 | { |
||
171 | $listItem = $listManager->createItem()->setType( $listtype ); |
||
172 | } |
||
173 | |||
174 | $listItem = $listItem->setPosition( $pos++ )->fromArray( $list, true ); |
||
175 | $listManager->saveItem( $listItem, false ); |
||
176 | } |
||
177 | } |
||
178 | |||
179 | $listManager->deleteItems( $listItems->toArray() ); |
||
180 | $data = $this->getObject()->process( $product, $data ); |
||
181 | |||
182 | $manager->commit(); |
||
183 | } |
||
184 | catch( \Exception $e ) |
||
185 | { |
||
186 | $manager->rollback(); |
||
187 | throw $e; |
||
188 | } |
||
189 | |||
190 | return $data; |
||
191 | } |
||
262 |