refactor(webstatement): perbaikan struktur dan penyempurnaan kode pada GenerateBiayaKartuCsvJob

- Mengubah konversi konstanta MAX_EXECUTION_TIME menjadi properti.
- Memindahkan dan merapikan fungsi-fungsi `updateCsvLogStart`, `updateCsvLogSuccess`, dan `updateCsvLogFailed` untuk meningkatkan keterbacaan.
- Mengubah format deklarasi fungsi untuk konsistensi dalam penggunaan tipe data return.
- Memperbaiki logika pengambilan file info pada proses pembuatan dan pengunggahan CSV.
- Menambahkan logging yang lebih terstruktur untuk setiap proses (mulai, sukses, gagal).

Signed-off-by: Daeng Deni Mardaeni <ddeni05@gmail.com>
This commit is contained in:
Daeng Deni Mardaeni
2025-05-11 17:14:08 +07:00
parent 7df50b5141
commit 012d1629c9

View File

@@ -19,26 +19,22 @@
class GenerateBiayaKartuCsvJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
// Changed from const to property
private const MAX_EXECUTION_TIME = 3600; // 1 jam dalam detik
private $csvFilename;
/**
* Periode yang akan diproses (YYYY-MM)
*/
protected $periode;
/**
* ID log sinkronisasi
*/
protected $syncLogId;
/**
* Model log sinkronisasi
*/
protected $syncLog;
private $csvFilename;
/**
* Create a new job instance.
@@ -50,17 +46,6 @@
$this->syncLogId = KartuSyncLog::where('periode', $this->periode)->first();
}
/**
* Get default fees from JenisKartu table
*
* @return array
*/
private function getDefaultFees(): array
{
return JenisKartu::getDefaultFees();
}
/**
* Execute the job.
*/
@@ -70,7 +55,7 @@
set_time_limit(self::MAX_EXECUTION_TIME);
// Load log sinkronisasi
$this->syncLog = KartuSyncLog::findOrFail($this->syncLogId);
$this->syncLog = KartuSyncLog::findOrFail($this->syncLogId->id);
try {
@@ -105,6 +90,21 @@
}
}
/**
* Update log saat pembuatan CSV dimulai
*/
private function updateCsvLogStart()
: void
{
$this->syncLog->update([
'csv_notes' => 'Pembuatan file CSV untuk periode ' . $this->periode . ' dimulai',
'is_csv' => false,
'csv_at' => null,
]);
Log::info('Memulai pembuatan file CSV untuk periode ' . $this->periode);
}
/**
* Generate CSV file with ATM card data and upload to SFTP
*
@@ -172,6 +172,17 @@
->get();
}
/**
* Get default fees from JenisKartu table
*
* @return array
*/
private function getDefaultFees()
: array
{
return JenisKartu::getDefaultFees();
}
/**
* Determine fee for a card based on its type
*
@@ -305,58 +316,11 @@
}
}
/**
* Update log saat pembuatan CSV dimulai
*/
private function updateCsvLogStart(): void
{
$this->syncLog->update([
'csv_notes' => 'Pembuatan file CSV untuk periode ' . $this->periode . ' dimulai',
'is_csv' => false,
'csv_at' => null,
]);
Log::info('Memulai pembuatan file CSV untuk periode ' . $this->periode);
}
/**
* Update log saat pembuatan CSV berhasil
*/
private function updateCsvLogSuccess($result): void
{
// Get file info
$fileInfo = pathinfo($result['localFilePath']);
$fileName = $fileInfo['basename'];
$fileSize = Storage::size($result['localFilePath']);
$this->syncLog->update([
'is_csv' => true,
'csv_at' => Carbon::now(),
'csv_notes' => 'File CSV berhasil dibuat: ' . $fileName . ' (' . $this->formatSize($fileSize) . ')',
'file_path' => storage_path('app/' . $result['localFilePath']),
'file_name' => $fileName,
]);
Log::info('File CSV berhasil dibuat: ' . $result['localFilePath']);
}
/**
* Update log saat pembuatan CSV gagal
*/
private function updateCsvLogFailed($errorMessage): void
{
$this->syncLog->update([
'is_csv' => false,
'csv_notes' => 'Pembuatan file CSV gagal: ' . $errorMessage
]);
Log::error('Pembuatan file CSV gagal: ' . $errorMessage);
}
/**
* Update log saat upload SFTP dimulai
*/
private function updateSftpLogStart(): void
private function updateSftpLogStart()
: void
{
$this->syncLog->update([
'ftp_notes' => 'Upload ke SFTP untuk file periode ' . $this->periode . ' dimulai',
@@ -370,7 +334,8 @@
/**
* Update log saat upload SFTP berhasil
*/
private function updateSftpLogSuccess(): void
private function updateSftpLogSuccess()
: void
{
$this->syncLog->update([
'is_ftp' => true,
@@ -385,7 +350,8 @@
/**
* Update log saat upload SFTP gagal
*/
private function updateSftpLogFailed($errorMessage): void
private function updateSftpLogFailed($errorMessage)
: void
{
$this->syncLog->update([
'is_ftp' => false,
@@ -395,5 +361,38 @@
Log::error('Upload ke SFTP gagal: ' . $errorMessage);
}
/**
* Update log saat pembuatan CSV berhasil
*/
private function updateCsvLogSuccess($result)
: void
{
// Get file info
$fileInfo = pathinfo($result['localFilePath']);
$fileName = $fileInfo['basename'];
$this->syncLog->update([
'is_csv' => true,
'csv_at' => Carbon::now(),
'csv_notes' => 'File CSV berhasil dibuat: ' . $fileName,
'file_path' => $result['localFilePath'],
'file_name' => $fileName,
]);
Log::info('File CSV berhasil dibuat: ' . $result['localFilePath']);
}
/**
* Update log saat pembuatan CSV gagal
*/
private function updateCsvLogFailed($errorMessage)
: void
{
$this->syncLog->update([
'is_csv' => false,
'csv_notes' => 'Pembuatan file CSV gagal: ' . $errorMessage
]);
Log::error('Pembuatan file CSV gagal: ' . $errorMessage);
}
}