Skip to content

Commit

Permalink
Constants are your good friends
Browse files Browse the repository at this point in the history
  • Loading branch information
iranianpep committed Dec 13, 2017
1 parent 36581bd commit e31d041
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 14 deletions.
6 changes: 6 additions & 0 deletions src/Botonomous/AbstractBot.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ abstract class AbstractBot
protected $dictionary;
protected $commandExtractor;

/**
* @param null $key
* @return mixed
*/
abstract public function getRequest($key = null);

/**
* @return Config
*/
Expand Down
6 changes: 4 additions & 2 deletions src/Botonomous/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Botonomous;

use Botonomous\listener\SlashCommandListener;

/**
* Class Config.
*/
Expand All @@ -27,8 +29,8 @@ class Config extends AbstractConfig
],
'iconURL' => 'YOUR_BOT_ICON_URL_48_BY_48',
'asUser' => true,
// possible values are: slashCommand, event
'listener' => 'slashCommand',
// possible values are: SlashCommandListener::KEY, EventListener::KEY
'listener' => SlashCommandListener::KEY,
// this is used if there is no command has been specified in the message
'defaultCommand' => 'qa',
'commandPrefix' => '/',
Expand Down
24 changes: 15 additions & 9 deletions src/Botonomous/Sender.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@

namespace Botonomous;

use Botonomous\listener\EventListener;
use Botonomous\listener\SlashCommandListener;
use /* @noinspection PhpUndefinedClassInspection */
GuzzleHttp\Psr7\Request;

class Sender extends AbstractSender
{
const SLACK_RESPONSE_TYPE = 'slack';
const SLASH_COMMAND_RESPONSE_TYPE = 'slashCommand';
const JSON_RESPONSE_TYPE = 'json';

private $slackbot;

/**
Expand Down Expand Up @@ -70,11 +76,11 @@ public function send($text, $channel = null, $attachments = null)
$this->getLoggerUtility()->logChat(__METHOD__, $text, $channel);

$responseType = $this->getResponseType();
if ($responseType === 'slack') {
if ($responseType === self::SLACK_RESPONSE_TYPE) {
$this->respondToSlack($data);
} elseif ($responseType === 'slashCommand') {
} elseif ($responseType === self::SLASH_COMMAND_RESPONSE_TYPE) {
$this->respondToSlashCommand($text);
} elseif ($responseType === 'json') {
} elseif ($responseType === self::JSON_RESPONSE_TYPE) {
$this->respondAsJSON($data);
}

Expand Down Expand Up @@ -132,7 +138,7 @@ private function getResponseType()
{
if ($this->getSlackbot()->getRequest('debug') === true
|| $this->getSlackbot()->getRequest('debug') === 'true') {
return 'json';
return self::JSON_RESPONSE_TYPE;
}

// response type in the config is empty, so choose it based on listener type
Expand All @@ -146,12 +152,12 @@ private function getResponseByListenerType(): string
{
$listener = $this->getConfig()->get('listener');
switch ($listener) {
case 'slashCommand':
return 'slashCommand';
case 'event':
return 'slack';
case SlashCommandListener::KEY:
return self::SLASH_COMMAND_RESPONSE_TYPE;
case EventListener::KEY:
return self::SLACK_RESPONSE_TYPE;
default:
return 'slashCommand';
return self::SLASH_COMMAND_RESPONSE_TYPE;
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/Botonomous/listener/AbstractBaseListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ abstract public function extractRequest();
*/
abstract public function getChannelId(): string;

/**
* @return string
*/
abstract public function getKey(): string;

/**
* @param null|string $key
*
Expand Down
13 changes: 11 additions & 2 deletions src/Botonomous/listener/EventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
*/
class EventListener extends AbstractBaseListener
{
const KEY = 'event';
const MISSING_TOKEN_OR_APP_ID_MESSAGE = 'Token or api_app_id is not provided';
const MISSING_APP_ID_MESSAGE = 'Api app id must be provided';
const MISSING_VERIFICATION_TOKEN_MESSAGE = 'Verification token must be provided';
const MISSING_EVENT_TYPE = 'Event type must be specified';
const MISSING_EVENT_TYPE_MESSAGE = 'Event type must be specified';

private $token;
private $teamId;
Expand Down Expand Up @@ -113,7 +114,7 @@ private function loadEvent()

$request = $request['event'];
if (!isset($request['type'])) {
throw new BotonomousException(self::MISSING_EVENT_TYPE);
throw new BotonomousException(self::MISSING_EVENT_TYPE_MESSAGE);
}

// create the event
Expand Down Expand Up @@ -211,4 +212,12 @@ public function getChannelId(): string
{
return $this->getEvent()->getChannel();
}

/**
* @return string
*/
public function getKey(): string
{
return self::KEY;
}
}
9 changes: 9 additions & 0 deletions src/Botonomous/listener/SlashCommandListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

class SlashCommandListener extends AbstractBaseListener
{
const KEY = 'slashCommand';
const VERIFICATION_TOKEN = 'verificationToken';
const MISSING_TOKEN_MESSAGE = 'Token is missing';
const MISSING_TOKEN_CONFIG_MESSAGE = 'Token must be set in the config';
Expand Down Expand Up @@ -80,4 +81,12 @@ public function getChannelId(): string
{
return $this->getRequest('channel_id');
}

/**
* @return string
*/
public function getKey(): string
{
return self::KEY;
}
}
2 changes: 1 addition & 1 deletion tests/Botonomous/listener/EventListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function testGetEventEmptyEventType()
$eventListener->setRequestUtility($requestUtility);

$this->expectException('\Exception');
$this->expectExceptionMessage(EventListener::MISSING_EVENT_TYPE);
$this->expectExceptionMessage(EventListener::MISSING_EVENT_TYPE_MESSAGE);

$eventListener->getEvent();
}
Expand Down

0 comments on commit e31d041

Please sign in to comment.