zend-blog-3-backend
79 строк · 2.3 Кб
1<?php
2
3namespace App\Telegram\Command;4
5use App\DTO\CommentDTO;6use App\DTO\CommentUserDTO;7use App\Repository\CommentRepository;8use App\Service\CommentManager;9use App\Utils\Http;10use Xelbot\Telegram\Command\AbstractAdminCommand;11use Xelbot\Telegram\Command\TelegramCommandInterface;12use Xelbot\Telegram\Command\TelegramCommandTrait;13use Xelbot\Telegram\Entity\Message;14use Xelbot\Telegram\Robot;15
16class AnswerComment extends AbstractAdminCommand implements TelegramCommandInterface17{
18use TelegramCommandTrait;19
20/**21* @var CommentRepository
22*/
23private $repository;24
25/**26* @var CommentManager
27*/
28private $commentManager;29
30public function __construct(CommentRepository $repository, CommentManager $commentManager)31{32$this->repository = $repository;33$this->commentManager = $commentManager;34}35
36public function getCommandName(): string37{38return 'answer';39}40
41protected function executeCommand(Message $message): void42{43$comment = null;44$matches = [];45if (preg_match('/^\/answer (\d+)\s+(.+)$/ms', $message->getText(), $matches)) {46$commentId = (int)$matches[1];47$comment = $this->repository->find($commentId);48}49
50if ($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 here66$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