Conditions | 11 |
Paths | 565 |
Total Lines | 70 |
Code Lines | 43 |
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 |
||
80 | public function process( \Aimeos\MShop\Product\Item\Iface $product, array $data ) |
||
81 | { |
||
82 | $context = $this->getContext(); |
||
83 | $manager = \Aimeos\MShop\Factory::createManager( $context, 'media' ); |
||
84 | $listManager = \Aimeos\MShop\Factory::createManager( $context, 'product/lists' ); |
||
85 | $separator = $context->getConfig()->get( 'controller/common/product/import/csv/separator', "\n" ); |
||
86 | |||
87 | $manager->begin(); |
||
88 | |||
89 | try |
||
90 | { |
||
91 | $map = $this->getMappedChunk( $data ); |
||
92 | $listItems = $product->getListItems( 'media' ); |
||
93 | |||
94 | foreach( $map as $pos => $list ) |
||
95 | { |
||
96 | if( $this->checkEntry( $list ) === false ) { |
||
97 | continue; |
||
98 | } |
||
99 | |||
100 | $urls = explode( $separator, $list['media.url'] ); |
||
101 | $type = ( isset( $list['media.type'] ) ? $list['media.type'] : 'default' ); |
||
102 | $typecode = ( isset( $list['product.lists.type'] ) ? $list['product.lists.type'] : 'default' ); |
||
103 | $langid = ( isset( $list['media.languageid'] ) && $list['media.languageid'] !== '' ? $list['media.languageid'] : null ); |
||
104 | |||
105 | foreach( $urls as $url ) |
||
106 | { |
||
107 | if( ( $listItem = array_shift( $listItems ) ) !== null ) { |
||
108 | $refItem = $listItem->getRefItem(); |
||
109 | } else { |
||
110 | $listItem = $listManager->createItem(); |
||
111 | $refItem = $manager->createItem(); |
||
112 | } |
||
113 | |||
114 | $list['media.typeid'] = $this->getTypeId( 'media/type', 'product', $type ); |
||
115 | $list['media.languageid'] = $langid; |
||
116 | $list['media.domain'] = 'product'; |
||
117 | $list['media.url'] = $url; |
||
118 | |||
119 | $refItem->fromArray( $this->addItemDefaults( $list ) ); |
||
120 | $manager->saveItem( $refItem ); |
||
121 | |||
122 | $list['product.lists.typeid'] = $this->getTypeId( 'product/lists/type', 'media', $typecode ); |
||
123 | $list['product.lists.parentid'] = $product->getId(); |
||
124 | $list['product.lists.refid'] = $refItem->getId(); |
||
125 | $list['product.lists.domain'] = 'media'; |
||
126 | |||
127 | $listItem->fromArray( $this->addListItemDefaults( $list, $pos++ ) ); |
||
128 | $listManager->saveItem( $listItem ); |
||
129 | } |
||
130 | } |
||
131 | |||
132 | foreach( $listItems as $listItem ) |
||
133 | { |
||
134 | $manager->deleteItem( $listItem->getRefItem()->getId() ); |
||
135 | $listManager->deleteItem( $listItem->getId() ); |
||
136 | } |
||
137 | |||
138 | $remaining = $this->getObject()->process( $product, $data ); |
||
139 | |||
140 | $manager->commit(); |
||
141 | } |
||
142 | catch( \Exception $e ) |
||
143 | { |
||
144 | $manager->rollback(); |
||
145 | throw $e; |
||
146 | } |
||
147 | |||
148 | return $remaining; |
||
149 | } |
||
150 | |||
193 |