Conditions | 11 |
Paths | 22 |
Total Lines | 320 |
Code Lines | 59 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 1 | 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 |
||
51 | public function run() |
||
52 | { |
||
53 | $total = $errors = 0; |
||
54 | $context = $this->getContext(); |
||
55 | $config = $context->getConfig(); |
||
56 | $logger = $context->getLogger(); |
||
57 | $domains = array('media', 'text', 'address'); |
||
58 | $mappings = $this->getDefaultMapping(); |
||
59 | |||
60 | |||
61 | if( file_exists( $config->get( 'controller/jobs/supplier/import/csv/location' ) ) === false ) { |
||
62 | return; |
||
63 | } |
||
64 | |||
65 | |||
66 | /** controller/common/supplier/import/csv/domains |
||
67 | * List of item domain names that should be retrieved along with the supplier items |
||
68 | * |
||
69 | * For efficient processing, the items associated to the suppliers can be |
||
70 | * fetched to, minimizing the number of database queries required. To be |
||
71 | * most effective, the list of item domain names should be used in the |
||
72 | * mapping configuration too, so the retrieved items will be used during |
||
73 | * the import. |
||
74 | * |
||
75 | * @param array Associative list of MShop item domain names |
||
76 | * @since 2020.07 |
||
77 | * @category Developer |
||
78 | * @see controller/common/supplier/import/csv/mapping |
||
79 | * @see controller/common/supplier/import/csv/converter |
||
80 | * @see controller/common/supplier/import/csv/max-size |
||
81 | */ |
||
82 | $domains = $config->get( 'controller/common/supplier/import/csv/domains', $domains ); |
||
83 | |||
84 | /** controller/jobs/supplier/import/csv/domains |
||
85 | * List of item domain names that should be retrieved along with the supplier items |
||
86 | * |
||
87 | * This configuration setting overwrites the shared option |
||
88 | * "controller/common/supplier/import/csv/domains" if you need a |
||
89 | * specific setting for the job controller. Otherwise, you should |
||
90 | * use the shared option for consistency. |
||
91 | * |
||
92 | * @param array Associative list of MShop item domain names |
||
93 | * @since 2020.07 |
||
94 | * @category Developer |
||
95 | * @see controller/jobs/supplier/import/csv/mapping |
||
96 | * @see controller/jobs/supplier/import/csv/skip-lines |
||
97 | * @see controller/jobs/supplier/import/csv/converter |
||
98 | * @see controller/jobs/supplier/import/csv/strict |
||
99 | * @see controller/jobs/supplier/import/csv/backup |
||
100 | * @see controller/common/supplier/import/csv/max-size |
||
101 | */ |
||
102 | $domains = $config->get( 'controller/jobs/supplier/import/csv/domains', $domains ); |
||
103 | |||
104 | |||
105 | /** controller/common/supplier/import/csv/mapping |
||
106 | * List of mappings between the position in the CSV file and item keys |
||
107 | * |
||
108 | * The importer have to know which data is at which position in the CSV |
||
109 | * file. Therefore, you need to specify a mapping between each position |
||
110 | * and the MShop domain item key (e.g. "supplier.code") it represents. |
||
111 | * |
||
112 | * You can use all domain item keys which are used in the fromArray() |
||
113 | * methods of the item classes. |
||
114 | * |
||
115 | * These mappings are grouped together by their processor names, which |
||
116 | * are responsible for importing the data, e.g. all mappings in "item" |
||
117 | * will be processed by the base supplier importer while the mappings in |
||
118 | * "text" will be imported by the text processor. |
||
119 | * |
||
120 | * @param array Associative list of processor names and lists of key/position pairs |
||
121 | * @since 2020.07 |
||
122 | * @category Developer |
||
123 | * @see controller/common/supplier/import/csv/domains |
||
124 | * @see controller/common/supplier/import/csv/converter |
||
125 | * @see controller/common/supplier/import/csv/max-size |
||
126 | */ |
||
127 | $mappings = $config->get( 'controller/common/supplier/import/csv/mapping', $mappings ); |
||
128 | |||
129 | /** controller/jobs/supplier/import/csv/mapping |
||
130 | * List of mappings between the position in the CSV file and item keys |
||
131 | * |
||
132 | * This configuration setting overwrites the shared option |
||
133 | * "controller/common/supplier/import/csv/mapping" if you need a |
||
134 | * specific setting for the job controller. Otherwise, you should |
||
135 | * use the shared option for consistency. |
||
136 | * |
||
137 | * @param array Associative list of processor names and lists of key/position pairs |
||
138 | * @since 2020.07 |
||
139 | * @category Developer |
||
140 | * @see controller/jobs/supplier/import/csv/domains |
||
141 | * @see controller/jobs/supplier/import/csv/skip-lines |
||
142 | * @see controller/jobs/supplier/import/csv/converter |
||
143 | * @see controller/jobs/supplier/import/csv/strict |
||
144 | * @see controller/jobs/supplier/import/csv/backup |
||
145 | * @see controller/common/supplier/import/csv/max-size |
||
146 | */ |
||
147 | $mappings = $config->get( 'controller/jobs/supplier/import/csv/mapping', $mappings ); |
||
148 | |||
149 | |||
150 | /** controller/common/supplier/import/csv/converter |
||
151 | * List of converter names for the values at the position in the CSV file |
||
152 | * |
||
153 | * Not all data in the CSV file is already in the required format. Maybe |
||
154 | * the text encoding isn't UTF-8, the date is not in ISO format or something |
||
155 | * similar. In order to convert the data before it's imported, you can |
||
156 | * specify a list of converter objects that should be applied to the data |
||
157 | * from the CSV file. |
||
158 | * |
||
159 | * To each field in the CSV file, you can apply one or more converters, |
||
160 | * e.g. to encode a Latin text to UTF8 for the second CSV field: |
||
161 | * |
||
162 | * array( 1 => 'Text/LatinUTF8' ) |
||
163 | * |
||
164 | * Similarly, you can also apply several converters at once to the same |
||
165 | * field: |
||
166 | * |
||
167 | * array( 1 => array( 'Text/LatinUTF8', 'DateTime/EnglishISO' ) ) |
||
168 | * |
||
169 | * It would convert the data of the second CSV field first to UTF-8 and |
||
170 | * afterwards try to translate it to an ISO date format. |
||
171 | * |
||
172 | * The available converter objects are named "\Aimeos\MW\Convert\<type>_<conversion>" |
||
173 | * where <type> is the data type and <conversion> the way of the conversion. |
||
174 | * In the configuration, the type and conversion must be separated by a |
||
175 | * slash (<type>/<conversion>). |
||
176 | * |
||
177 | * '''Note:''' Keep in mind that the position of the CSV fields start at |
||
178 | * zero (0). If you only need to convert a few fields, you don't have to |
||
179 | * configure all fields. Only specify the positions in the array you |
||
180 | * really need! |
||
181 | * |
||
182 | * @param array Associative list of position/converter name (or list of names) pairs |
||
183 | * @since 2020.07 |
||
184 | * @category Developer |
||
185 | * @see controller/common/supplier/import/csv/domains |
||
186 | * @see controller/common/supplier/import/csv/mapping |
||
187 | * @see controller/common/supplier/import/csv/max-size |
||
188 | */ |
||
189 | $converters = $config->get( 'controller/common/supplier/import/csv/converter', [] ); |
||
190 | |||
191 | /** controller/jobs/supplier/import/csv/converter |
||
192 | * List of converter names for the values at the position in the CSV file |
||
193 | * |
||
194 | * This configuration setting overwrites the shared option |
||
195 | * "controller/common/supplier/import/csv/converter" if you need a |
||
196 | * specific setting for the job controller. Otherwise, you should |
||
197 | * use the shared option for consistency. |
||
198 | * |
||
199 | * @param array Associative list of position/converter name (or list of names) pairs |
||
200 | * @since 2020.07 |
||
201 | * @category Developer |
||
202 | * @see controller/jobs/supplier/import/csv/domains |
||
203 | * @see controller/jobs/supplier/import/csv/mapping |
||
204 | * @see controller/jobs/supplier/import/csv/skip-lines |
||
205 | * @see controller/jobs/supplier/import/csv/strict |
||
206 | * @see controller/jobs/supplier/import/csv/backup |
||
207 | * @see controller/common/supplier/import/csv/max-size |
||
208 | */ |
||
209 | $converters = $config->get( 'controller/jobs/supplier/import/csv/converter', $converters ); |
||
210 | |||
211 | |||
212 | /** controller/common/supplier/import/csv/max-size |
||
213 | * Maximum number of CSV rows to import at once |
||
214 | * |
||
215 | * It's more efficient to read and import more than one row at a time |
||
216 | * to speed up the import. Usually, the bigger the chunk that is imported |
||
217 | * at once, the less time the importer will need. The downside is that |
||
218 | * the amount of memory required by the import process will increase as |
||
219 | * well. Therefore, it's a trade-off between memory consumption and |
||
220 | * import speed. |
||
221 | * |
||
222 | * @param integer Number of rows |
||
223 | * @since 2020.07 |
||
224 | * @category Developer |
||
225 | * @see controller/common/supplier/import/csv/domains |
||
226 | * @see controller/common/supplier/import/csv/mapping |
||
227 | * @see controller/common/supplier/import/csv/converter |
||
228 | */ |
||
229 | $maxcnt = (int)$config->get( 'controller/common/supplier/import/csv/max-size', 1000 ); |
||
230 | |||
231 | |||
232 | /** controller/jobs/supplier/import/csv/skip-lines |
||
233 | * Number of rows skipped in front of each CSV files |
||
234 | * |
||
235 | * Some CSV files contain header information describing the content of |
||
236 | * the column values. These data is for informational purpose only and |
||
237 | * can't be imported into the database. Using this option, you can |
||
238 | * define the number of lines that should be left out before the import |
||
239 | * begins. |
||
240 | * |
||
241 | * @param integer Number of rows |
||
242 | * @since 2020.07 |
||
243 | * @category Developer |
||
244 | * @see controller/jobs/supplier/import/csv/domains |
||
245 | * @see controller/jobs/supplier/import/csv/mapping |
||
246 | * @see controller/jobs/supplier/import/csv/converter |
||
247 | * @see controller/jobs/supplier/import/csv/strict |
||
248 | * @see controller/jobs/supplier/import/csv/backup |
||
249 | * @see controller/common/supplier/import/csv/max-size |
||
250 | */ |
||
251 | $skiplines = (int)$config->get( 'controller/jobs/supplier/import/csv/skip-lines', 0 ); |
||
252 | |||
253 | |||
254 | /** controller/jobs/supplier/import/csv/strict |
||
255 | * Log all columns from the file that are not mapped and therefore not imported |
||
256 | * |
||
257 | * Depending on the mapping, there can be more columns in the CSV file |
||
258 | * than those which will be imported. This can be by purpose if you want |
||
259 | * to import only selected columns or if you've missed to configure one |
||
260 | * or more columns. This configuration option will log all columns that |
||
261 | * have not been imported if set to true. Otherwise, the left over fields |
||
262 | * in the imported line will be silently ignored. |
||
263 | * |
||
264 | * @param boolen True if not imported columns should be logged, false if not |
||
265 | * @since 2020.07 |
||
266 | * @category User |
||
267 | * @category Developer |
||
268 | * @see controller/jobs/supplier/import/csv/domains |
||
269 | * @see controller/jobs/supplier/import/csv/mapping |
||
270 | * @see controller/jobs/supplier/import/csv/skip-lines |
||
271 | * @see controller/jobs/supplier/import/csv/converter |
||
272 | * @see controller/jobs/supplier/import/csv/backup |
||
273 | * @see controller/common/supplier/import/csv/max-size |
||
274 | */ |
||
275 | $strict = (bool)$config->get( 'controller/jobs/supplier/import/csv/strict', true ); |
||
276 | |||
277 | |||
278 | /** controller/jobs/supplier/import/csv/backup |
||
279 | * Name of the backup for sucessfully imported files |
||
280 | * |
||
281 | * After a CSV file was imported successfully, you can move it to another |
||
282 | * location, so it won't be imported again and isn't overwritten by the |
||
283 | * next file that is stored at the same location in the file system. |
||
284 | * |
||
285 | * You should use an absolute path to be sure but can be relative path |
||
286 | * if you absolutely know from where the job will be executed from. The |
||
287 | * name of the new backup location can contain placeholders understood |
||
288 | * by the PHP strftime() function to create dynamic paths, e.g. "backup/%Y-%m-%d" |
||
289 | * which would create "backup/2000-01-01". For more information about the |
||
290 | * strftime() placeholders, please have a look into the PHP documentation of |
||
291 | * the {@link http://php.net/manual/en/function.strftime.php strftime() function}. |
||
292 | * |
||
293 | * '''Note:''' If no backup name is configured, the file or directory |
||
294 | * won't be moved away. Please make also sure that the parent directory |
||
295 | * and the new directory are writable so the file or directory could be |
||
296 | * moved. |
||
297 | * |
||
298 | * @param integer Name of the backup file, optionally with date/time placeholders |
||
299 | * @since 2020.07 |
||
300 | * @category Developer |
||
301 | * @see controller/jobs/supplier/import/csv/domains |
||
302 | * @see controller/jobs/supplier/import/csv/mapping |
||
303 | * @see controller/jobs/supplier/import/csv/skip-lines |
||
304 | * @see controller/jobs/supplier/import/csv/converter |
||
305 | * @see controller/jobs/supplier/import/csv/strict |
||
306 | * @see controller/common/supplier/import/csv/max-size |
||
307 | */ |
||
308 | $backup = $config->get( 'controller/jobs/supplier/import/csv/backup' ); |
||
309 | |||
310 | |||
311 | if( !isset( $mappings['item'] ) || !is_array( $mappings['item'] ) ) { |
||
312 | $msg = sprintf( 'Required mapping key "%1$s" is missing or contains no array', 'item' ); |
||
313 | throw new \Aimeos\Controller\Jobs\Exception( $msg ); |
||
314 | } |
||
315 | |||
316 | try { |
||
317 | $procMappings = $mappings; |
||
318 | unset( $procMappings['item'] ); |
||
319 | |||
320 | $codePos = $this->getCodePosition( $mappings['item'] ); |
||
321 | $convlist = $this->getConverterList( $converters ); |
||
322 | $processor = $this->getProcessors( $procMappings ); |
||
323 | $supplierMap = $this->getSupplierMap( $domains ); |
||
324 | $container = $this->getContainer(); |
||
325 | $path = $container->getName(); |
||
326 | |||
327 | |||
328 | $msg = sprintf( 'Started supplier import from "%1$s" (%2$s)', $path, __CLASS__ ); |
||
329 | $logger->log( $msg, \Aimeos\MW\Logger\Base::NOTICE ); |
||
330 | |||
331 | foreach( $container as $content ) { |
||
332 | $name = $content->getName(); |
||
333 | |||
334 | for( $i = 0; $i < $skiplines; $i++ ) { |
||
335 | $content->next(); |
||
336 | } |
||
337 | |||
338 | while ( ($data = $this->getData( $content, $maxcnt, $codePos )) !== [] ) { |
||
339 | $data = $this->convertData( $convlist, $data ); |
||
340 | $errcnt = $this->import( $supplierMap, $data, $mappings['item'], $processor, $strict ); |
||
341 | $chunkcnt = count( $data ); |
||
342 | |||
343 | $msg = 'Imported supplier lines from "%1$s": %2$d/%3$d (%4$s)'; |
||
344 | $logger->log( sprintf( $msg, $name, $chunkcnt - $errcnt, $chunkcnt, __CLASS__ ), \Aimeos\MW\Logger\Base::NOTICE ); |
||
345 | |||
346 | $errors += $errcnt; |
||
347 | $total += $chunkcnt; |
||
348 | unset( $data ); |
||
349 | } |
||
350 | } |
||
351 | |||
352 | $container->close(); |
||
353 | } catch ( \Exception $e ) { |
||
354 | $logger->log( 'Supplier import error: ' . $e->getMessage() . "\n" . $e->getTraceAsString() ); |
||
355 | $this->mail( 'Supplier CSV import error', $e->getMessage() . "\n" . $e->getTraceAsString() ); |
||
356 | throw new \Aimeos\Controller\Jobs\Exception( $e->getMessage() ); |
||
357 | } |
||
358 | |||
359 | $msg = 'Finished supplier import from "%1$s": %2$d successful, %3$s errors, %4$s total (%5$s)'; |
||
360 | $logger->log( sprintf( $msg, $path, $total - $errors, $errors, $total, __CLASS__ ), \Aimeos\MW\Logger\Base::NOTICE ); |
||
361 | |||
362 | if( $errors > 0 ) { |
||
363 | $msg = sprintf( 'Invalid supplier lines in "%1$s": %2$d/%3$d', $path, $errors, $total ); |
||
364 | $this->mail( 'Supplier CSV import error', $msg ); |
||
365 | throw new \Aimeos\Controller\Jobs\Exception( $msg ); |
||
366 | } |
||
367 | |||
368 | if( !empty( $backup ) && @rename( $path, strftime( $backup ) ) === false ) { |
||
369 | $msg = sprintf( 'Unable to move imported file "%1$s" to "%2$s"', $path, strftime( $backup ) ); |
||
370 | throw new \Aimeos\Controller\Jobs\Exception( $msg ); |
||
371 | } |
||
578 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.