![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/dceprojects.corals.io/Corals/modules/ProjectPlan/Jobs/ |
<?php namespace Corals\Modules\ProjectPlan\Jobs; use Corals\Modules\ProjectPlan\Contracts\ModelImportContract; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\{InteractsWithQueue, SerializesModels}; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\File; use Illuminate\Support\Facades\Validator; use Illuminate\Support\Str; use League\Csv\{Exception as CSVException, Reader, Writer}; class HandleImportFile implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; protected $importLogWriter; protected $importLogFile; protected $importFilePath; protected $importHeaders; protected $model; protected $success_records_count = 0; protected $failed_records_count = 0; protected ModelImportContract $importer; protected $user; /** * HandleBrandsImportFile constructor. * @param $model * @param $importFilePath * @param $headers * @param $user */ public function __construct($model, $importFilePath, $headers, $user) { $this->user = $user; $this->model = $model; $this->importer = $this->getImporter($model); $this->importFilePath = $importFilePath; $this->importHeaders = array_keys($headers); } protected function getImporter($model) { $importerClass = sprintf("\Corals\Modules\ProjectPlan\Import\%sImporter", ucfirst($model)); return new $importerClass(); } /** * @throws CSVException */ public function handle() { $reader = Reader::createFromPath($this->importFilePath, 'r') ->setDelimiter(config('corals.csv_delimiter', ',')) ->setHeaderOffset(0); foreach ($reader->getRecords() as $record) { DB::beginTransaction(); try { $this->handleImportRecord($record); $this->success_records_count++; } catch (\Exception $exception) { $this->failed_records_count++; $this->logRecordException($record, $exception->getMessage()); } DB::commit(); } event('notifications.project_plan.import_status', [ 'model' => $this->model, 'import_file_name' => basename($this->importFilePath), 'import_log_file' => $this->importLogWriter ? HtmlElement('a', ['href' => asset($this->importLogFile), 'target' => '_blank'], basename($this->importLogFile)) : '-', 'success_records_count' => $this->success_records_count, 'failed_records_count' => $this->failed_records_count, ]); } /** * @param $record * @throws \Exception */ protected function handleImportRecord($record) { $record = array_map('trim', $record); $data = $this->importer->getData($record); $rules = $this->importer->getValidationRules(); $validator = Validator::make($data, $rules); if ($validator->fails()) { throw new \Exception(json_encode($validator->errors()->jsonSerialize())); } $data['userId'] = $this->user->id; $this->importer->saveData($data); } protected function logRecordException($record, $message) { if (!$this->importLogWriter) { //we create the CSV into memory $logName = basename($this->importFilePath, '.csv') . Str::random(10); $logBasePath = ucfirst($this->model) . 's/imports'; $this->importLogFile = "$logBasePath/$logName.csv"; if (!File::exists(public_path($logBasePath))) { File::makeDirectory(public_path($logBasePath), 0755, true); } $this->importLogWriter = Writer::createFromPath(public_path($this->importLogFile), 'w+') ->setDelimiter(config('corals.csv_delimiter', ',')); $headers = $this->importHeaders; $headers[] = 'Import Message'; //we insert the CSV header $this->importLogWriter->insertOne($headers); } $record['Import Message'] = $message; $this->importLogWriter->insertOne($record); } }