Share repeated controller and service code
- AppController: joinUrl(), adminUrlFor(), setPollInterval() and protect() instead of copies in Admin-, Home-, Participate- and EmbedController. - PollMailer::sendSafely() replaces two try/catch blocks around mail sending. - Slide::optionIds() replaces five identical array_map() calls. - PollStateService: one pollPayload() for presenter and embed; the vote action passes the already loaded slide instead of loading it again. - ResultsService::forSlide() takes the answer/comment limit and the rating flag, so ReportService no longer queries answers, comments and the scale distribution twice and the export loads the slides only once. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ad537b9b34
commit
3e44ded107
14 files changed
+189
-137
No files matched your search
@@ -9,9 +9,6 @@ use App\Model\Entity\Slide;
|
||||
use App\Model\Table\PollsTable;
|
||||
use Cake\Core\Configure;
|
||||
use Cake\Http\Response;
|
||||
use Cake\Log\Log;
|
||||
use Cake\Routing\Router;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Public pages: start page, poll creation, joining by code, link recovery.
|
||||
@@ -82,9 +79,8 @@ class HomeController extends AppController
|
||||
} elseif (!$poll->hasErrors()) {
|
||||
[$poll, $token] = $this->Polls->insertPoll($poll);
|
||||
if ($token !== null) {
|
||||
$adminUrl = Router::url(['_name' => 'admin', 'token' => $token], true);
|
||||
$joinUrl = Router::url(['_name' => 'participate', 'code' => $poll->join_code], true);
|
||||
$sent = $this->sendMail(fn(PollMailer $mailer) => $mailer->send('adminLink', [$poll, $adminUrl, $joinUrl]));
|
||||
$adminUrl = $this->adminUrlFor($token);
|
||||
$sent = (new PollMailer())->sendSafely('adminLink', [$poll, $adminUrl, $this->joinUrl($poll)]);
|
||||
|
||||
$session = $this->request->getSession();
|
||||
$session->write('Plenario.created', [
|
||||
@@ -218,31 +214,12 @@ class HomeController extends AppController
|
||||
}
|
||||
$links[] = [
|
||||
'title' => $poll->title,
|
||||
'url' => Router::url(['_name' => 'admin', 'token' => $token], true),
|
||||
'url' => $this->adminUrlFor($token),
|
||||
'created' => $poll->created->i18nFormat('dd.MM.yyyy HH:mm'),
|
||||
];
|
||||
}
|
||||
if ($links !== []) {
|
||||
$this->sendMail(fn(PollMailer $mailer) => $mailer->send('recovery', [$email, $links]));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a mail and logs (instead of throwing) transport errors.
|
||||
*
|
||||
* @param callable(\App\Mailer\PollMailer): mixed $send Callback
|
||||
* @return bool
|
||||
*/
|
||||
protected function sendMail(callable $send): bool
|
||||
{
|
||||
try {
|
||||
$send(new PollMailer());
|
||||
|
||||
return true;
|
||||
} catch (Throwable $e) {
|
||||
Log::error('Mail delivery failed: ' . $e->getMessage());
|
||||
|
||||
return false;
|
||||
(new PollMailer())->sendSafely('recovery', [$email, $links]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user