Conditions | 6 |
Paths | 11 |
Total Lines | 108 |
Code Lines | 25 |
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 |
||
136 | public function import( $filepath ) |
||
137 | { |
||
138 | $context = $this->getContext(); |
||
139 | $config = $context->getConfig(); |
||
140 | $logger = $context->getLogger(); |
||
141 | |||
142 | /** controller/common/stock/import/csv/max-size |
||
143 | * Maximum number of CSV rows to import at once |
||
144 | * |
||
145 | * It's more efficient to read and import more than one row at a time |
||
146 | * to speed up the import. Usually, the bigger the chunk that is imported |
||
147 | * at once, the less time the importer will need. The downside is that |
||
148 | * the amount of memory required by the import process will increase as |
||
149 | * well. Therefore, it's a trade-off between memory consumption and |
||
150 | * import speed. |
||
151 | * |
||
152 | * '''Note:''' The maximum size is 10000 records |
||
153 | * |
||
154 | * @param integer Number of rows |
||
155 | * @since 2019.04 |
||
156 | * @category Developer |
||
157 | * @see controller/jobs/stock/import/csv/backup |
||
158 | * @see controller/jobs/stock/import/csv/skip-lines |
||
159 | */ |
||
160 | $maxcnt = (int) $config->get( 'controller/common/stock/import/csv/max-size', 1000 ); |
||
161 | |||
162 | /** controller/jobs/stock/import/csv/skip-lines |
||
163 | * Number of rows skipped in front of each CSV files |
||
164 | * |
||
165 | * Some CSV files contain header information describing the content of |
||
166 | * the column values. These data is for informational purpose only and |
||
167 | * can't be imported into the database. Using this option, you can |
||
168 | * define the number of lines that should be left out before the import |
||
169 | * begins. |
||
170 | * |
||
171 | * @param integer Number of rows |
||
172 | * @since 2019.04 |
||
173 | * @category Developer |
||
174 | * @see controller/jobs/stock/import/csv/backup |
||
175 | * @see controller/common/stock/import/csv/max-size |
||
176 | */ |
||
177 | $skiplines = (int) $config->get( 'controller/jobs/stock/import/csv/skip-lines', 0 ); |
||
178 | |||
179 | /** controller/jobs/stock/import/csv/backup |
||
180 | * Name of the backup for sucessfully imported files |
||
181 | * |
||
182 | * After a CSV file was imported successfully, you can move it to another |
||
183 | * location, so it won't be imported again and isn't overwritten by the |
||
184 | * next file that is stored at the same location in the file system. |
||
185 | * |
||
186 | * You should use an absolute path to be sure but can be relative path |
||
187 | * if you absolutely know from where the job will be executed from. The |
||
188 | * name of the new backup location can contain placeholders understood |
||
189 | * by the PHP strftime() function to create dynamic paths, e.g. "backup/%Y-%m-%d" |
||
190 | * which would create "backup/2000-01-01". For more information about the |
||
191 | * strftime() placeholders, please have a look into the PHP documentation of |
||
192 | * the {@link http://php.net/manual/en/function.strftime.php strftime() function}. |
||
193 | * |
||
194 | * '''Note:''' If no backup name is configured, the file or directory |
||
195 | * won't be moved away. Please make also sure that the parent directory |
||
196 | * and the new directory are writable so the file or directory could be |
||
197 | * moved. |
||
198 | * |
||
199 | * @param integer Name of the backup file, optionally with date/time placeholders |
||
200 | * @since 2019.04 |
||
201 | * @category Developer |
||
202 | * @see controller/common/stock/import/csv/max-size |
||
203 | * @see controller/jobs/stock/import/csv/skip-lines |
||
204 | */ |
||
205 | $backup = $config->get( 'controller/jobs/stock/import/csv/backup' ); |
||
206 | |||
207 | |||
208 | $total = 0; |
||
209 | $container = $this->getContainer( $filepath ); |
||
210 | |||
211 | try |
||
212 | { |
||
213 | $msg = sprintf( 'Started stock import from "%1$s" (%2$s)', $container->getName(), __CLASS__ ); |
||
214 | $logger->log( $msg, \Aimeos\MW\Logger\Base::NOTICE ); |
||
215 | |||
216 | foreach( $container as $content ) |
||
217 | { |
||
218 | $name = $content->getName(); |
||
219 | |||
220 | for( $i = 0; $i < $skiplines; $i++ ) { |
||
221 | $content->next(); |
||
222 | } |
||
223 | |||
224 | $total += $this->importStocks( $content, $maxcnt ); |
||
225 | } |
||
226 | |||
227 | $container->close(); |
||
228 | } |
||
229 | catch( \Exception $e ) |
||
230 | { |
||
231 | $container->close(); |
||
232 | |||
233 | $logger->log( 'Stock import error: ' . $e->getMessage() ); |
||
234 | $logger->log( $e->getTraceAsString() ); |
||
235 | |||
236 | throw new \Aimeos\Controller\Jobs\Exception( $e->getMessage() ); |
||
237 | } |
||
238 | |||
239 | if( !empty( $backup ) && @rename( $path, strftime( $backup ) ) === false ) { |
||
240 | throw new \Aimeos\Controller\Jobs\Exception( sprintf( 'Unable to move imported file' ) ); |
||
241 | } |
||
242 | |||
243 | return $total; |
||
244 | } |
||
392 |