Conditions | 9 |
Paths | 68 |
Total Lines | 89 |
Code Lines | 40 |
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 |
||
83 | public function process( \Aimeos\MShop\Product\Item\Iface $product, array $data ) |
||
84 | { |
||
85 | $context = $this->getContext(); |
||
86 | $manager = \Aimeos\MShop\Factory::createManager( $context, 'catalog' ); |
||
87 | $listManager = \Aimeos\MShop\Factory::createManager( $context, 'catalog/lists' ); |
||
88 | |||
89 | /** controller/common/product/import/csv/separator |
||
90 | * Single separator character for multiple entries in one field of the import file |
||
91 | * |
||
92 | * The product importer is able split the content of a field from the import |
||
93 | * file into several entries based on the given separator character. Thus, |
||
94 | * you can create more compact import files and handle a variable range |
||
95 | * of entries better. The default separator character is a new line. |
||
96 | * |
||
97 | * '''Caution:''' The separator character must not be part of any entry |
||
98 | * in the field. Otherwise, you will get invalid entries and the importer |
||
99 | * may fail! |
||
100 | * |
||
101 | * @param string Single separator character |
||
102 | * @since 2015.07 |
||
103 | * @category User |
||
104 | * @category Developer |
||
105 | * @see controller/common/product/import/csv/domains |
||
106 | */ |
||
107 | $separator = $context->getConfig()->get( 'controller/common/product/import/csv/separator', "\n" ); |
||
108 | |||
109 | $manager->begin(); |
||
110 | |||
111 | try |
||
112 | { |
||
113 | $listMap = array(); |
||
114 | $prodid = $product->getId(); |
||
115 | $map = $this->getMappedChunk( $data ); |
||
116 | $listItems = $this->getListItems( $prodid, $this->listTypes ); |
||
117 | |||
118 | foreach( $listItems as $listItem ) { |
||
119 | $listMap[ $listItem->getParentId() ][ $listItem->getType() ] = $listItem; |
||
120 | } |
||
121 | |||
122 | foreach( $map as $pos => $list ) |
||
123 | { |
||
124 | if( $this->checkEntry( $list ) === false ) { |
||
125 | continue; |
||
126 | } |
||
127 | |||
128 | $codes = explode( $separator, $list['catalog.code'] ); |
||
129 | $type = ( isset( $list['catalog.lists.type'] ) ? $list['catalog.lists.type'] : 'default' ); |
||
130 | |||
131 | foreach( $codes as $code ) |
||
132 | { |
||
133 | if( ( $catid = $this->cache->get( $code ) ) === null ) |
||
134 | { |
||
135 | $msg = 'No category for code "%1$s" available when importing product with code "%2$s"'; |
||
136 | throw new \Aimeos\Controller\Jobs\Exception( sprintf( $msg, $code, $product->getCode() ) ); |
||
137 | } |
||
138 | |||
139 | $list['catalog.lists.typeid'] = $this->getTypeId( 'catalog/lists/type', 'product', $type ); |
||
140 | $list['catalog.lists.parentid'] = $catid; |
||
141 | $list['catalog.lists.refid'] = $prodid; |
||
142 | $list['catalog.lists.domain'] = 'product'; |
||
143 | |||
144 | if( isset( $listMap[$catid][$type] ) ) |
||
145 | { |
||
146 | $listItem = $listMap[$catid][$type]; |
||
147 | unset( $listItems[ $listItem->getId() ] ); |
||
148 | } |
||
149 | else |
||
150 | { |
||
151 | $listItem = $listManager->createItem(); |
||
152 | } |
||
153 | |||
154 | $listItem->fromArray( $this->addListItemDefaults( $list, $pos++ ) ); |
||
155 | $listManager->saveItem( $listItem ); |
||
156 | } |
||
157 | } |
||
158 | |||
159 | $listManager->deleteItems( array_keys( $listItems ) ); |
||
160 | $remaining = $this->getObject()->process( $product, $data ); |
||
161 | |||
162 | $manager->commit(); |
||
163 | } |
||
164 | catch( \Exception $e ) |
||
165 | { |
||
166 | $manager->rollback(); |
||
167 | throw $e; |
||
168 | } |
||
169 | |||
170 | return $remaining; |
||
171 | } |
||
172 | |||
241 |