حق با شماست. متد getOneCustomTemplateBySuperTypeId همانطور که مشخص است، به ازای هر بار فراخوانی (که داخل لوپ foreach انجام میشود)، منطق مربوط به خواندن متغیرهای محیطی و سپس فراخوانی getOneById را تکرار میکند. اگر getOneById یک کوئری دیتابیس میزند (که احتمالاً همینطور است)، این منجر به مشکل N+1 Query میشود.
یعنی اگر 1000 نوتیفیکیشن آماده ارسال داشته باشیم که هر کدام نیاز به قالب سفارشی دارند، به جای 2 کوئری (یکی برای قالب غذایی و یکی برای غیرغذایی)، 1000 کوئری اضافی برای دریافت قالبها به دیتابیس زده میشود.
راه حل: Pre-fetch کردن (از قبل واکشی کردن) قالبها
با توجه به اینکه فقط دو نوع قالب سفارشی داریم (یکی برای SuperType های غذایی و دیگری برای غیر غذایی)، میتوانیم این دو قالب را قبل از ورود به لوپ یک بار واکشی کنیم و سپس در داخل لوپ از آنها استفاده کنیم.
تغییرات پیشنهادی:
public function getNotificationReadyToSend(): array
{
$notifications = $this->notificationRepository->getNotificationsReadyToSend();
$notificationDateIDs = [];
// --- Start of Optimization ---
// 1. Read environment variables for food super types once
$foodSuperTypeIdList = $this->envVariableReader->getRequiredString(EnvVariable::FOOD_SUPERTYPE_IDS);
$foodSuperTypeIds = explode(',', $foodSuperTypeIdList);
// Convert to integers for proper comparison with (int)$superTypeIds[0]
$foodSuperTypeIds = array_map('intval', $foodSuperTypeIds);
// 2. Determine and fetch the two possible custom templates once
$foodCustomTemplateId = $this->envVariableReader->getRequiredInt(EnvVariable::NOTIFICATION_FOOD_CUSTOM_TEMPLATE_ID);
$nonFoodCustomTemplateId = $this->envVariableReader->getRequiredInt(EnvVariable::NOTIFICATION_NON_FOOD_CUSTOM_TEMPLATE_ID);
$foodCustomTemplate = $this->templateService->getOneById($foodCustomTemplateId);
$nonFoodCustomTemplate = $this->templateService->getOneById($nonFoodCustomTemplateId);
// --- End of Optimization Setup ---
foreach ($notifications as &$notification) {
$userIDs = array_column($this->notificationUserService->getUsersToSendSMS($notification['notification_date_id']), 'userId');
$notification['user_ids'] = $userIDs;
if ($notification['template'] == null) {
$superTypeIds = explode(',', $notification['vendor_super_type_ids']);
// Ensure superTypeIds is not empty and get the first one
$firstSuperTypeId = !empty($superTypeIds) ? (int)$superTypeIds[0] : null;
$customTemplate = null;
if ($firstSuperTypeId !== null) {
// Use the pre-fetched templates based on the super type
if (in_array($firstSuperTypeId, $foodSuperTypeIds)) {
$customTemplate = $foodCustomTemplate;
} else {
$customTemplate = $nonFoodCustomTemplate;
}
}
if ($customTemplate !== null) { // Make sure a custom template was found/assigned
$notification['template'] = $customTemplate->getContent();
$notification['template_key'] = $customTemplate->getKey();
$notification['has_voucher'] = $customTemplate->getHasVoucher();
} else {
// Handle cases where no suitable custom template is found, e.g., log an error or set a default
// For now, it will just leave template, template_key, has_voucher as null if no custom template found
// based on the superTypeId logic.
}
}
$notification['raven_param'] = $this->templateService->getTemplateParams($notification);
$notificationDateIDs[] = $notification['notification_date_id'];
}
$this->notificationDateService->updateNotificationDatesStatus($notificationDateIDs, NotificationDateStatus::RUNNING->value);
return $notifications;
}
// The getOneCustomTemplateBySuperTypeId method can potentially be removed if no other part of the application uses it.
// If it's used elsewhere, consider adding an internal caching mechanism to it, or passing the pre-fetched templates to it.
// For the purpose of optimizing this specific loop, the above change is sufficient.