2016-05-17 19:25:17 -07:00
< ? php
2021-06-10 13:15:52 -07:00
2016-05-17 19:25:17 -07:00
namespace App\Console\Commands ;
2016-08-29 15:49:32 -07:00
use Illuminate\Console\Command ;
use Symfony\Component\Console\Input\InputArgument ;
use Symfony\Component\Console\Input\InputOption ;
2024-05-29 04:38:15 -07:00
use Illuminate\Support\Facades\Log ;
2016-06-13 21:41:09 -07:00
2021-04-05 21:47:57 -07:00
ini_set ( 'max_execution_time' , env ( 'IMPORT_TIME_LIMIT' , 600 )); //600 seconds = 10 minutes
ini_set ( 'memory_limit' , env ( 'IMPORT_MEMORY_LIMIT' , '500M' ));
2016-06-13 21:41:09 -07:00
2016-05-17 19:25:17 -07:00
/**
* Class ObjectImportCommand
*/
2016-06-22 12:27:41 -07:00
class ObjectImportCommand extends Command
{
/**
* The console command name .
*
* @ var string
*/
protected $name = 'snipeit:import' ;
/**
* The console command description .
*
* @ var string
*/
protected $description = 'Import Items from CSV' ;
/**
* Create a new command instance .
*
* @ return void
*/
public function __construct ()
{
parent :: __construct ();
}
2021-06-10 13:15:52 -07:00
2016-12-26 15:16:42 -08:00
private $bar ;
2021-06-10 13:15:52 -07:00
2016-06-22 12:27:41 -07:00
/**
* Execute the console command .
*
* @ return mixed
*/
2018-07-25 01:28:44 -07:00
public function handle ()
2016-06-22 12:27:41 -07:00
{
$filename = $this -> argument ( 'filename' );
2017-01-05 15:45:12 -08:00
$class = title_case ( $this -> option ( 'item-type' ));
$classString = " App \\ Importer \\ { $class } Importer " ;
2017-01-10 16:19:18 -08:00
$importer = new $classString ( $filename );
$importer -> setCallbacks ([ $this , 'log' ], [ $this , 'progress' ], [ $this , 'errorCallback' ])
-> setUserId ( $this -> option ( 'user_id' ))
-> setUpdating ( $this -> option ( 'update' ))
2018-10-03 00:52:29 -07:00
-> setShouldNotify ( $this -> option ( 'send-welcome' ))
2017-01-10 16:19:18 -08:00
-> setUsernameFormat ( $this -> option ( 'username_format' ));
2020-11-13 12:34:42 -08:00
// This $logFile/useFiles() bit is currently broken, so commenting it out for now
// $logFile = $this->option('logfile');
2024-05-29 04:38:15 -07:00
// Log::useFiles($logFile);
2017-09-29 17:31:39 -07:00
$this -> comment ( '======= Importing Items from ' . $filename . ' =========' );
2016-12-26 15:16:42 -08:00
$importer -> import ();
2016-05-17 19:25:17 -07:00
2016-12-26 15:16:42 -08:00
$this -> bar = null ;
2016-06-22 12:27:41 -07:00
2021-06-10 13:15:52 -07:00
if ( ! empty ( $this -> errors )) {
$this -> comment ( 'The following Errors were encountered.' );
2016-12-26 15:16:42 -08:00
foreach ( $this -> errors as $asset => $error ) {
2021-06-10 13:15:52 -07:00
$this -> comment ( 'Error: Item: ' . $asset . ' failed validation: ' . json_encode ( $error ));
2016-06-22 12:27:41 -07:00
}
2016-12-26 15:16:42 -08:00
} else {
2021-06-10 13:15:52 -07:00
$this -> comment ( 'All Items imported successfully!' );
2016-06-22 12:27:41 -07:00
}
2021-06-10 13:15:52 -07:00
$this -> comment ( '' );
2016-12-26 15:16:42 -08:00
}
2016-05-26 19:29:29 -07:00
2016-12-26 15:16:42 -08:00
public function errorCallback ( $item , $field , $errorString )
{
$this -> errors [ $item -> name ][ $field ] = $errorString ;
}
2021-06-10 13:15:52 -07:00
2016-12-26 15:16:42 -08:00
public function progress ( $count )
{
2021-06-10 13:15:52 -07:00
if ( ! $this -> bar ) {
2016-12-26 15:16:42 -08:00
$this -> bar = $this -> output -> createProgressBar ( $count );
}
2021-06-10 13:15:52 -07:00
static $index = 0 ;
2016-12-26 15:16:42 -08:00
$index ++ ;
2016-12-29 14:02:18 -08:00
if ( $index < $count ) {
2016-12-26 15:16:42 -08:00
$this -> bar -> advance ();
2016-06-22 12:27:41 -07:00
} else {
2016-12-26 15:16:42 -08:00
$this -> bar -> finish ();
2016-06-22 12:27:41 -07:00
}
}
2021-06-10 13:15:52 -07:00
2016-06-22 12:27:41 -07:00
// Tracks the current item for error messages
2016-10-25 19:51:13 -07:00
private $updating ;
2016-06-22 12:27:41 -07:00
// An array of errors encountered while parsing
private $errors ;
/**
* Log a message to file , configurable by the -- log - file parameter .
* If a warning message is passed , we ' ll spit it to the console as well .
*
* @ author Daniel Melzter
* @ since 3.0
* @ param string $string
* @ param string $level
2021-06-10 13:15:52 -07:00
*/
2016-12-26 15:16:42 -08:00
public function log ( $string , $level = 'info' )
2016-06-22 12:27:41 -07:00
{
if ( $level === 'warning' ) {
2024-05-29 04:38:15 -07:00
Log :: warning ( $string );
2016-06-22 12:27:41 -07:00
$this -> comment ( $string );
} else {
2024-05-29 04:38:15 -07:00
Log :: Info ( $string );
2016-06-22 12:27:41 -07:00
if ( $this -> option ( 'verbose' )) {
$this -> comment ( $string );
}
}
}
2021-06-10 13:15:52 -07:00
2016-06-22 12:27:41 -07:00
/**
* Get the console command arguments .
*
* @ author Daniel Melzter
* @ since 3.0
* @ return array
*/
protected function getArguments ()
{
2021-06-10 13:15:52 -07:00
return [
[ 'filename' , InputArgument :: REQUIRED , 'File for the CSV import.' ],
];
2016-06-22 12:27:41 -07:00
}
/**
* Get the console command options .
*
* @ author Daniel Melzter
* @ since 3.0
* @ return array
*/
protected function getOptions ()
{
2021-06-10 13:15:52 -07:00
return [
[ 'email_format' , null , InputOption :: VALUE_REQUIRED , 'The format of the email addresses that should be generated. Options are firstname.lastname, firstname, filastname' , null ],
[ 'username_format' , null , InputOption :: VALUE_REQUIRED , 'The format of the username that should be generated. Options are firstname.lastname, firstname, filastname, email' , null ],
[ 'logfile' , null , InputOption :: VALUE_REQUIRED , 'The path to log output to. storage/logs/importer.log by default' , storage_path ( 'logs/importer.log' )],
[ 'item-type' , null , InputOption :: VALUE_REQUIRED , 'Item Type To import. Valid Options are Asset, Consumable, Accessory, License, or User' , 'Asset' ],
[ 'web-importer' , null , InputOption :: VALUE_NONE , 'Internal: packages output for use with the web importer' ],
[ 'user_id' , null , InputOption :: VALUE_REQUIRED , 'ID of user creating items' , 1 ],
[ 'update' , null , InputOption :: VALUE_NONE , 'If a matching item is found, update item information' ],
[ 'send-welcome' , null , InputOption :: VALUE_NONE , 'Whether to send a welcome email to any new users that are created.' ],
];
2016-06-22 12:27:41 -07:00
}
2016-05-31 17:00:11 -07:00
}