zend-blog-3-backend
73 строки · 2.1 Кб
1<?php
2
3namespace App\Telegram\Command;
4
5use App\Event\DeleteCommentEvent;
6use App\Repository\CommentRepository;
7use Symfony\Component\EventDispatcher\EventDispatcherInterface;
8use Xelbot\Telegram\Command\AbstractAdminCommand;
9use Xelbot\Telegram\Command\TelegramCommandInterface;
10use Xelbot\Telegram\Command\TelegramCommandTrait;
11use Xelbot\Telegram\Entity\Message;
12use Xelbot\Telegram\Robot;
13
14class DeleteComment extends AbstractAdminCommand implements TelegramCommandInterface
15{
16use TelegramCommandTrait;
17
18/**
19* @var CommentRepository
20*/
21private $repository;
22
23/**
24* @var EventDispatcherInterface
25*/
26private $dispatcher;
27
28/**
29* @param CommentRepository $repository
30* @param EventDispatcherInterface $dispatcher
31*/
32public function __construct(CommentRepository $repository, EventDispatcherInterface $dispatcher)
33{
34$this->repository = $repository;
35$this->dispatcher = $dispatcher;
36}
37
38public function getCommandName(): string
39{
40return 'deletecomment';
41}
42
43/**
44* @param Message $message
45*/
46protected function executeCommand(Message $message): void
47{
48$comment = null;
49$matches = [];
50if (preg_match('/^\/deletecomment (\d+)$/', $message->getText(), $matches)) {
51$commentId = (int)$matches[1];
52$comment = $this->repository->find($commentId);
53}
54
55if ($comment) {
56$this->repository->markAsDeleted($comment);
57$this->dispatcher->dispatch(new DeleteCommentEvent($comment));
58
59//TODO Null pointer exception may occur here
60$this->requester->sendMessage([
61'chat_id' => $message->getChat()->getId(),
62'text' => 'Готово ' . Robot::EMOJI_ROBOT,
63'parse_mode' => 'HTML',
64]);
65} else {
66$this->requester->sendMessage([
67'chat_id' => $message->getChat()->getId(),
68'text' => 'Нет такого комментария, хозяин ' . Robot::EMOJI_ROBOT,
69'parse_mode' => 'HTML',
70]);
71}
72}
73}
74