-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #121 from dbarzin/dev
add new command
- Loading branch information
Showing
1 changed file
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
namespace App\Console\Commands; | ||
|
||
use App\Http\Controllers\MeasureImportController; | ||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Facades\Log; | ||
|
||
class ImportFramework extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'deming:import-framework | ||
{filename : the file contaning the framework} | ||
{--clean : remove all other controls}'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Import a framwork from repository'; | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return mixed | ||
*/ | ||
public function handle() | ||
{ | ||
Log::debug('ImportFramework - Start.'); | ||
|
||
$controller = new MeasureImportController(); | ||
|
||
$fileName = $this->argument('filename'); | ||
|
||
if (! file_exists($fileName)) { | ||
Log::error('ImportFramework - file does not exists'); | ||
$this->components->error('File does not exists'); | ||
} else { | ||
Log::debug('ImportFramework - Import ' . $fileName); | ||
|
||
// XLSX | ||
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx(); | ||
$reader->setReadDataOnly(true); | ||
$spreadsheet = $reader->load($fileName); | ||
|
||
$sheet = $spreadsheet->getSheet($spreadsheet->getFirstSheetIndex()); | ||
$data = $sheet->toArray(); | ||
|
||
// create error collection | ||
$errors = Collect(); | ||
|
||
// create controller | ||
$importController = new MeasureImportController(); | ||
|
||
// Import file | ||
if ($importController->canImportFromFile($data, $this->option('clean'), $errors)) { | ||
// Clear database | ||
if ($this->option('clean')) { | ||
$importController->clean(); | ||
$errors->push('Database cleared'); | ||
} | ||
|
||
// Improt data | ||
$importController->importFromFile($data, $errors); | ||
|
||
foreach ($errors as $error) { | ||
Log::info('ImportFramework - ' . $error); | ||
$this->components->info($error); | ||
} | ||
} else { | ||
foreach ($errors as $error) { | ||
Log::error('ImportFramework - ' . $error); | ||
$this->components->error($error); | ||
} | ||
} | ||
} | ||
|
||
Log::debug('ImportFramework - DONE.'); | ||
} | ||
} |