src/Service/CommandResolver.php line 38

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use App\Service\SessionManager;
  4. use App\Service\StateHandler;
  5. use App\Service\Messenger;
  6. use Psr\Log\LoggerInterface;
  7. use Psr\Log\Test\LoggerInterfaceTest;
  8. class CommandResolver
  9. {
  10.     private $command;
  11.     private $sessionManager;
  12.     private $sessionState;
  13.     private $baseCommandStart;
  14.     private $baseCommandCancel;
  15.     private $stateHandler;
  16.     private $messenger;
  17.     private $devEnvironment;
  18.     private $telegramDTO;
  19.     private $telegramId;
  20.     private $statusInactive;
  21.     private $prefixMsgWelcome;
  22.     private $prefixMsgCancel;
  23.     private $prefixMenuMain;
  24.     private $logger;
  25.     /**
  26.      * CommandResolver constructor.
  27.      * @param LoggerInterface $logger
  28.      * @param \App\Service\SessionManager $sessionManager
  29.      * @param \App\Service\Messenger $messenger
  30.      * @param TelegramConnector $telegramConnector
  31.      * @param \App\Service\StateHandler $stateHandler
  32.      */
  33.     public function __construct(
  34.         LoggerInterface $logger,
  35.         SessionManager $sessionManager,
  36.         Messenger $messenger,
  37.         TelegramConnector $telegramConnector,
  38.         StateHandler $stateHandler
  39.     ) {
  40.         $this->command false;
  41.         $this->baseCommandStart $_ENV['APP_COMMAND_START'];
  42.         $this->baseCommandCancel $_ENV['APP_COMMAND_CANCEL'];
  43.         $this->statusInactive $_ENV['APP_STATUS_INACTIVE'];
  44.         $this->prefixMsgWelcome $_ENV['APP_PREFIX_MSG_WELCOME'];
  45.         $this->prefixMsgCancel $_ENV['APP_PREFIX_MSG_CANCEL'];
  46.         $this->prefixMenuMain $_ENV['APP_PREFIX_MENU_MAIN'];
  47.         $this->sessionManager $sessionManager;
  48.         $this->telegramDTO $telegramConnector->receiveTgData();
  49.         $this->messenger $messenger;
  50.         $this->stateHandler $stateHandler;
  51.         $this->devEnvironment getenv('APP_ENV');
  52.         $this->logger $logger;
  53.     }
  54.     private function isCommandStart()
  55.     {
  56.         if ($this->baseCommandStart == $this->getCommand()) {
  57.             return true;
  58.         }
  59.         return false;
  60.     }
  61.     private function isCommandCancel()
  62.     {
  63.         if ($this->baseCommandCancel == $this->getCommand()) {
  64.             return true;
  65.         }
  66.         return false;
  67.     }
  68.     # Если сессия не пуста
  69.     private function executeCommand()
  70.     {
  71.         # для команды start # сброс сессии
  72.         if ($this->isCommandStart()) {
  73.             $this->logger->warning('PrefixMsg: ' $this->prefixMsgWelcome);
  74.             $this->logger->warning('PrefixMenu: ' $this->prefixMenuMain);
  75.             $this->messenger->sendTextMessage($this->prefixMsgWelcome$this->prefixMenuMain);
  76.             $this->sessionManager->setSession($this->telegramId$this->statusInactive);
  77.             return true;
  78.         }
  79.         # для команды Отмена # сброс сессии
  80.         if ($this->isCommandCancel()) {
  81.             $this->messenger->sendTextMessage($this->prefixMsgCancel$this->prefixMenuMain);
  82.             $this->sessionManager->setSession($this->telegramId$this->statusInactive);
  83.             return true;
  84.         }
  85.         //Обработка всех остальных команд (Анкет и API запросов)
  86.         $this->stateHandler->handle($this->telegramDTO);
  87.         return false;
  88.     }
  89.     private function initServiceData()
  90.     {
  91.         $this->telegramId $this->telegramDTO->getUserId();
  92.         $this->sessionState $this->sessionManager->getSession($this->telegramId);
  93.         if (!$this->sessionState) {
  94.             $this->sessionManager->initSession($this->telegramId$this->statusInactive);
  95.         }
  96.         $command $this->telegramDTO->getTextMessage();
  97.         $this->setCommand($command);
  98.     }
  99.     public function resolve()
  100.     {
  101.         $this->initServiceData();
  102.         return $this->executeCommand();
  103.     }
  104.     /**
  105.      * Get the value of command
  106.      */
  107.     public function getCommand()
  108.     {
  109.         return $this->command;
  110.     }
  111.     /**
  112.      * Set the value of command
  113.      *
  114.      * @return  self
  115.      */
  116.     public function setCommand($command)
  117.     {
  118.         $this->command $command;
  119.         return $this;
  120.     }
  121.     /**
  122.      * @param false $sessionState
  123.      */
  124.     public function setSessionState($sessionState): void
  125.     {
  126.         $this->sessionState $sessionState;
  127.     }
  128. }