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
@@ -13,16 +13,13 @@ use App\Service\ReportService;
|
||||
use App\Service\ResetService;
|
||||
use App\Service\SlideControlService;
|
||||
use App\View\PdfView;
|
||||
use Cake\Core\Configure;
|
||||
use Cake\Http\Exception\BadRequestException;
|
||||
use Cake\Http\Response;
|
||||
use Cake\I18n\DateTime;
|
||||
use Cake\Log\Log;
|
||||
use Cake\Routing\Router;
|
||||
use chillerlan\QRCode\Output\QRMarkupSVG;
|
||||
use chillerlan\QRCode\QRCode;
|
||||
use chillerlan\QRCode\QROptions;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Poll level admin actions: dashboard, settings, presenter mode, export.
|
||||
@@ -58,7 +55,7 @@ class AdminController extends AdminAppController
|
||||
$responseCounts = $this->Responses->countParticipantsPerSlide($this->poll->id);
|
||||
$typeLabels = Slide::typeLabels();
|
||||
$typeDescriptions = Slide::typeDescriptions();
|
||||
$joinUrl = Router::url(['_name' => 'participate', 'code' => $this->poll->join_code], true);
|
||||
$joinUrl = $this->joinUrl($this->poll);
|
||||
// Created on first visit, so every slide has its embed URL ready to copy.
|
||||
$embedKey = $this->Polls->ensureEmbedKey($this->poll);
|
||||
|
||||
@@ -95,13 +92,10 @@ class AdminController extends AdminAppController
|
||||
$this->Polls->saveOrFail($this->poll);
|
||||
$this->token = $token;
|
||||
|
||||
$adminUrl = Router::url($this->adminUrl(), true);
|
||||
$joinUrl = Router::url(['_name' => 'participate', 'code' => $this->poll->join_code], true);
|
||||
try {
|
||||
(new PollMailer())->send('adminLink', [$this->poll, $adminUrl, $joinUrl]);
|
||||
$sent = (new PollMailer())->sendSafely('adminLink', [$this->poll, $this->adminUrlFor($token), $this->joinUrl($this->poll)]);
|
||||
if ($sent) {
|
||||
$this->Flash->success(__('Neuer Admin-Link erstellt und per E-Mail verschickt. Der alte Link ist ab sofort ungültig – speichere dir diese Seite als Lesezeichen.'));
|
||||
} catch (Throwable $e) {
|
||||
Log::error('Mail delivery failed: ' . $e->getMessage());
|
||||
} else {
|
||||
$this->Flash->warning(__('Neuer Admin-Link erstellt, die E-Mail konnte aber nicht verschickt werden. Speichere dir diese Seite unbedingt als Lesezeichen!'));
|
||||
}
|
||||
|
||||
@@ -177,9 +171,9 @@ class AdminController extends AdminAppController
|
||||
}
|
||||
|
||||
$this->viewBuilder()->setLayout('present');
|
||||
$this->set('joinUrl', Router::url(['_name' => 'participate', 'code' => $this->poll->join_code], true));
|
||||
$this->set('joinUrl', $this->joinUrl($this->poll));
|
||||
$this->set('joinBaseUrl', Router::url(['_name' => 'join:short'], true));
|
||||
$this->set('pollIntervalMs', (int)Configure::read('Plenario.pollIntervalMs', 2000));
|
||||
$this->setPollInterval();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -275,7 +269,7 @@ class AdminController extends AdminAppController
|
||||
*/
|
||||
public function qr(): Response
|
||||
{
|
||||
$url = Router::url(['_name' => 'participate', 'code' => $this->poll->join_code], true);
|
||||
$url = $this->joinUrl($this->poll);
|
||||
$options = new QROptions([
|
||||
'outputInterface' => QRMarkupSVG::class,
|
||||
'outputBase64' => false,
|
||||
@@ -301,13 +295,13 @@ class AdminController extends AdminAppController
|
||||
*/
|
||||
public function export(): Response
|
||||
{
|
||||
$report = new ReportService();
|
||||
$hasInternal = $report->hasInternalContent($this->poll);
|
||||
$sections = (new ReportService())->build($this->poll, $this->request->getQuery('internal') === '1');
|
||||
$hasInternal = ReportService::hasInternal($sections);
|
||||
$includeInternal = $hasInternal && $this->request->getQuery('internal') === '1';
|
||||
|
||||
$this->viewBuilder()->setClassName(PdfView::class);
|
||||
$this->set([
|
||||
'sections' => $report->build($this->poll, $includeInternal),
|
||||
'sections' => $sections,
|
||||
'includeInternal' => $includeInternal,
|
||||
'hasInternal' => $hasInternal,
|
||||
'exportedAt' => DateTime::now(),
|
||||
|
||||
Reference in new issue
Block a user