clrghouz/app/Jobs/EchoareaImport.php

106 lines
2.2 KiB
PHP
Raw Normal View History

2021-08-27 14:10:42 +00:00
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use App\Models\{Domain,Echoarea};
use App\Traits\Import as ImportTrait;
class EchoareaImport implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
use ImportTrait;
2021-09-11 14:07:02 +00:00
protected const LOGKEY = 'JEI';
2021-08-27 14:10:42 +00:00
private const importkey = 'echoarea';
private string $file;
private Domain $do;
private string $prefix;
private bool $delete_file;
private bool $delete_recs;
/**
* Import Echoarea constructor.
*
* @param string $file
* @param Domain $do
* @param string $prefix
* @param bool $delete_recs
* @param bool $delete_file
*/
public function __construct(string $file,Domain $do,string $prefix,bool $delete_recs=FALSE,bool $delete_file=FALSE)
{
$this->file = $file;
$this->do = $do;
$this->prefix = $prefix ?: '';
2021-08-27 14:10:42 +00:00
$this->delete_file = $delete_file;
$this->delete_recs = $delete_recs;
}
public function __get($key): mixed
{
switch ($key) {
case 'subject':
return sprintf('%s-%s',$this->do->name,$this->file);
default:
return NULL;
}
}
2021-08-27 14:10:42 +00:00
/**
* Execute the job.
*
* @return void
* @throws \Exception
*/
public function handle()
{
$fh = fopen($this->file,'r');
2021-08-27 14:10:42 +00:00
$p = $c = 0;
while (! feof($fh)) {
$line = stream_get_line($fh, 0, "\n");
// Remove any embedded CR and BOM
$line = str_replace("\r",'',$line);
$line = preg_replace('/^\x{feff}/u','',$line);
if (! $line)
continue;
$c++;
if (str_contains($line,' '))
list($area,$description) = preg_split('/[\s|\t]+/',$line,2);
else {
$area = $line;
$description = '[No Description]';
2021-08-27 14:10:42 +00:00
}
$o = Echoarea::whereRaw(sprintf("LOWER(name)='%s'",strtolower($area)))->firstOrNew();
$o->name = strtoupper($area);
$o->description = ($this->prefix.' ' ?: '').ucfirst($description);
2021-08-27 14:10:42 +00:00
$o->active = TRUE;
$o->show = TRUE;
2023-09-14 13:42:25 +00:00
$o->security = 9;
2021-08-27 14:10:42 +00:00
$this->do->echoareas()->save($o);
}
fclose($fh);
if ($this->delete_file and $c)
2023-09-14 13:42:25 +00:00
unlink($this->file);
2021-08-27 14:10:42 +00:00
2023-09-14 13:42:25 +00:00
Log::info(sprintf('%s:= Updated %d records',self::LOGKEY,$p));
2021-08-27 14:10:42 +00:00
}
}