Skip to content

Commit

Permalink
refactor: use vars for repeated calls
Browse files Browse the repository at this point in the history
Signed-off-by: Git'Fellow <12234510+solracsf@users.noreply.github.com>
  • Loading branch information
solracsf authored Feb 11, 2025
1 parent b98345d commit 0d6a47b
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions lib/MailNotifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public function sendEmails(int $batchSize, int $sendTime): void {
return;
}

$userIds = array_map(static fn (Settings $settings) => $settings->getUserId(), $userSettings);
$userId = $settings->getUserId();

Check failure on line 65 in lib/MailNotifications.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

UndefinedVariable

lib/MailNotifications.php:65:13: UndefinedVariable: Cannot find referenced variable $settings (see https://psalm.dev/024)
$userIds = array_map(static fn (Settings $settings) => $userId, $userSettings);

// Batch-read settings
$fallbackTimeZone = date_default_timezone_get();
Expand All @@ -81,7 +82,7 @@ public function sendEmails(int $batchSize, int $sendTime): void {
}

foreach ($userSettings as $settings) {
if (isset($userEnabled[$settings->getUserId()]) && $userEnabled[$settings->getUserId()] === 'false') {
if (isset($userEnabled[$settings->getUserId()]) && $userEnabled[$userId] === 'false') {
// User is disabled, skip sending the email for them
if ($settings->getNextSendTime() <= $sendTime) {
$settings->setNextSendTime(
Expand All @@ -93,11 +94,11 @@ public function sendEmails(int $batchSize, int $sendTime): void {
}

// Get the settings for this particular user, then check if we have notifications to email them
$languageCode = $userLanguages[$settings->getUserId()] ?? $fallbackLang;
$timezone = $userTimezones[$settings->getUserId()] ?? $fallbackTimeZone;
$languageCode = $userLanguages[$userId] ?? $fallbackLang;
$timezone = $userTimezones[$userId] ?? $fallbackTimeZone;

/** @var array<int, INotification> $notifications */
$notifications = $this->handler->getAfterId($settings->getLastSendId(), $settings->getUserId());
$notifications = $this->handler->getAfterId($settings->getLastSendId(), $userId);
if (!empty($notifications)) {
$oldestNotification = end($notifications);
$shouldSendAfter = $oldestNotification->getDateTime()->getTimestamp() + $settings->getBatchTime();
Expand Down Expand Up @@ -185,30 +186,33 @@ protected function prepareEmailMessage(string $uid, array $notifications, string
}

$userEmailAddress = $user->getEMailAddress();

if (empty($userEmailAddress)) {
return null;
}

// Prepare our email template
$l10n = $this->l10nFactory->get('notifications', $language);

$userDisplayName = $user->getDisplayName();
$absoluteUrl = $this->urlGenerator->getAbsoluteURL('/');
$instanceName = $this->defaults->getName();

$template = $this->mailer->createEMailTemplate('notifications.EmailNotification', [
'displayname' => $user->getDisplayName(),
'url' => $this->urlGenerator->getAbsoluteURL('/')
'displayname' => $userDisplayName,
'url' => $absoluteUrl
]);

// Prepare email header
$template->addHeader();
$template->addHeading($l10n->t('Hello %s', [$user->getDisplayName()]), $l10n->t('Hello %s,', [$user->getDisplayName()]));
$template->addHeading($l10n->t('Hello %s', [$userDisplayName]), $l10n->t('Hello %s,', [$userDisplayName]));

// Prepare email subject and body mentioning amount of notifications
$homeLink = '<a href="' . $this->urlGenerator->getAbsoluteURL('/') . '">' . htmlspecialchars($this->defaults->getName()) . '</a>';
$homeLink = '<a href="' . $this->urlGenerator->getAbsoluteURL('/') . '">' . htmlspecialchars($instanceName) . '</a>';
$notificationsCount = count($notifications);
$template->setSubject($l10n->n('New notification for %s', '%n new notifications for %s', $notificationsCount, [$this->defaults->getName()]));
$template->setSubject($l10n->n('New notification for %s', '%n new notifications for %s', $notificationsCount, [$instanceName]));
$template->addBodyText(
$l10n->n('You have a new notification for %s', 'You have %n new notifications for %s', $notificationsCount, [$homeLink]),
$l10n->n('You have a new notification for %s', 'You have %n new notifications for %s', $notificationsCount, [$this->urlGenerator->getAbsoluteURL('/')])
$l10n->n('You have a new notification for %s', 'You have %n new notifications for %s', $notificationsCount, [$absoluteUrl])
);

// Prepare email body with the content of missed notifications
Expand Down Expand Up @@ -246,17 +250,18 @@ protected function prepareEmailMessage(string $uid, array $notifications, string
}

// Prepare email footer
$linkToPersonalSettings = $this->urlGenerator->linkToRouteAbsolute('settings.PersonalSettings.index', ['section' => 'notifications']);
$template->addBodyText(
$l10n->t('You can change the frequency of these emails or disable them in the <a href="%s">settings</a>.', $this->urlGenerator->linkToRouteAbsolute('settings.PersonalSettings.index', ['section' => 'notifications'])),
$l10n->t('You can change the frequency of these emails or disable them in the settings: %s', $this->urlGenerator->linkToRouteAbsolute('settings.PersonalSettings.index', ['section' => 'notifications']))
$l10n->t('You can change the frequency of these emails or disable them in the <a href="%s">settings</a>.', $linkToPersonalSettings),
$l10n->t('You can change the frequency of these emails or disable them in the settings: %s', $linkToPersonalSettings)
);

$template->addFooter();

$message = $this->mailer->createMessage();
$message->useTemplate($template);
$message->setTo([$userEmailAddress => $user->getDisplayName()]);
$message->setFrom([Util::getDefaultEmailAddress('no-reply') => $this->defaults->getName()]);
$message->setTo([$userEmailAddress => $userDisplayName]);
$message->setFrom([Util::getDefaultEmailAddress('no-reply') => $instanceName]);

return $message;
}
Expand Down

0 comments on commit 0d6a47b

Please # to comment.