Fix seed imports
This commit is contained in:
parent
b55767426f
commit
da39d9177e
|
@ -76,7 +76,16 @@ class EggImporterService
|
||||||
public function handle(UploadedFile $file, int $nest): Egg
|
public function handle(UploadedFile $file, int $nest): Egg
|
||||||
{
|
{
|
||||||
if ($file->getError() !== UPLOAD_ERR_OK || ! $file->isFile()) {
|
if ($file->getError() !== UPLOAD_ERR_OK || ! $file->isFile()) {
|
||||||
throw new InvalidFileUploadException(trans('exceptions.nest.importer.file_error'));
|
throw new InvalidFileUploadException(
|
||||||
|
sprintf(
|
||||||
|
'The selected file ["%s"] was not in a valid format to import. (is_file: %s is_valid: %s err_code: %s err: %s)',
|
||||||
|
$file->getFilename(),
|
||||||
|
$file->isFile() ? 'true' : 'false',
|
||||||
|
$file->isValid() ? 'true' : 'false',
|
||||||
|
$file->getError(),
|
||||||
|
$file->getErrorMessage()
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
$parsed = json_decode($file->openFile()->fread($file->getSize()));
|
$parsed = json_decode($file->openFile()->fread($file->getSize()));
|
||||||
|
|
|
@ -57,7 +57,16 @@ class EggUpdateImporterService
|
||||||
public function handle(int $egg, UploadedFile $file)
|
public function handle(int $egg, UploadedFile $file)
|
||||||
{
|
{
|
||||||
if ($file->getError() !== UPLOAD_ERR_OK || ! $file->isFile()) {
|
if ($file->getError() !== UPLOAD_ERR_OK || ! $file->isFile()) {
|
||||||
throw new InvalidFileUploadException(trans('exceptions.nest.importer.file_error'));
|
throw new InvalidFileUploadException(
|
||||||
|
sprintf(
|
||||||
|
'The selected file ["%s"] was not in a valid format to import. (is_file: %s is_valid: %s err_code: %s err: %s)',
|
||||||
|
$file->getFilename(),
|
||||||
|
$file->isFile() ? 'true' : 'false',
|
||||||
|
$file->isValid() ? 'true' : 'false',
|
||||||
|
$file->getError(),
|
||||||
|
$file->getErrorMessage()
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
$parsed = json_decode($file->openFile()->fread($file->getSize()));
|
$parsed = json_decode($file->openFile()->fread($file->getSize()));
|
||||||
|
|
|
@ -112,14 +112,15 @@ class EggSeeder extends Seeder
|
||||||
$files = $this->filesystem->allFiles(database_path('seeds/eggs/' . kebab_case($nest->name)));
|
$files = $this->filesystem->allFiles(database_path('seeds/eggs/' . kebab_case($nest->name)));
|
||||||
|
|
||||||
$this->command->alert('Updating Eggs for Nest: ' . $nest->name);
|
$this->command->alert('Updating Eggs for Nest: ' . $nest->name);
|
||||||
collect($files)->each(function ($file) use ($nest) {
|
Collection::make($files)->each(function ($file) use ($nest) {
|
||||||
/* @var \Symfony\Component\Finder\SplFileInfo $file */
|
/* @var \Symfony\Component\Finder\SplFileInfo $file */
|
||||||
$decoded = json_decode($file->getContents());
|
$decoded = json_decode($file->getContents());
|
||||||
if (json_last_error() !== JSON_ERROR_NONE) {
|
if (json_last_error() !== JSON_ERROR_NONE) {
|
||||||
return $this->command->error('JSON decode exception for ' . $file->getFilename() . ': ' . json_last_error_msg());
|
$this->command->error('JSON decode exception for ' . $file->getFilename() . ': ' . json_last_error_msg());
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$file = new UploadedFile($file->getPathname(), $file->getFilename(), 'application/json', $file->getSize());
|
$file = new UploadedFile($file->getPathname(), $file->getFilename(), 'application/json');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$egg = $this->repository->setColumns('id')->findFirstWhere([
|
$egg = $this->repository->setColumns('id')->findFirstWhere([
|
||||||
|
@ -130,11 +131,11 @@ class EggSeeder extends Seeder
|
||||||
|
|
||||||
$this->updateImporterService->handle($egg->id, $file);
|
$this->updateImporterService->handle($egg->id, $file);
|
||||||
|
|
||||||
return $this->command->info('Updated ' . $decoded->name);
|
$this->command->info('Updated ' . $decoded->name);
|
||||||
} catch (RecordNotFoundException $exception) {
|
} catch (RecordNotFoundException $exception) {
|
||||||
$this->importerService->handle($file, $nest->id);
|
$this->importerService->handle($file, $nest->id);
|
||||||
|
|
||||||
return $this->command->comment('Created ' . $decoded->name);
|
$this->command->comment('Created ' . $decoded->name);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue