Files
myep/src/Dashboard/Widget/RecentLogEntriesWidgetProvider.php
T

79 lines
2.2 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Dashboard\Widget;
use App\Dashboard\Contract\DashboardWidgetProviderInterface;
use App\Entity\LogEntry;
use App\Model\DashboardWidget;
use App\Model\DashboardWidgetEntry;
use App\Repository\LogEntryRepository;
use App\Security\Role;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
/**
* What the application has been up to lately.
*
* The entries themselves are display only: a log line is a report, not somewhere to go, and
* everything worth drilling into lives behind the log list's own filters — which is where the
* card's header link leads.
*/
class RecentLogEntriesWidgetProvider implements DashboardWidgetProviderInterface
{
private const LIMIT = 5;
/**
* Log messages have no length limit worth relying on, and the widget renders one line per
* entry, so the message is cut to something a card can hold.
*/
private const MESSAGE_LENGTH = 60;
public function __construct(
private readonly LogEntryRepository $logEntryRepository,
private readonly UrlGeneratorInterface $urlGenerator,
) {
}
public function getRequiredRole(): string
{
return Role::CUSTOMER_EXPERT;
}
public function getPriority(): int
{
return 80;
}
public function build(): ?DashboardWidget
{
return new DashboardWidget(
'Neueste Logeinträge',
array_map(static fn (LogEntry $entry): DashboardWidgetEntry => new DashboardWidgetEntry(
self::label($entry),
null,
'document',
), $this->logEntryRepository->findLatest(self::LIMIT)),
'Keine Logeinträge vorhanden.',
$this->urlGenerator->generate('app_admin_log'),
'Alle Logeinträge',
);
}
private static function label(LogEntry $entry): string
{
$message = (string) $entry->getMessage();
if (mb_strlen($message) > self::MESSAGE_LENGTH) {
$message = mb_substr($message, 0, self::MESSAGE_LENGTH).'…';
}
return sprintf(
'%s %s: %s',
$entry->getCreatedAt()?->format('d.m.Y H:i') ?? '',
(string) $entry->getChannel(),
$message,
);
}
}