/** @test */ public function shouldClearAllVariableValues() { Variables::clear(); Variables::set('test1', '123'); Variables::set('test2', '456'); Variables::clear(); $this->assertEquals([], Variables::all()); }
/** * @param string|null $string * @return string */ private function substituteVariables($string) { $vars = Variables::all(); if (0 === count($vars)) { return $string; } foreach ($vars as $name => $value) { $string = str_replace($name, $value, $string); } return $string; }
/** * @param ActionDto $dto * @return null */ public function processAction(ActionDto $dto) { if ($this->conditionResolver->isConditionMet($dto->get('condition'), Variables::all())) { $this->processActions($dto->get('then')); } else { $else = $dto->get('else'); if (is_array($else) && count($else) > 0) { $this->processActions($else); } } }
/** * @param array $actions */ protected function processActions($actions) { foreach ($actions as $action) { if (true === Variables::get('flowcontrol.continue')) { break; } if (true === Variables::get('flowcontrol.break')) { break; } $actionDto = $this->createActionDto(); $this->populateActionDto($actionDto, $action); $this->coreProcessor->processAction($actionDto); } }
/** * @param ActionDto $dto * @return null */ private function processUntilLoop(ActionDto $dto) { $actions = $dto->get('actions'); while (!$this->conditionResolver->isConditionMet($dto->get('condition'), Variables::all())) { if (true === Variables::get('flowcontrol.continue')) { Variables::remove('flowcontrol.continue'); continue; } if (true === Variables::get('flowcontrol.break')) { Variables::remove('flowcontrol.break'); break; } $this->processActions($actions); } }
/** * @param ActionDto $dto * @return null */ public function processAction(ActionDto $dto) { $name = $dto->get('name'); $value = $dto->get('value'); switch ((string) $value) { case 'increment': Variables::set($name, Variables::get($name) + 1); break; case 'decrement': Variables::set($name, Variables::get($name) - 1); break; default: Variables::set($name, $value); } }
/** * @param RequestDto $dto * @return null */ public function handler(RequestDto $dto) { // 4a. Timeout, exiting if ($this->coreProcessor->isMessageTimedOut($this->handlerId)) { $this->slackFacade->getSlackApi()->chatPostMessage($this->recipientId, 'Sorry, response timed out'); $this->coreProcessor->removeTimedMessageHandler($this->handlerId); return; } // 4b. Processed. Setting variable. if ($this->coreProcessor->isMessageHandled($this->handlerId)) { $dto = $this->coreProcessor->getTimedMessageHandleResult($this->handlerId); if (null === $dto) { return; } $this->coreProcessor->removeTimedMessageHandler($this->handlerId); $response = $dto->get('text'); Variables::set(Ar::get($this->dtoData, 'variable'), $response); // 5. Send "after" message $afterMessage = Ar::get($this->dtoData, 'messages.after'); if (null !== $afterMessage) { $this->slackFacade->getSlackApi()->chatPostMessage($this->recipientId, $afterMessage); } // 6. Processing other actions. $afterActions = Ar::get($this->dtoData, 'after'); if (0 === count($afterActions)) { return; } foreach ($afterActions as $afterAction) { $actionDto = $this->createActionDto(); $this->populateActionDto($actionDto, $afterAction); $this->coreProcessor->processAction($actionDto); } } }
/** * @param ActionDto $dto * @return null */ public function processAction(ActionDto $dto) { Variables::set('flowcontrol.break', 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; }