You're right - there's a performance issue here. The code is making a database query inside a loop by calling getOneCustomTemplateBySuperTypeId() for each notification without a template.
Here's how you could optimize it:
public function getNotificationReadyToSend(): array
{
$notifications = $this->notificationRepository->getNotificationsReadyToSend();
$notificationDateIDs = [];
$templateCache = []; // Cache for templates by superTypeId
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']);
$superTypeId = $superTypeIds[0];
// Check cache first before making a database call
if (!isset($templateCache[$superTypeId])) {
$templateCache[$superTypeId] = $this->templateService->getOneCustomTemplateBySuperTypeId($superTypeId);
}
$customTemplate = $templateCache[$superTypeId];
$notification['template'] = $customTemplate->getContent();
$notification['template_key'] = $customTemplate->getKey();
$notification['has_voucher'] = $customTemplate->getHasVoucher();
}
$notification['raven_param'] = $this->templateService->getTemplateParams($notification);
$notificationDateIDs[] = $notification['notification_date_id'];
}
$this->notificationDateService->updateNotificationDatesStatus($notificationDateIDs, NotificationDateStatus::RUNNING->value);
return $notifications;
}
Would you like me to explain or break down the code?