45 lines
1.3 KiB
PHP
45 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Download\Framework\Controller;
|
|
|
|
use App\Download\Action\Handler\AddMonitorHandler;
|
|
use App\Download\Action\Input\AddMonitorInput;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
|
use Symfony\Component\Mercure\HubInterface;
|
|
use Symfony\Component\Mercure\Update;
|
|
use Symfony\Component\Routing\Attribute\Route;
|
|
use Twig\Environment;
|
|
|
|
class ApiController extends AbstractController
|
|
{
|
|
public function __construct(
|
|
#[Autowire(service: 'twig')]
|
|
private readonly Environment $renderer,
|
|
private readonly HubInterface $hub,
|
|
) {}
|
|
|
|
#[Route('/api/monitor', name: 'api_monitor', methods: ['POST'])]
|
|
public function addMonitor(
|
|
AddMonitorInput $input,
|
|
AddMonitorHandler $handler,
|
|
HubInterface $hub,
|
|
) {
|
|
$response = $handler->handle($input->toCommand());
|
|
|
|
$hub->publish(new Update(
|
|
'alerts',
|
|
$this->renderer->render('broadcast/Alert.html.twig', [
|
|
'alert_id' => uniqid(),
|
|
'title' => 'Success',
|
|
'message' => "New monitor added for {$input->title}",
|
|
])
|
|
));
|
|
|
|
return $this->json([
|
|
'status' => 200,
|
|
'message' => $response
|
|
]);
|
|
}
|
|
}
|