zend-blog-3-backend

Форк
0
73 строки · 2.1 Кб
1
<?php
2

3
namespace App\Telegram\Command;
4

5
use App\Event\DeleteCommentEvent;
6
use App\Repository\CommentRepository;
7
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
8
use Xelbot\Telegram\Command\AbstractAdminCommand;
9
use Xelbot\Telegram\Command\TelegramCommandInterface;
10
use Xelbot\Telegram\Command\TelegramCommandTrait;
11
use Xelbot\Telegram\Entity\Message;
12
use Xelbot\Telegram\Robot;
13

14
class DeleteComment extends AbstractAdminCommand implements TelegramCommandInterface
15
{
16
    use TelegramCommandTrait;
17

18
    /**
19
     * @var CommentRepository
20
     */
21
    private $repository;
22

23
    /**
24
     * @var EventDispatcherInterface
25
     */
26
    private $dispatcher;
27

28
    /**
29
     * @param CommentRepository $repository
30
     * @param EventDispatcherInterface $dispatcher
31
     */
32
    public function __construct(CommentRepository $repository, EventDispatcherInterface $dispatcher)
33
    {
34
        $this->repository = $repository;
35
        $this->dispatcher = $dispatcher;
36
    }
37

38
    public function getCommandName(): string
39
    {
40
        return 'deletecomment';
41
    }
42

43
    /**
44
     * @param Message $message
45
     */
46
    protected function executeCommand(Message $message): void
47
    {
48
        $comment = null;
49
        $matches = [];
50
        if (preg_match('/^\/deletecomment (\d+)$/', $message->getText(), $matches)) {
51
            $commentId = (int)$matches[1];
52
            $comment = $this->repository->find($commentId);
53
        }
54

55
        if ($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

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.