/**
  * @param RequestHandlerInterface $handler
  * @param RequestDto $dto
  * @return bool
  */
 public function shouldExecute(RequestHandlerInterface $handler, RequestDto $dto)
 {
     $section = $this->config->getSection('custom');
     if (empty($section)) {
         return true;
     }
     $dtoChannel = $dto->getChannel();
     if (null === $dtoChannel) {
         return true;
     }
     $handlerId = $handler->getId();
     $handlerConfig = $this->config->getEntry('custom.' . $handlerId);
     if (empty($handlerConfig)) {
         return false;
     }
     $dtoChannelType = $dtoChannel[0];
     if ('D' === $dtoChannelType) {
         return Ar::get($handlerConfig, 'dm') ?: true;
     }
     if ('C' === $dtoChannelType) {
         $dtoChannelInfo = $this->slackApi->channelsInfo($dtoChannel);
         $dtoChannelName = '#' . Ar::get($dtoChannelInfo, 'channel.name');
     } else {
         $dtoChannelInfo = $this->slackApi->groupsInfo($dtoChannel);
         $dtoChannelName = Ar::get($dtoChannelInfo, 'group.name');
     }
     $defaultBehavior = $this->config->getEntry('custom.' . $handlerId . '.default');
     $handlerChannels = $this->config->getEntry('custom.' . $handlerId . '.channels');
     if (empty($handlerChannels)) {
         return 'allow' === $defaultBehavior ? true : false;
     }
     $handlerChannelConfig = $this->config->getSectionFromArray('custom.' . $handlerId . '.channels', 'name=' . $dtoChannelName);
     if (empty($handlerChannelConfig)) {
         return 'allow' === $defaultBehavior ? true : false;
     }
     $this->params = Ar::get($handlerChannelConfig, 'params') ?: [];
     return true;
 }
 public function buildServer()
 {
     $server = new Server("SlackBot", "0.1");
     $server->post('/playbook/run/', function (Request $request, Response $response, $next) {
         $rawData = $request->getData();
         $postParser = Registry::get('container')['post_parser'];
         $parsedData = $postParser->parse($rawData);
         $playbook = urldecode($parsedData['playbook']);
         $yamlParser = Registry::get('container')['yaml_parser'];
         $playbook = $yamlParser->parse($playbook);
         /** @var SlackApi $slackApi */
         $slackApi = Registry::get('container')['slack_api'];
         $playbookToken = Ar::get($playbook, 'auth.token');
         $oldToken = null;
         if (null !== $playbookToken) {
             $oldToken = $slackApi->getToken();
             $slackApi->setToken($playbookToken);
         }
         $executor = new PlaybookExecutor(Registry::get('container')['core_processor']);
         Variables::clear();
         $executor->execute($playbook);
         $response->write('Playbook executed successfully');
         $response->end();
         if (null !== $playbookToken) {
             $slackApi->setToken($oldToken);
         }
         $fileName = basename($parsedData['filename']);
         echo '[INFO] Executing playbook file ' . $fileName . "\n";
         $next();
     });
     $server->post('/command/run/', function (Request $request, Response $response, $next) {
         $rawData = $request->getData();
         $postParser = Registry::get('container')['post_parser'];
         $parsedData = $postParser->parse($rawData);
         $command = urldecode($parsedData['command']);
         $coreProcessor = Registry::get('container')['core_processor'];
         $dto = new RequestDto();
         $dto->setData(['type' => 'message', 'channel' => 'cron', 'user' => 'cron', 'text' => $command, 'ts' => time()]);
         $coreProcessor->processCommand($dto);
         $response->write('Playbook executed successfully');
         $response->end();
         echo '[INFO] Executing command ' . $command . "\n";
         $next();
     });
     $server->post('/process/message/', function (Request $request, Response $response, $next) {
         $response->sendHeaders();
         $response->writeJson(['ok' => true]);
         $response->end();
         $rawData = $request->getData();
         $postParser = Registry::get('container')['post_parser'];
         $parsedData = $postParser->parse($rawData);
         /** @var CoreProcessor $coreProcessor */
         $coreProcessor = Registry::get('container')['core_processor'];
         $dto = new RequestDto();
         $dto->setSource('rtm');
         $dto->setData(json_decode(Ar::get($parsedData, 'message'), true));
         $coreProcessor->process($dto);
         $next();
     });
     $server->get('/info/cron/', function (Request $request, Response $response, $next) {
         $response->writeJson(Registry::get('container')['config']->getSection('cron'));
         $response->end();
         $next();
     });
     return $server;
 }
 /**
  * @param RequestDto $dto
  * @return bool
  */
 public function checker(RequestDto $dto)
 {
     return (int) $dto->get('text') > 0;
 }
 /**
  * @param RequestDto $dto
  * @param $commandHandler
  * @return bool
  */
 protected function checkAccess(RequestDto $dto, CommandHandlerInterface $commandHandler)
 {
     /** @var Config $config */
     $config = Registry::get('container')['config'];
     $acl = $commandHandler->getAcl();
     if (CommandHandlerInterface::ACL_ANY === $acl) {
         return true;
     } elseif (CommandHandlerInterface::ACL_ADMIN === $acl) {
         $currentUser = $dto->getUser();
         $currentUserName = $this->slackFacade->getUserNameById($currentUser);
         $admins = $config->getEntry('acl.admins') ?: [];
         if (0 === count($admins)) {
             return false;
         }
         return in_array($currentUserName, $admins);
     } else {
         if (!is_array($acl)) {
             throw new \RuntimeException('Wrong ACL format: array expected');
         }
         $currentUser = $dto->getUser();
         $aclUsers = [];
         foreach ($acl as $aclItem) {
             $aclUsers = array_merge($aclUsers, $this->slackFacade->getRecipientUsersIds($aclItem));
         }
         $aclUsers = array_unique($aclUsers);
         return in_array($currentUser, $aclUsers);
     }
 }