Conditions | 12 |
Paths | 648 |
Total Lines | 69 |
Code Lines | 31 |
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 |
||
154 | protected function saveListItems( \Aimeos\MShop\Common\Item\ListsRef\Iface $item, string $domain, |
||
155 | bool $fetch = true ) : \Aimeos\MShop\Common\Item\ListsRef\Iface |
||
156 | { |
||
157 | $context = $this->context(); |
||
158 | $rmListItems = $rmItems = $refManager = []; |
||
159 | $listManager = $this->object()->getSubManager( 'lists' ); |
||
160 | |||
161 | |||
162 | foreach( $item->getListItemsDeleted() as $listItem ) |
||
163 | { |
||
164 | $rmListItems[] = $listItem; |
||
165 | |||
166 | if( ( $refItem = $listItem->getRefItem() ) !== null ) { |
||
167 | $rmItems[$listItem->getDomain()][] = $refItem->getId(); |
||
168 | } |
||
169 | } |
||
170 | |||
171 | |||
172 | try |
||
173 | { |
||
174 | foreach( $rmItems as $refDomain => $list ) |
||
175 | { |
||
176 | $refManager[$refDomain] = \Aimeos\MShop::create( $context, $refDomain ); |
||
177 | $refManager[$refDomain]->begin(); |
||
178 | |||
179 | $refManager[$refDomain]->delete( $list ); |
||
180 | } |
||
181 | |||
182 | $listManager->delete( $rmListItems ); |
||
183 | |||
184 | |||
185 | foreach( $item->getListItems( null, null, null, false ) as $listItem ) |
||
186 | { |
||
187 | $refDomain = $listItem->getDomain(); |
||
188 | |||
189 | if( ( $refItem = $listItem->getRefItem() ) !== null ) |
||
190 | { |
||
191 | if( !isset( $refManager[$refDomain] ) ) |
||
192 | { |
||
193 | $refManager[$refDomain] = \Aimeos\MShop::create( $context, $refDomain ); |
||
194 | $refManager[$refDomain]->begin(); |
||
195 | } |
||
196 | |||
197 | $refItem = $refManager[$refDomain]->save( $refItem ); |
||
198 | $listItem->setRefId( $refItem->getId() ); |
||
199 | } |
||
200 | |||
201 | if( $listItem->getParentId() && $listItem->getParentId() != $item->getId() ) { |
||
202 | $listItem->setId( null ); // create new list item if copied |
||
203 | } |
||
204 | |||
205 | $listManager->save( $listItem->setParentId( $item->getId() ), $fetch ); |
||
206 | } |
||
207 | |||
208 | |||
209 | foreach( $refManager as $manager ) { |
||
210 | $manager->commit(); |
||
211 | } |
||
212 | } |
||
213 | catch( \Exception $e ) |
||
214 | { |
||
215 | foreach( $refManager as $manager ) { |
||
216 | $manager->rollback(); |
||
217 | } |
||
218 | |||
219 | throw $e; |
||
220 | } |
||
221 | |||
222 | return $item; |
||
223 | } |
||
225 |