init
This commit is contained in:
84
app/Console/Commands/ParseEntries.php
Normal file
84
app/Console/Commands/ParseEntries.php
Normal file
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Service\Service;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class ParseEntries extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'app:parse-entries {file}';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Parse entries from json file';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$filePath = $this->argument('file');
|
||||
if(!file_exists($filePath)) {
|
||||
$this->error('File not found');
|
||||
return;
|
||||
}
|
||||
$json = file_get_contents($filePath);
|
||||
$data = json_decode($json, true);
|
||||
|
||||
if (json_last_error() !== JSON_ERROR_NONE) {
|
||||
$this->error('Invalid JSON file');
|
||||
Log::channel('parser')->error('Invalid JSON file '. $filePath);
|
||||
return;
|
||||
}
|
||||
$this->info('File loaded');
|
||||
|
||||
$service = new Service();
|
||||
$successCount = 0;
|
||||
$failCount = 0;
|
||||
$this->line('Start parsing...');
|
||||
Log::channel('parser')->info('Start parsing file ' . $filePath);
|
||||
|
||||
foreach ($data as $entry) {
|
||||
try {
|
||||
if($service->processEntry($entry)) $successCount++; else $failCount++;
|
||||
Log::channel('parser')->info('Processed entry ' . $entry['number']);
|
||||
} catch (\Exception $e) {
|
||||
$failCount++;
|
||||
$number = array_key_exists('number', $entry) ? $entry['number'] : '';
|
||||
$message = 'Error processing entry ' . $number . ': ' . $e->getMessage();
|
||||
$this->error($message);
|
||||
Log::channel('parser')->error('Error processing entry ', [
|
||||
'number' => $number,
|
||||
'error' => $e->getMessage()
|
||||
]);
|
||||
}
|
||||
}
|
||||
$this->line('Parsing finished.');
|
||||
$this->info('Total entries: ' . ($successCount + $failCount));
|
||||
$this->info('Success entries: ' . $successCount);
|
||||
$this->info('Total inspections: '. $service->getInspectionsCount());
|
||||
$this->info('Total failure reports: '. $service->getFailureReportsCount());
|
||||
$this->info('Fail entries: ' . $failCount);
|
||||
Log::channel('parser')->info('Parsing finished. Total entries: ' . ($successCount + $failCount));
|
||||
Log::channel('parser')->info('Success entries: ' . $successCount);
|
||||
Log::channel('parser')->info('Total inspections: '. $service->getInspectionsCount());
|
||||
Log::channel('parser')->info('Total failure reports: '. $service->getFailureReportsCount());
|
||||
Log::channel('parser')->info('Fail entries: ' . $failCount);
|
||||
$path = base_path('./output');
|
||||
File::ensureDirectoryExists($path);
|
||||
File::put($path.'/failureReports.json', json_encode($service->getFailureReports(), JSON_PRETTY_PRINT| JSON_UNESCAPED_UNICODE));
|
||||
File::put($path.'/inspections.json', json_encode($service->getInspections(), JSON_PRETTY_PRINT| JSON_UNESCAPED_UNICODE));
|
||||
File::put($path.'/unprocessable.json', json_encode($service->getUnprocessable(), JSON_PRETTY_PRINT| JSON_UNESCAPED_UNICODE));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user