151 lines
4.6 KiB
PHP
151 lines
4.6 KiB
PHP
<?php
|
||
/**
|
||
* WeldLog Save Trigger - Refactored Version
|
||
*
|
||
* This file is the entry point for all weld log save triggers.
|
||
* All trigger logic has been extracted to separate classes in:
|
||
* app/Services/WeldLogTriggers/Triggers/
|
||
*
|
||
* The trigger system uses:
|
||
* - WeldLogTriggerInterface: Contract for all triggers
|
||
* - BaseTrigger: Base class with common functionality
|
||
* - WeldLogTriggerRegistry: Central registry for all triggers
|
||
* - WeldLogTriggerManager: Orchestrates trigger execution
|
||
*
|
||
* @see app/Services/WeldLogTriggers/
|
||
*/
|
||
|
||
use App\Services\WeldLogTriggers\WeldLogTriggerManager;
|
||
use App\Services\WeldLogTriggers\WeldLogTriggerRegistry;
|
||
use Illuminate\Support\Facades\Log;
|
||
|
||
// Memory and execution settings
|
||
ini_set('memory_limit', '2G');
|
||
ini_set('max_execution_time', 600); // 10 minutes
|
||
|
||
// Get weld log ID from request
|
||
$id = $request['key'];
|
||
|
||
// Fetch current weld log data
|
||
$data = db($tableName)->where("id", $id)->first();
|
||
if(is_null($data)) {
|
||
$data = db($tableName)->where($id)->first();
|
||
}
|
||
|
||
// Safety check - ensure we have data
|
||
if(is_null($data)) {
|
||
Log::error("WeldLog not found for trigger execution", [
|
||
'weld_log_id' => $id,
|
||
'table_name' => $tableName
|
||
]);
|
||
return;
|
||
}
|
||
|
||
// Detect if this is a new record
|
||
// beforeData comes from the Job - no need for additional checks
|
||
$isNewRecord = false;
|
||
if (is_null($beforeData)) {
|
||
// Batch Excel job'dan changedColumns geliyorsa bu yeni kayıt DEĞİL, update'tir
|
||
if (!empty($changedColumns)) {
|
||
$isNewRecord = false;
|
||
Log::info("WeldLog batch update detected (changedColumns present, beforeData null)", [
|
||
'weld_log_id' => $id,
|
||
'reason' => 'batch_excel_update'
|
||
]);
|
||
} else {
|
||
$isNewRecord = true;
|
||
Log::info("WeldLog new record detected", [
|
||
'weld_log_id' => $id,
|
||
'reason' => 'beforeData_is_null'
|
||
]);
|
||
}
|
||
} else {
|
||
// Check if all fields match - could be a new record
|
||
$allFieldsMatch = true;
|
||
$dataArray = (array) $data;
|
||
foreach ($dataArray as $key => $value) {
|
||
if (isset($beforeData->$key) && $beforeData->$key != $value) {
|
||
$allFieldsMatch = false;
|
||
break;
|
||
}
|
||
}
|
||
|
||
if ($allFieldsMatch) {
|
||
$isNewRecord = true;
|
||
Log::info("WeldLog new record detected", [
|
||
'weld_log_id' => $id,
|
||
'reason' => 'all_fields_match'
|
||
]);
|
||
}
|
||
}
|
||
|
||
// Detect changed fields
|
||
// Batch Excel job'dan changedColumns geliyorsa onu kullan (beforeData batch'te yanlış/null)
|
||
if (!empty($changedColumns)) {
|
||
$changedFields = $changedColumns;
|
||
// changedColumns varsa bu bir update'tir, tüm trigger'ları çalıştırma
|
||
if ($isNewRecord && !is_null($beforeData)) {
|
||
$isNewRecord = false;
|
||
}
|
||
Log::info("WeldLog using changedColumns from batch Excel", [
|
||
'weld_log_id' => $id,
|
||
'changed_columns_count' => count($changedColumns),
|
||
'changed_columns' => $changedColumns
|
||
]);
|
||
} else {
|
||
$changedFields = detectChangedFields($data, $beforeData);
|
||
}
|
||
|
||
Log::info("WeldLog changed fields detected", [
|
||
'weld_log_id' => $id,
|
||
'changed_fields' => $changedFields,
|
||
'changed_fields_count' => count($changedFields),
|
||
'is_new_record' => $isNewRecord,
|
||
'source' => !empty($changedColumns) ? 'batch_excel_columns' : 'detectChangedFields',
|
||
'action' => $action ?? 'unknown'
|
||
]);
|
||
|
||
// Initialize trigger system
|
||
$registry = new WeldLogTriggerRegistry();
|
||
$manager = new WeldLogTriggerManager($registry);
|
||
|
||
// Execute all triggers
|
||
try {
|
||
$results = $manager->executeTriggers(
|
||
$data,
|
||
$beforeData,
|
||
$changedFields,
|
||
$isNewRecord,
|
||
$action ?? null
|
||
);
|
||
|
||
Log::info("WeldLog triggers execution completed", [
|
||
'weld_log_id' => $id,
|
||
'results_summary' => array_map(function($result) {
|
||
if (isset($result['skipped']) && $result['skipped']) {
|
||
return 'skipped';
|
||
} elseif (isset($result['executed']) && $result['executed']) {
|
||
return 'executed';
|
||
} elseif (isset($result['queued']) && $result['queued']) {
|
||
return 'queued';
|
||
} elseif (isset($result['error'])) {
|
||
return 'failed';
|
||
}
|
||
return 'unknown';
|
||
}, $results)
|
||
]);
|
||
|
||
} catch (\Throwable $th) {
|
||
Log::error("WeldLog triggers execution failed with critical error", [
|
||
'weld_log_id' => $id,
|
||
'error' => $th->getMessage(),
|
||
'file' => $th->getFile(),
|
||
'line' => $th->getLine(),
|
||
'trace' => $th->getTraceAsString()
|
||
]);
|
||
|
||
// Re-throw to ensure error is visible
|
||
throw $th;
|
||
}
|
||
|