-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChatController.php
62 lines (51 loc) · 1.8 KB
/
ChatController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
namespace App\Controller;
use App\Entity\Message;
use App\Repository\MessageRepository;
use DateTime;
use Doctrine\ORM\EntityManagerInterface;
use Lcobucci\JWT\Builder;
use Lcobucci\JWT\Signer\Hmac\Sha256;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mercure\Publisher;
use Symfony\Component\Mercure\Update;
use Symfony\Component\Routing\Annotation\Route;
class ChatController extends AbstractController
{
/**
* @Route("/chat", name="chat")
*/
public function index(MessageRepository $messageRepository)
{
$messages = $messageRepository->findAll();
$response = $this->render('chat/index.html.twig', [
'messages' => $messages
]);
return $response;
}
/**
* @Route("/post", name="post_chat", methods={"POST"})
*/
public function post(EntityManagerInterface $entityManager, Publisher $publisher, Request $request)
{
$texte = $request->request->get('texte');
$message = new Message();
$message->setCreated(new DateTime('now'));
$message->setMessage($texte);
$message->setUser($this->getUser());
$entityManager->persist($message);
$entityManager->flush();
$update = new Update(
'http://chatlp.com/message',
json_encode(['message' => $texte,
'username' => $this->getUser()->getUsername(),
'created' => $message->getCreated()->format('d/m/Y H:i')])
// ['http://chatlp.com/message/user1', 'http://chatlp.com/message/user2']
);
// Sync, or async (RabbitMQ, Kafka...)
$publisher($update);
return $this->json(true, Response::HTTP_OK);
}
}