[backend] stabilise codebase
This commit is contained in:
@@ -7,30 +7,32 @@ namespace App\MessageHandler;
|
||||
use App\Message\SignalCreatedMessage;
|
||||
use App\Repository\SignalRepository;
|
||||
use App\Service\SignalSnapshotBuilder;
|
||||
use DateTimeImmutable;
|
||||
use DateTimeZone;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
use Symfony\Component\Mercure\HubInterface;
|
||||
use Symfony\Component\Mercure\Update;
|
||||
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
|
||||
use Symfony\Component\Serializer\Exception\ExceptionInterface;
|
||||
use Symfony\Component\Serializer\SerializerInterface;
|
||||
use Throwable;
|
||||
use function json_encode;
|
||||
use const JSON_THROW_ON_ERROR;
|
||||
|
||||
#[AsMessageHandler]
|
||||
final class SignalCreatedMessageHandler
|
||||
final readonly class SignalCreatedMessageHandler
|
||||
{
|
||||
public function __construct(
|
||||
private readonly SignalRepository $signals,
|
||||
private readonly SignalSnapshotBuilder $snapshotBuilder,
|
||||
private readonly HubInterface $hub,
|
||||
#[Autowire('%app.signal_stream_topic%')] private readonly string $topic,
|
||||
#[Autowire('%app.signal_snapshot_limit%')] private readonly int $snapshotLimit,
|
||||
private readonly ?LoggerInterface $logger = null,
|
||||
private SignalRepository $signals,
|
||||
private SignalSnapshotBuilder $snapshotBuilder,
|
||||
private HubInterface $hub,
|
||||
private SerializerInterface $serializer,
|
||||
private LoggerInterface $logger,
|
||||
#[Autowire('%app.signal_stream_topic%')] private string $topic,
|
||||
#[Autowire('%app.signal_snapshot_limit%')] private int $snapshotLimit,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ExceptionInterface
|
||||
*/
|
||||
public function __invoke(SignalCreatedMessage $message): void
|
||||
{
|
||||
$recentSignals = $this->signals->findRecent($this->snapshotLimit);
|
||||
@@ -43,21 +45,17 @@ final class SignalCreatedMessageHandler
|
||||
'density' => $snapshot['density'],
|
||||
'latestByUser' => $snapshot['latestByUser'],
|
||||
'totals' => $snapshot['totals'],
|
||||
'updatedAt' => (new DateTimeImmutable('now', new DateTimeZone('UTC')))->format(DATE_ATOM),
|
||||
'updatedAt' => new \DateTimeImmutable('now', new \DateTimeZone('UTC'))->format(DATE_ATOM),
|
||||
],
|
||||
];
|
||||
|
||||
$data = json_encode($payload, JSON_THROW_ON_ERROR);
|
||||
|
||||
$update = new Update(
|
||||
topics: $this->topic,
|
||||
data: $data,
|
||||
);
|
||||
$data = $this->serializer->serialize($payload, 'json');
|
||||
$update = new Update(topics: $this->topic, data: $data);
|
||||
|
||||
try {
|
||||
$this->hub->publish($update);
|
||||
} catch (Throwable $exception) {
|
||||
$this->logger?->error('Failed to publish signal update to Mercure.', [
|
||||
$this->logger->error('Failed to publish signal update to Mercure.', [
|
||||
'exception' => $exception,
|
||||
]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user