getDisk(); $this->log('Démarrage du traitement'); $files = $disk->files(); $ocbBreeders = OCBBreeder::all(); $progress = 1; $total = count(array_filter($files, function ($filename) { if ($this->fileHasToBeIgnored($filename)) { return false; } return true; })); foreach ($files as $filename) { if ($this->fileHasToBeIgnored($filename)) { continue; } $this->log(sprintf(' traitement %d sur %s ', $progress++, $total)); /** @var string $data */ $data = $disk->get($filename); //load the CSV document from a file path $csv = Reader::createFromString($data); $csv->setDelimiter(';'); $csv->setHeaderOffset(0); $header = $csv->getHeader(); $records = $csv->getRecords(); try { $this->assertHeaderIsAsExpected($header); foreach ($records as $record) { $noElev = $record['No Eleveur']; $inuav = trim(substr($noElev, strlen($noElev) - 7, strlen($noElev))); // Check if inuav exists // attemps to fix weird issue on ocb waiting list if ( !$ocbBreeders->filter(function (OCBBreeder $breeder) use ($inuav) { return in_array($inuav, $breeder->inuavs); })->count() ) { $iWL = OCBWaitingList::where('inuav_code', $inuav)->firstOrNew(); $iWL->inuav_code = $inuav; $NP = explode(' ', $record['Nom Prenom Eleveur'], 2); // try to find more info with xml_breeders $xmlBreeder = XmlBreeder::where('inuav_code', $inuav)->first(); $iWL->fill([ 'date_mep' => Carbon::createFromFormat('d/m/Y', $record['Date mise en place']) ->toDateTimeString(), 'last_name' => $NP[0], 'first_name' => $NP[1], 'cp' => $xmlBreeder->postal_code ?? '', 'city' => $xmlBreeder->city ?? '', 'inuav' => $inuav ]); $iWL->save(); } } } catch (ErrorException | TypeError $e) { $this->log( sprintf( 'File %s could not be parsed => %s on line %d', $filename, $e->getMessage(), $e->getLine() ), LogType::critical() ); continue; // ignore the file } catch (AbortCsvImportException $e) { $this->log( sprintf( 'File %s could not be parsed => %s on line %d', $filename, $e->getMessage(), $e->getLine() ), LogType::error() ); continue; // ignore the file } catch (QueryException $e) { // this happens because of concurent jobs $this->log( sprintf( 'xmlBreeder %s could not be saved (already exists)', $filename, $e->getMessage(), $e->getLine() ), LogType::debug() ); continue; // ignore the file } $this->moveToArchives($filename); } // Clear old archives $this->clearOldArchives(); } protected function assertHeaderIsAsExpected(array $header) { if ($this->expectedArray !== $header) { throw new AbortRowImportException(trans('CSV Header does not match expected')); } } /** * Is the file to be ignored * * @param string $filename * @return boolean */ protected function fileHasToBeIgnored(string $filename): bool { return in_array($filename, $this->ignoreFiles) or !$this->checkExtension($filename, 'csv'); } }