| Conditions | 10 |
| Paths | 74 |
| Total Lines | 55 |
| 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 |
||
| 66 | public function process( \Aimeos\MShop\Product\Item\Iface $product, array $data ) |
||
| 67 | { |
||
| 68 | $manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'stock' ); |
||
| 69 | $manager->begin(); |
||
| 70 | |||
| 71 | try |
||
| 72 | { |
||
| 73 | $map = $this->getMappedChunk( $data, $this->getMapping() ); |
||
| 74 | $items = $this->getStockItems( $product->getCode() ); |
||
| 75 | |||
| 76 | foreach( $map as $pos => $list ) |
||
| 77 | { |
||
| 78 | if( !array_key_exists( 'stock.stocklevel', $list ) ) { |
||
| 79 | continue; |
||
| 80 | } |
||
| 81 | |||
| 82 | $list['stock.productcode'] = $product->getCode(); |
||
| 83 | $list['stock.type'] = trim( isset( $list['stock.type'] ) ? $list['stock.type'] : 'default' ); |
||
| 84 | |||
| 85 | if( !in_array( $list['stock.type'], $this->types ) ) |
||
| 86 | { |
||
| 87 | $msg = sprintf( 'Invalid type "%1$s" (%2$s)', $list['stock.type'], 'stock' ); |
||
| 88 | throw new \Aimeos\Controller\Common\Exception( $msg ); |
||
| 89 | } |
||
| 90 | |||
| 91 | if( isset( $list['stock.dateback'] ) && trim( $list['stock.dateback'] ) === '' ) { |
||
| 92 | $list['stock.dateback'] = null; |
||
| 93 | } |
||
| 94 | |||
| 95 | if( trim( $list['stock.stocklevel'] ) === '' ) { |
||
| 96 | $list['stock.stocklevel'] = null; |
||
| 97 | } |
||
| 98 | |||
| 99 | if( ( $item = array_pop( $items ) ) === null ) { |
||
| 100 | $item = $manager->createItem(); |
||
| 101 | } |
||
| 102 | |||
| 103 | $item->fromArray( $list ); |
||
| 104 | $manager->saveItem( $item, false ); |
||
| 105 | } |
||
| 106 | |||
| 107 | $manager->deleteItems( array_keys( $items ) ); |
||
| 108 | |||
| 109 | $data = $this->getObject()->process( $product, $data ); |
||
| 110 | |||
| 111 | $manager->commit(); |
||
| 112 | } |
||
| 113 | catch( \Exception $e ) |
||
| 114 | { |
||
| 115 | $manager->rollback(); |
||
| 116 | throw $e; |
||
| 117 | } |
||
| 118 | |||
| 119 | return $data; |
||
| 120 | } |
||
| 121 | |||
| 139 |