Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/iranianpep/botonomous
Browse files Browse the repository at this point in the history
  • Loading branch information
iranianpep committed Jul 13, 2017
2 parents 5900d84 + 5f4a5cc commit 88834d0
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 18 deletions.
36 changes: 27 additions & 9 deletions src/Botonomous/Slackbot.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,19 +214,13 @@ public function respond($message = null)
return $this->getLastError();
}

// create the class
$pluginClassFile = $command->getClass();
$pluginClass = new $pluginClassFile($this);

// check class is valid
if (!$pluginClass instanceof AbstractPlugin) {
throw new \Exception("Couldn't create class: '{$pluginClassFile}'");
}
$pluginClass = $this->getPluginClassByCommand($command);

// check action exists
$action = $command->getAction();
if (!method_exists($pluginClass, $action)) {
throw new \Exception("Action / function: '{$action}' does not exist in '{$pluginClassFile}'");
$className = get_class($pluginClass);
throw new \Exception("Action / function: '{$action}' does not exist in '{$className}'");
}

return $pluginClass->$action();
Expand All @@ -235,6 +229,30 @@ public function respond($message = null)
}
}

/**
* Get plugin class by command.
*
* @param Command $command
*
* @throws \Exception
*
* @return AbstractPlugin
*/
private function getPluginClassByCommand(Command $command)
{
// create the class
$pluginClassFile = $command->getClass();
$pluginClass = new $pluginClassFile($this);

// check class is valid
if (!$pluginClass instanceof AbstractPlugin) {
$className = get_class($pluginClass);
throw new \Exception("Couldn't create class: '{$className}'");
}

return $pluginClass;
}

/**
* @param null $message
*
Expand Down
19 changes: 15 additions & 4 deletions src/Botonomous/utility/LoggerUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,24 @@ private function pushMonologHandler(Logger $logger, $handlerKey)
*/
public function getLogFilePath()
{
$monologConfig = $this->getMonologConfig();

if (!isset($monologConfig['handlers']['file']['fileName'])) {
$monologConfigFile = $this->getMonologConfigFileName();
if (empty($monologConfigFile)) {
return false;
}

return $this->getTempDir().DIRECTORY_SEPARATOR.$monologConfig['handlers']['file']['fileName'];
return $this->getTempDir().DIRECTORY_SEPARATOR.$monologConfigFile;
}

/**
* @return mixed
*/
private function getMonologConfigFileName()
{
$monologConfig = $this->getMonologConfig();

if (isset($monologConfig['handlers']['file']['fileName'])) {
return $monologConfig['handlers']['file']['fileName'];
}
}

/**
Expand Down
13 changes: 8 additions & 5 deletions src/Botonomous/utility/MessageUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ class MessageUtility extends AbstractUtility
*/
public function removeMentionedBot($message)
{
$botUserId = $this->getConfig()->get('botUserId');
$userLink = $this->linkToUser($botUserId);
$userLink = $this->getUserLink();

return preg_replace("/{$userLink}/", '', $message, 1);
}
Expand All @@ -37,8 +36,7 @@ public function removeMentionedBot($message)
*/
public function isBotMentioned($message)
{
$botUserId = $this->getConfig()->get('botUserId');
$userLink = $this->linkToUser($botUserId);
$userLink = $this->getUserLink();

return (new StringUtility())->findInString($userLink, $message, false);
}
Expand Down Expand Up @@ -74,7 +72,7 @@ public function extractCommandName($message)
*
* @param $message
*
* @return \Botonomous\Command
* @return \Botonomous\Command|null
*/
public function extractCommandDetails($message)
{
Expand Down Expand Up @@ -118,4 +116,9 @@ public function linkToUser($userId, $userName = '')

return "<@{$userId}{$userName}>";
}

private function getUserLink()
{
return $this->linkToUser($this->getConfig()->get('botUserId'));
}
}

0 comments on commit 88834d0

Please sign in to comment.