Passed
Push — master ( a2156e...434824 )
by Aimeos
03:54
created

Standard::__destruct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2019-2022
6
 * @package Controller
7
 * @subpackage Jobs
8
 */
9
10
11
namespace Aimeos\Controller\Jobs\Stock\Import\Csv;
12
13
14
/**
15
 * Job controller for CSV stock imports
16
 *
17
 * @package Controller
18
 * @subpackage Jobs
19
 */
20
class Standard
21
	extends \Aimeos\Controller\Jobs\Base
22
	implements \Aimeos\Controller\Jobs\Iface
23
{
24
	/** controller/jobs/stock/import/csv/name
25
	 * Class name of the used stock suggestions scheduler controller implementation
26
	 *
27
	 * Each default job controller can be replace by an alternative imlementation.
28
	 * To use this implementation, you have to set the last part of the class
29
	 * name as configuration value so the controller factory knows which class it
30
	 * has to instantiate.
31
	 *
32
	 * For example, if the name of the default class is
33
	 *
34
	 *  \Aimeos\Controller\Jobs\Stock\Import\Csv\Standard
35
	 *
36
	 * and you want to replace it with your own version named
37
	 *
38
	 *  \Aimeos\Controller\Jobs\Stock\Import\Csv\Mycsv
39
	 *
40
	 * then you have to set the this configuration option:
41
	 *
42
	 *  controller/jobs/stock/import/csv/name = Mycsv
43
	 *
44
	 * The value is the last part of your own class name and it's case sensitive,
45
	 * so take care that the configuration value is exactly named like the last
46
	 * part of the class name.
47
	 *
48
	 * The allowed characters of the class name are A-Z, a-z and 0-9. No other
49
	 * characters are possible! You should always start the last part of the class
50
	 * name with an upper case character and continue only with lower case characters
51
	 * or numbers. Avoid chamel case names like "MyCsv"!
52
	 *
53
	 * @param string Last part of the class name
54
	 * @since 2019.04
55
	 * @category Developer
56
	 */
57
58
	/** controller/jobs/stock/import/csv/decorators/excludes
59
	 * Excludes decorators added by the "common" option from the stock import CSV job controller
60
	 *
61
	 * Decorators extend the functionality of a class by adding new aspects
62
	 * (e.g. log what is currently done), executing the methods of the underlying
63
	 * class only in certain conditions (e.g. only for logged in users) or
64
	 * modify what is returned to the caller.
65
	 *
66
	 * This option allows you to remove a decorator added via
67
	 * "controller/jobs/common/decorators/default" before they are wrapped
68
	 * around the job controller.
69
	 *
70
	 *  controller/jobs/stock/import/csv/decorators/excludes = array( 'decorator1' )
71
	 *
72
	 * This would remove the decorator named "decorator1" from the list of
73
	 * common decorators ("\Aimeos\Controller\Jobs\Common\Decorator\*") added via
74
	 * "controller/jobs/common/decorators/default" to the job controller.
75
	 *
76
	 * @param array List of decorator names
77
	 * @since 2019.04
78
	 * @category Developer
79
	 * @see controller/jobs/common/decorators/default
80
	 * @see controller/jobs/stock/import/csv/decorators/global
81
	 * @see controller/jobs/stock/import/csv/decorators/local
82
	 */
83
84
	/** controller/jobs/stock/import/csv/decorators/global
85
	 * Adds a list of globally available decorators only to the stock import CSV job controller
86
	 *
87
	 * Decorators extend the functionality of a class by adding new aspects
88
	 * (e.g. log what is currently done), executing the methods of the underlying
89
	 * class only in certain conditions (e.g. only for logged in users) or
90
	 * modify what is returned to the caller.
91
	 *
92
	 * This option allows you to wrap global decorators
93
	 * ("\Aimeos\Controller\Jobs\Common\Decorator\*") around the job controller.
94
	 *
95
	 *  controller/jobs/stock/import/csv/decorators/global = array( 'decorator1' )
96
	 *
97
	 * This would add the decorator named "decorator1" defined by
98
	 * "\Aimeos\Controller\Jobs\Common\Decorator\Decorator1" only to the job controller.
99
	 *
100
	 * @param array List of decorator names
101
	 * @since 2019.04
102
	 * @category Developer
103
	 * @see controller/jobs/common/decorators/default
104
	 * @see controller/jobs/stock/import/csv/decorators/excludes
105
	 * @see controller/jobs/stock/import/csv/decorators/local
106
	 */
107
108
	/** controller/jobs/stock/import/csv/decorators/local
109
	 * Adds a list of local decorators only to the stock import CSV job controller
110
	 *
111
	 * Decorators extend the functionality of a class by adding new aspects
112
	 * (e.g. log what is currently done), executing the methods of the underlying
113
	 * class only in certain conditions (e.g. only for logged in users) or
114
	 * modify what is returned to the caller.
115
	 *
116
	 * This option allows you to wrap local decorators
117
	 * ("\Aimeos\Controller\Jobs\Stock\Import\Csv\Decorator\*") around the job
118
	 * controller.
119
	 *
120
	 *  controller/jobs/stock/import/csv/decorators/local = array( 'decorator2' )
121
	 *
122
	 * This would add the decorator named "decorator2" defined by
123
	 * "\Aimeos\Controller\Jobs\Stock\Import\Csv\Decorator\Decorator2"
124
	 * only to the job controller.
125
	 *
126
	 * @param array List of decorator names
127
	 * @since 2019.04
128
	 * @category Developer
129
	 * @see controller/jobs/common/decorators/default
130
	 * @see controller/jobs/stock/import/csv/decorators/excludes
131
	 * @see controller/jobs/stock/import/csv/decorators/global
132
	 */
133
134
135
	use \Aimeos\Controller\Common\Common\Import\Traits;
136
137
138
	/**
139
	 * Returns the localized name of the job.
140
	 *
141
	 * @return string Name of the job
142
	 */
143
	public function getName() : string
144
	{
145
		return $this->context()->translate( 'controller/jobs', 'Stock import CSV' );
146
	}
147
148
149
	/**
150
	 * Returns the localized description of the job.
151
	 *
152
	 * @return string Description of the job
153
	 */
154
	public function getDescription() : string
155
	{
156
		return $this->context()->translate( 'controller/jobs', 'Imports new and updates existing stocks from CSV files' );
157
	}
158
159
160
	/**
161
	 * Executes the job
162
	 *
163
	 * @throws \Aimeos\Controller\Jobs\Exception If an error occurs
164
	 */
165
	public function run()
166
	{
167
		$context = $this->context();
168
		$logger = $context->logger();
169
		$location = $this->location();
170
171
		try
172
		{
173
			$logger->info( sprintf( 'Started stock import from "%1$s"', $location ), 'import/csv/stock' );
174
175
			if( !file_exists( $location ) )
176
			{
177
				$msg = sprintf( 'File or directory "%1$s" doesn\'t exist', $location );
178
				throw new \Aimeos\Controller\Jobs\Exception( $msg );
179
			}
180
181
			$files = [];
182
183
			if( is_dir( $location ) )
184
			{
185
				foreach( new \DirectoryIterator( $location ) as $entry )
186
				{
187
					if( strncmp( $entry->getFilename(), 'stock', 5 ) === 0 && $entry->getExtension() === 'csv' ) {
188
						$files[] = $entry->getPathname();
189
					}
190
				}
191
			}
192
			else
193
			{
194
				$files[] = $location;
195
			}
196
197
			sort( $files );
198
			$context->__sleep();
199
200
			$fcn = function( $filepath ) {
201
				$this->import( $filepath );
202
			};
203
204
			foreach( $files as $filepath ) {
205
				$context->process()->start( $fcn, [$filepath] );
206
			}
207
208
			$context->process()->wait();
209
210
			$logger->info( sprintf( 'Finished stock import from "%1$s"', $location ), 'import/csv/stock' );
211
		}
212
		catch( \Exception $e )
213
		{
214
			$logger->error( 'Stock import error: ' . $e->getMessage() . "\n" . $e->getTraceAsString(), 'import/csv/stock' );
215
			$this->mail( 'Stock CSV import error', $e->getMessage() . "\n" . $e->getTraceAsString() );
216
			throw $e;
217
		}
218
	}
219
220
221
	/**
222
	 * Returns the directory for storing imported files
223
	 *
224
	 * @return string Directory for storing imported files
225
	 */
226
	protected function backup() : string
227
	{
228
		/** controller/jobs/stock/import/csv/backup
229
		 * Name of the backup for sucessfully imported files
230
		 *
231
		 * After a CSV file was imported successfully, you can move it to another
232
		 * location, so it won't be imported again and isn't overwritten by the
233
		 * next file that is stored at the same location in the file system.
234
		 *
235
		 * You should use an absolute path to be sure but can be relative path
236
		 * if you absolutely know from where the job will be executed from. The
237
		 * name of the new backup location can contain placeholders understood
238
		 * by the PHP DateTime::format() method (with percent signs prefix) to
239
		 * create dynamic paths, e.g. "backup/%Y-%m-%d" which would create
240
		 * "backup/2000-01-01". For more information about the date() placeholders,
241
		 * please have a look  into the PHP documentation of the
242
		 * {@link https://www.php.net/manual/en/datetime.format.php format() method}.
243
		 *
244
		 * **Note:** If no backup name is configured, the file or directory
245
		 * won't be moved away. Please make also sure that the parent directory
246
		 * and the new directory are writable so the file or directory could be
247
		 * moved.
248
		 *
249
		 * @param integer Name of the backup file, optionally with date/time placeholders
250
		 * @since 2019.04
251
		 * @see controller/jobs/stock/import/csv/location
252
		 * @see controller/jobs/stock/import/csv/max-size
253
		 * @see controller/jobs/stock/import/csv/skip-lines
254
		 */
255
		return (string) $this->context()->config()->get( 'controller/jobs/stock/import/csv/backup' );
256
	}
257
258
259
	/**
260
	 * Executes the job.
261
	 *
262
	 * @param string $filename Absolute path to the file that whould be imported
263
	 */
264
	protected function import( string $filename )
265
	{
266
		$context = $this->context();
267
		$logger = $context->logger();
268
269
		$maxcnt = $this->max();
270
		$skiplines = $this->skip();
271
		$container = $this->getContainer( $filename );
272
273
		$logger->info( sprintf( 'Started stock import from file "%1$s"', $filename ), 'import/csv/stock' );
274
275
		foreach( $container as $content )
276
		{
277
			for( $i = 0; $i < $skiplines; $i++ ) {
278
				$content->next();
279
			}
280
281
			$this->importStocks( $content, $maxcnt );
282
		}
283
284
		$logger->info( sprintf( 'Finished stock import from file "%1$s"', $filename ), 'import/csv/stock' );
285
286
		$container->close();
287
		$this->saveTypes();
288
289
		if( !empty( $backup = $this->backup() ) && @rename( $filename, $backup = \Aimeos\Base\Str::strtime( $backup ) ) === false )
290
		{
291
			$msg = sprintf( 'Unable to move imported file "%1$s" to "%2$s"', $filename, $backup );
292
			throw new \Aimeos\Controller\Jobs\Exception( $msg );
293
		}
294
	}
295
296
297
	/**
298
	 * Opens and returns the container which includes the stock data
299
	 *
300
	 * @param string $location Absolute path to the file
301
	 * @return \Aimeos\MW\Container\Iface Container object
302
	 */
303
	protected function getContainer( string $location ) : \Aimeos\MW\Container\Iface
304
	{
305
		$config = $this->context()->config();
306
307
		/** controller/jobs/stock/import/csv/container/type
308
		 * Nave of the container type to read the data from
309
		 *
310
		 * The container type tells the importer how it should retrieve the data.
311
		 * There are currently two container types that support the necessary
312
		 * CSV content:
313
		 *
314
		 * * File
315
		 * * Zip
316
		 *
317
		 * @param string Container type name
318
		 * @since 2019.04
319
		 * @category User
320
		 * @see controller/jobs/stock/import/csv/location
321
		 * @see controller/jobs/stock/import/csv/container/options
322
		 */
323
		$container = $config->get( 'controller/jobs/stock/import/csv/container/type', 'File' );
324
325
		/** controller/jobs/stock/import/csv/container/options
326
		 * List of file container options for the stock import files
327
		 *
328
		 * Some container/content type allow you to hand over additional settings
329
		 * for configuration. Please have a look at the article about
330
		 * {@link http://aimeos.org/docs/Developers/Utility/Create_and_read_files container/content files}
331
		 * for more information.
332
		 *
333
		 * @param array Associative list of option name/value pairs
334
		 * @since 2019.04
335
		 * @category User
336
		 * @see controller/jobs/stock/import/csv/location
337
		 * @see controller/jobs/stock/import/csv/container/type
338
		 */
339
		$options = $config->get( 'controller/jobs/stock/import/csv/container/options', [] );
340
341
		return \Aimeos\MW\Container\Factory::getContainer( $location, $container, 'CSV', $options );
342
	}
343
344
345
	/**
346
	 * Returns the stock items for the given product IDs and stock types
347
	 *
348
	 * @param array $ids List of product IDs
349
	 * @param array $types List of stock types
350
	 * @return array Multi-dimensional array of code/type/item map
351
	 */
352
	protected function getStockItems( array $ids, array $types ) : array
353
	{
354
		$map = [];
355
		$manager = \Aimeos\MShop::create( $this->context(), 'stock' );
356
357
		$search = $manager->filter()->slice( 0, 10000 );
358
		$search->setConditions( $search->and( [
359
			$search->compare( '==', 'stock.productid', $ids ),
360
			$search->compare( '==', 'stock.type', $types )
361
		] ) );
362
363
		foreach( $manager->search( $search ) as $item ) {
364
			$map[$item->getProductId()][$item->getType()] = $item;
365
		}
366
367
		return $map;
368
	}
369
370
371
	/**
372
	 * Imports the CSV data and creates new stocks or updates existing ones
373
	 *
374
	 * @param \Aimeos\MW\Container\Content\Iface $content Content object
375
	 * @param int $maxcnt Maximum number of stock levels imported at once
376
	 * @return int Number of imported stocks
377
	 */
378
	protected function importStocks( \Aimeos\MW\Container\Content\Iface $content, int $maxcnt ) : int
379
	{
380
		$total = 0;
381
		$context = $this->context();
382
		$manager = \Aimeos\MShop::create( $context, 'stock' );
383
		$prodManager = \Aimeos\MShop::create( $context, 'product' );
384
385
		do
386
		{
387
			$count = 0;
388
			$codes = $data = $types = [];
389
390
			while( $content->valid() && $count < $maxcnt )
391
			{
392
				$row = $content->current();
393
				$content->next();
394
395
				if( $row[0] == '' ) {
396
					continue;
397
				}
398
399
				$type = $this->val( $row, 2, 'default' );
400
				$types[$type] = null;
401
				$codes[] = $row[0];
402
				$row[2] = $type;
403
				$data[] = $row;
404
405
				$count++;
406
			}
407
408
			if( $count === 0 ) {
409
				break;
410
			}
411
412
			$filter = $prodManager->filter()->add( ['product.code' => $codes] )->slice( 0, count( $codes ) );
413
			$products = $prodManager->search( $filter );
414
			$prodMap = $products->col( null, 'product.code' );
415
416
			$map = $this->getStockItems( $products->keys()->all(), array_keys( $types ) );
417
			$items = [];
418
419
			foreach( $data as $entry )
420
			{
421
				$code = $entry[0];
422
				$type = $entry[2];
423
424
				if( ( $product = $prodMap->get( $code ) ) === null ) {
425
					continue;
426
				}
427
428
				$item = $map[$product->getId()][$type] ?? $manager->create();
429
430
				$items[] = $item->setProductId( $product->getId() )->setType( $type )
431
					->setStocklevel( $this->val( $entry, 1 ) )
432
					->setDateBack( $this->val( $entry, 3 ) );
433
434
				if( $item->getStockLevel() === null || $item->getStockLevel() > 0 ) {
435
					$product->setInStock( 1 );
436
				}
437
438
				$this->addType( 'stock/type', 'product', $type );
439
				unset( $map[$code][$type] );
440
			}
441
442
			$prodManager->save( $products );
443
			$manager->save( $items );
444
			unset( $items );
445
446
			$total += $count;
447
		}
448
		while( $count > 0 );
449
450
		return $total;
451
	}
452
453
454
	/**
455
	 * Returns the path to the directory with the CSV file
456
	 *
457
	 * @return string Path to the directory with the CSV file
458
	 */
459
	protected function location() : string
460
	{
461
		/** controller/jobs/stock/import/csv/location
462
		 * File or directory where the content is stored which should be imported
463
		 *
464
		 * You need to configure the CSV file or directory with the CSV files that
465
		 * should be imported. It should be an absolute path to be sure but can be
466
		 * relative path if you absolutely know from where the job will be executed
467
		 * from.
468
		 *
469
		 * @param string Relative path to the CSV files
470
		 * @since 2019.04
471
		 * @see controller/jobs/stock/import/csv/backup
472
		 * @see controller/jobs/stock/import/csv/max-size
473
		 * @see controller/jobs/stock/import/csv/skip-lines
474
		 */
475
		return (string) $this->context()->config()->get( 'controller/jobs/stock/import/csv/location', 'stock' );
476
	}
477
478
479
	/**
480
	 * Returns the maximum number of CSV rows to import at once
481
	 *
482
	 * @return int Maximum number of CSV rows to import at once
483
	 */
484
	protected function max() : int
485
	{
486
		/** controller/jobs/stock/import/csv/max-size
487
		 * Maximum number of CSV rows to import at once
488
		 *
489
		 * It's more efficient to read and import more than one row at a time
490
		 * to speed up the import. Usually, the bigger the chunk that is imported
491
		 * at once, the less time the importer will need. The downside is that
492
		 * the amount of memory required by the import process will increase as
493
		 * well. Therefore, it's a trade-off between memory consumption and
494
		 * import speed.
495
		 *
496
		 * @param integer Number of rows
497
		 * @since 2019.04
498
		 * @see controller/jobs/stock/import/csv/backup
499
		 * @see controller/jobs/stock/import/csv/location
500
		 * @see controller/jobs/stock/import/csv/skip-lines
501
		 */
502
		return (int) $this->context()->config()->get( 'controller/jobs/stock/import/csv/max-size', 1000 );
503
	}
504
505
506
	/**
507
	 * Returns the number of rows skipped in front of each CSV files
508
	 *
509
	 * @return int Number of rows skipped in front of each CSV files
510
	 */
511
	protected function skip() : int
512
	{
513
		/** controller/jobs/stock/import/csv/skip-lines
514
		 * Number of rows skipped in front of each CSV files
515
		 *
516
		 * Some CSV files contain header information describing the content of
517
		 * the column values. These data is for informational purpose only and
518
		 * can't be imported into the database. Using this option, you can
519
		 * define the number of lines that should be left out before the import
520
		 * begins.
521
		 *
522
		 * @param integer Number of rows
523
		 * @since 2019.04
524
		 * @see controller/jobs/stock/import/csv/backup
525
		 * @see controller/jobs/stock/import/csv/location
526
		 * @see controller/jobs/stock/import/csv/max-size
527
		 */
528
		return (int) $this->context()->config()->get( 'controller/jobs/stock/import/csv/skip-lines', 0 );
529
	}
530
}
531