50 lines
1.1 KiB
PHP
50 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Konfigurasi;
|
|
|
|
use App\Models\Konfigurasi\LogAktivitas;
|
|
|
|
class BufferLogAktivitas
|
|
{
|
|
private bool $aktif = false;
|
|
|
|
private array $antrean = [];
|
|
|
|
public function mulai(): void
|
|
{
|
|
$this->aktif = true;
|
|
}
|
|
|
|
public function aktif(): bool
|
|
{
|
|
return $this->aktif;
|
|
}
|
|
|
|
private const KOLOM = [
|
|
'log_name', 'description', 'subject_type', 'subject_id', 'event',
|
|
'causer_type', 'causer_id', 'properties', 'batch_uuid', 'created_at', 'updated_at',
|
|
];
|
|
|
|
// Samakan kolom tiap baris supaya bisa di-insert sekaligus
|
|
public function tambah(array $baris): void
|
|
{
|
|
$this->antrean[] = array_merge(array_fill_keys(self::KOLOM, null), array_intersect_key($baris, array_flip(self::KOLOM)));
|
|
}
|
|
|
|
// Simpan semua log dalam satu insert
|
|
public function simpan(): void
|
|
{
|
|
foreach (array_chunk($this->antrean, 500) as $potongan) {
|
|
LogAktivitas::query()->insert($potongan);
|
|
}
|
|
|
|
$this->buang();
|
|
}
|
|
|
|
public function buang(): void
|
|
{
|
|
$this->antrean = [];
|
|
$this->aktif = false;
|
|
}
|
|
}
|