コード例 #1
0
 /** @test */
 public function shouldReturnToken()
 {
     $token = 'token';
     $curlRequestMock = \Mockery::mock('\\slackbot\\util\\CurlRequest');
     $slackApi = new SlackApi($curlRequestMock);
     $slackApi->setToken($token);
     $this->assertEquals($token, $slackApi->getToken());
 }
コード例 #2
0
 public function getUserGroups($userId)
 {
     $groups = Ar::get($this->slackApi->groupsList(), 'groups');
     return Ar::filter($groups, function ($group) use($userId) {
         return in_array($userId, Ar::get($group, 'members') ?: []);
     });
 }
コード例 #3
0
 /**
  * @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;
 }
コード例 #4
0
 public function buildContainer(ArgvParser $argvParser, Config $config = null)
 {
     $container = new Container();
     $container['started'] = time();
     $container['config'] = function () use($config, $container) {
         return null !== $config ? $config : new Config($container['yaml_parser'], $container['file_loader']);
     };
     $container['variables_placer'] = function () {
         return new VariablesPlacer();
     };
     $container['cron_expression'] = function () {
         return CronExpression::factory('@daily');
     };
     $container['argv_parser'] = function () use($argvParser) {
         return $argvParser;
     };
     $container['yaml_parser'] = function () {
         return new Parser();
     };
     $container['file_loader'] = function () {
         return new FileLoader();
     };
     $container['curl_request'] = function () {
         return new CurlRequest();
     };
     $container['post_parser'] = function () {
         return new PostParser();
     };
     $container['slack_api'] = function (Container $container) {
         $slackApi = new SlackApi($container['curl_request']);
         $token = $container['config']->getEntry('auth.token');
         if ($token !== null) {
             $slackApi->setToken($token);
         }
         return $slackApi;
     };
     $container['slack_facade'] = function (Container $container) {
         return new SlackFacade($container['slack_api']);
     };
     $container['names_resolver'] = function (Container $container) {
         return new NamesResolver($container['slack_facade']);
     };
     $container['condition_resolver'] = function () {
         return new ConditionResolver();
     };
     $container['handler_execution_resolver'] = function (Container $container) {
         return new HandlerExecutionResolver($container['config'], $container['slack_api']);
     };
     $container['core_processor'] = function (Container $container) {
         return new CoreProcessor($container['slack_facade'], $container['handler_execution_resolver']);
     };
     $container['output_manager'] = function (Container $container) {
         return new OutputManager($container['slack_facade']);
     };
     $container['api_cache'] = function () {
         return new ApiCache(new MemoryStorage());
     };
     $container['action_send_message'] = function (Container $container) {
         return new SendMessageActionHandler($container['slack_facade'], $container['output_manager']);
     };
     $container['action_set_variable'] = function (Container $container) {
         return new SetVariableActionHandler($container['slack_facade']);
     };
     $container['action_if'] = function (Container $container) {
         $handler = new IfActionHandler($container['slack_facade'], $container['condition_resolver']);
         $handler->setCoreProcessor($container['core_processor']);
         return $handler;
     };
     $container['action_loop'] = function (Container $container) {
         $handler = new LoopActionHandler($container['slack_facade'], $container['condition_resolver']);
         $handler->setCoreProcessor($container['core_processor']);
         return $handler;
     };
     $container['action_user_input'] = function (Container $container) {
         return new UserInputActionHandler($container['slack_facade'], $container['core_processor']);
     };
     $container['action_continue'] = function () {
         return new ContinueActionHandler();
     };
     $container['action_break'] = function () {
         return new BreakActionHandler();
     };
     $container['action_run_command'] = function (Container $container) {
         return new RunCommandActionHandler($container['slack_facade']);
     };
     $container['core_processor']->addActionHandler($container['action_send_message']);
     $container['core_processor']->addActionHandler($container['action_set_variable']);
     $container['core_processor']->addActionHandler($container['action_if']);
     $container['core_processor']->addActionHandler($container['action_loop']);
     $container['core_processor']->addActionHandler($container['action_user_input']);
     $container['core_processor']->addActionHandler($container['action_continue']);
     $container['core_processor']->addActionHandler($container['action_break']);
     $container['core_processor']->addActionHandler($container['action_run_command']);
     if (class_exists('\\slackbot\\handlers\\command\\TestCommandHandler')) {
         $container['command_test'] = function () {
             return new \slackbot\handlers\command\TestCommandHandler();
         };
         $container['core_processor']->addCommandHandler($container['command_test']);
     }
     $container['command_restart'] = function () {
         return new RestartCommandHandler();
     };
     $container['core_processor']->addCommandHandler($container['command_restart']);
     $container['command_status'] = function () {
         return new StatusCommandHandler();
     };
     $container['core_processor']->addCommandHandler($container['command_status']);
     $container['server'] = $this->buildServer();
     return $container;
 }