İlk temizlik tamamlandı bir önceki projeden
This commit is contained in:
@@ -0,0 +1,137 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\LineListTriggers\Base;
|
||||
|
||||
use App\Services\LineListTriggers\Contracts\LineListTriggerInterface;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
* Base class for all LineList triggers
|
||||
*
|
||||
* Provides common functionality like timing, logging, and shouldRun logic
|
||||
*/
|
||||
abstract class BaseLineListTrigger implements LineListTriggerInterface
|
||||
{
|
||||
/**
|
||||
* @var float Start time for performance tracking
|
||||
*/
|
||||
protected $startTime;
|
||||
|
||||
/**
|
||||
* @var array Execution context
|
||||
*/
|
||||
protected $context = [];
|
||||
|
||||
/**
|
||||
* Get trigger name - must be implemented by child classes
|
||||
*/
|
||||
abstract public function getName(): string;
|
||||
|
||||
/**
|
||||
* Get dependent fields - must be implemented by child classes
|
||||
*/
|
||||
abstract public function getDependentFields(): array;
|
||||
|
||||
/**
|
||||
* Process trigger logic - must be implemented by child classes
|
||||
*
|
||||
* @param object $lineListData Current line list data
|
||||
* @param object|null $beforeData Previous line list data
|
||||
* @param array $context Execution context
|
||||
* @return array Result data
|
||||
*/
|
||||
abstract protected function process($lineListData, $beforeData, array $context): array;
|
||||
|
||||
/**
|
||||
* Execute the trigger with logging and error handling
|
||||
*
|
||||
* @param object $lineListData Current line list data
|
||||
* @param object|null $beforeData Previous line list data
|
||||
* @param array $context Execution context
|
||||
* @return array Result data
|
||||
* @throws \Throwable
|
||||
*/
|
||||
public function execute($lineListData, $beforeData = null, array $context = []): array
|
||||
{
|
||||
$this->startTime = microtime(true);
|
||||
$this->context = $context;
|
||||
|
||||
Log::info("LineList Trigger {$this->getName()} - STARTED", [
|
||||
'line_list_id' => $lineListData->id,
|
||||
'line_no' => $lineListData->line_no ?? 'NULL',
|
||||
'process' => $this->getName()
|
||||
]);
|
||||
|
||||
try {
|
||||
$result = $this->process($lineListData, $beforeData, $context);
|
||||
$this->logCompletion($lineListData->id, 'success', $result);
|
||||
return $result;
|
||||
} catch (\Throwable $th) {
|
||||
$this->logCompletion($lineListData->id, 'error', [
|
||||
'error' => $th->getMessage(),
|
||||
'file' => $th->getFile(),
|
||||
'line' => $th->getLine()
|
||||
]);
|
||||
throw $th;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if trigger should run based on changed fields
|
||||
*
|
||||
* @param array $changedFields List of changed field names
|
||||
* @param bool $isNewRecord Whether this is a new record
|
||||
* @return bool
|
||||
*/
|
||||
public function shouldRun(array $changedFields, bool $isNewRecord): bool
|
||||
{
|
||||
if ($isNewRecord) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$dependentFields = $this->getDependentFields();
|
||||
|
||||
// If no dependent fields defined, always run
|
||||
if (empty($dependentFields)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Check if any changed field is in dependent fields
|
||||
return !empty(array_intersect($changedFields, $dependentFields));
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if trigger is async (default: false)
|
||||
* Override in child classes if needed
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isAsync(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Log trigger completion with performance metrics
|
||||
*
|
||||
* @param int $lineListId Line list ID
|
||||
* @param string $status Execution status (success/error)
|
||||
* @param array $additionalData Additional log data
|
||||
*/
|
||||
protected function logCompletion($lineListId, string $status, array $additionalData = [])
|
||||
{
|
||||
$duration = round((microtime(true) - $this->startTime) * 1000, 2);
|
||||
|
||||
$logLevel = $status === 'error' ? 'error' : 'info';
|
||||
|
||||
Log::$logLevel("LineList Trigger {$this->getName()} - COMPLETED", array_merge([
|
||||
'line_list_id' => $lineListId,
|
||||
'process' => $this->getName(),
|
||||
'status' => $status,
|
||||
'duration_ms' => $duration,
|
||||
'duration_sec' => round($duration / 1000, 3)
|
||||
], $additionalData));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user