zend-blog-3-backend

Форк
0
79 строк · 2.3 Кб
1
<?php
2

3
namespace App\Telegram\Command;
4

5
use App\DTO\CommentDTO;
6
use App\DTO\CommentUserDTO;
7
use App\Repository\CommentRepository;
8
use App\Service\CommentManager;
9
use App\Utils\Http;
10
use Xelbot\Telegram\Command\AbstractAdminCommand;
11
use Xelbot\Telegram\Command\TelegramCommandInterface;
12
use Xelbot\Telegram\Command\TelegramCommandTrait;
13
use Xelbot\Telegram\Entity\Message;
14
use Xelbot\Telegram\Robot;
15

16
class AnswerComment extends AbstractAdminCommand implements TelegramCommandInterface
17
{
18
    use TelegramCommandTrait;
19

20
    /**
21
     * @var CommentRepository
22
     */
23
    private $repository;
24

25
    /**
26
     * @var CommentManager
27
     */
28
    private $commentManager;
29

30
    public function __construct(CommentRepository $repository, CommentManager $commentManager)
31
    {
32
        $this->repository = $repository;
33
        $this->commentManager = $commentManager;
34
    }
35

36
    public function getCommandName(): string
37
    {
38
        return 'answer';
39
    }
40

41
    protected function executeCommand(Message $message): void
42
    {
43
        $comment = null;
44
        $matches = [];
45
        if (preg_match('/^\/answer (\d+)\s+(.+)$/ms', $message->getText(), $matches)) {
46
            $commentId = (int)$matches[1];
47
            $comment = $this->repository->find($commentId);
48
        }
49

50
        if ($comment) {
51
            $commentData = new CommentDTO();
52
            $commentData->topicId = $comment->getPost()->getId();
53
            $commentData->parentId = $comment->getId();
54
            $commentData->text = trim($matches[2]);
55

56
            $commentData->userAgent = $_SERVER['HTTP_USER_AGENT'] ?? '';
57
            $commentData->ipAddress = Http::getClientIp();
58

59
            $user = new CommentUserDTO();
60
            $user->id = 1;
61
            $commentData->user = $user;
62

63
            $this->commentManager->saveExternalComment($commentData);
64

65
            //TODO Null pointer exception may occur here
66
            $this->requester->sendMessage([
67
                'chat_id' => $message->getChat()->getId(),
68
                'text' => 'Готово ' . Robot::EMOJI_ROBOT,
69
                'parse_mode' => 'HTML',
70
            ]);
71
        } else {
72
            $this->requester->sendMessage([
73
                'chat_id' => $message->getChat()->getId(),
74
                'text' => 'Нет такого комментария, хозяин ' . Robot::EMOJI_ROBOT,
75
                'parse_mode' => 'HTML',
76
            ]);
77
        }
78
    }
79
}
80

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

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

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

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