<?php
namespace App\Service;
use App\Service\SessionManager;
use App\Service\StateHandler;
use App\Service\Messenger;
use Psr\Log\LoggerInterface;
use Psr\Log\Test\LoggerInterfaceTest;
class CommandResolver
{
private $command;
private $sessionManager;
private $sessionState;
private $baseCommandStart;
private $baseCommandCancel;
private $stateHandler;
private $messenger;
private $devEnvironment;
private $telegramDTO;
private $telegramId;
private $statusInactive;
private $prefixMsgWelcome;
private $prefixMsgCancel;
private $prefixMenuMain;
private $logger;
/**
* CommandResolver constructor.
* @param LoggerInterface $logger
* @param \App\Service\SessionManager $sessionManager
* @param \App\Service\Messenger $messenger
* @param TelegramConnector $telegramConnector
* @param \App\Service\StateHandler $stateHandler
*/
public function __construct(
LoggerInterface $logger,
SessionManager $sessionManager,
Messenger $messenger,
TelegramConnector $telegramConnector,
StateHandler $stateHandler
) {
$this->command = false;
$this->baseCommandStart = $_ENV['APP_COMMAND_START'];
$this->baseCommandCancel = $_ENV['APP_COMMAND_CANCEL'];
$this->statusInactive = $_ENV['APP_STATUS_INACTIVE'];
$this->prefixMsgWelcome = $_ENV['APP_PREFIX_MSG_WELCOME'];
$this->prefixMsgCancel = $_ENV['APP_PREFIX_MSG_CANCEL'];
$this->prefixMenuMain = $_ENV['APP_PREFIX_MENU_MAIN'];
$this->sessionManager = $sessionManager;
$this->telegramDTO = $telegramConnector->receiveTgData();
$this->messenger = $messenger;
$this->stateHandler = $stateHandler;
$this->devEnvironment = getenv('APP_ENV');
$this->logger = $logger;
}
private function isCommandStart()
{
if ($this->baseCommandStart == $this->getCommand()) {
return true;
}
return false;
}
private function isCommandCancel()
{
if ($this->baseCommandCancel == $this->getCommand()) {
return true;
}
return false;
}
# Если сессия не пуста
private function executeCommand()
{
# для команды start # сброс сессии
if ($this->isCommandStart()) {
$this->logger->warning('PrefixMsg: ' . $this->prefixMsgWelcome);
$this->logger->warning('PrefixMenu: ' . $this->prefixMenuMain);
$this->messenger->sendTextMessage($this->prefixMsgWelcome, $this->prefixMenuMain);
$this->sessionManager->setSession($this->telegramId, $this->statusInactive);
return true;
}
# для команды Отмена # сброс сессии
if ($this->isCommandCancel()) {
$this->messenger->sendTextMessage($this->prefixMsgCancel, $this->prefixMenuMain);
$this->sessionManager->setSession($this->telegramId, $this->statusInactive);
return true;
}
//Обработка всех остальных команд (Анкет и API запросов)
$this->stateHandler->handle($this->telegramDTO);
return false;
}
private function initServiceData()
{
$this->telegramId = $this->telegramDTO->getUserId();
$this->sessionState = $this->sessionManager->getSession($this->telegramId);
if (!$this->sessionState) {
$this->sessionManager->initSession($this->telegramId, $this->statusInactive);
}
$command = $this->telegramDTO->getTextMessage();
$this->setCommand($command);
}
public function resolve()
{
$this->initServiceData();
return $this->executeCommand();
}
/**
* Get the value of command
*/
public function getCommand()
{
return $this->command;
}
/**
* Set the value of command
*
* @return self
*/
public function setCommand($command)
{
$this->command = $command;
return $this;
}
/**
* @param false $sessionState
*/
public function setSessionState($sessionState): void
{
$this->sessionState = $sessionState;
}
}