old-hotel-new/app/Services/Admin/ImportService.php

48 lines
1.3 KiB
PHP

<?php
namespace App\Services\Admin;
use App\Models\ImportJob;
use App\Models\ImportJobLog;
use Illuminate\Http\File;
class ImportService
{
protected $driver;
public function import(ImportJob $job)
{
if ($job->status == 1) {
$this->driver = new $job->type();
if(strpos($job->file, 'http') !== false) {
$res = $this->driver->readFileByUrl($job->file);
}else{
$file = new File(public_path('storage/'.$job->file));
$res = $this->driver->readFile($file);
}
if ($res) {
$job->update([
'status'=>2,
'success' => $res['success']??0,
'fails'=> $res['fails']??0,
]);
}
if (isset($res['errors']) && count($res['errors']) > 0) {
$this->createErrorLogs($job, $res['errors']);
}
}
}
public function createErrorLogs(ImportJob $job, array $errors)
{
ImportJobLog::insert(array_map(function ($value) use ($job) {
return array_merge($value, [
'job_id'=>$job->id,
'created_at' => now(),
'updated_at' => now(),
]);
}, $errors));
}
}