From ff00e8841736a6e20357199712c21ff64a5a71a0 Mon Sep 17 00:00:00 2001 From: Deon George Date: Sun, 4 Sep 2022 10:28:39 +1000 Subject: [PATCH] Options code tidyup --- src/Options/Base.php | 24 +++++------------------- src/Options/BlockSuggestion.php | 2 +- src/Options/Factory.php | 5 +++-- src/Options/InteractiveMessage.php | 23 +---------------------- src/Options/Unknown.php | 19 +++++++++++++++++++ 5 files changed, 29 insertions(+), 44 deletions(-) diff --git a/src/Options/Base.php b/src/Options/Base.php index 9740a05..09e3176 100644 --- a/src/Options/Base.php +++ b/src/Options/Base.php @@ -39,14 +39,12 @@ abstract class Base extends SlackBase public function __get(string $key) { switch ($key) { - case 'team_id': - return object_get($this->_data,'team.id'); case 'channel_id': return object_get($this->_data,'channel.id'); - case 'enterprise_id': return object_get($this->_data,'team.'.$key); - + case 'team_id': + return object_get($this->_data,'team.id'); case 'user_id': return object_get($this->_data,'user.id'); @@ -59,23 +57,11 @@ abstract class Base extends SlackBase } /** - * Interactive messages can return their output in the incoming HTTP post + * Interactive messages can return their output in the incoming HTTP post. + * This function should be overwritten in the parent class, and finish by calling return Message::blank(); * * @return Message * @throws \Exception */ - public function respond(): Message - { - Log::info(sprintf('%s:Interactive Option - Callback [%s] Name [%s] Value [%s]',self::LOGKEY,$this->callback_id,$this->name,$this->value),['m'=>__METHOD__]); - - if (preg_match('/^(.*)\|([0-9]+)/',$this->callback_id)) { - [$action,$id] = explode('|',$this->callback_id,2); - - } else { - // If we get here, its an action that we dont know about. - Log::notice(sprintf('%s:Unhandled CALLBACK [%s]',self::LOGKEY,$this->callback_id),['m'=>__METHOD__]); - } - - return Message::blank(); - } + abstract public function respond(): Message; } \ No newline at end of file diff --git a/src/Options/BlockSuggestion.php b/src/Options/BlockSuggestion.php index 5892c0a..a117d7a 100644 --- a/src/Options/BlockSuggestion.php +++ b/src/Options/BlockSuggestion.php @@ -126,7 +126,7 @@ use Slack\Message; ) ) */ -class BlockSuggestion extends Base +abstract class BlockSuggestion extends Base { protected const LOGKEY = 'OIM'; diff --git a/src/Options/Factory.php b/src/Options/Factory.php index 9a7df23..f09c64d 100644 --- a/src/Options/Factory.php +++ b/src/Options/Factory.php @@ -2,7 +2,6 @@ namespace Slack\Options; -use Illuminate\Http\Request; use Illuminate\Support\Arr; use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\Log; @@ -26,13 +25,15 @@ class Factory { * @param string $type * @param array $request * @return Base + * @note Options classes must be defined in the calling application and extend these self::map abstract classes + * otherwise errors will be thrown */ public static function create(string $type,array $request) { $class = Arr::get(config('slack.options',self::map),$type,Unknown::class); Log::debug(sprintf('%s:Working out Interactive Options Event Class for [%s] as [%s]',static::LOGKEY,$type,$class),['m'=>__METHOD__]); - if (App::environment() == 'dev') + if (App::environment() == 'local') file_put_contents('/tmp/option.'.$type,print_r(json_decode($request->input('payload')),TRUE)); return new $class($request); diff --git a/src/Options/InteractiveMessage.php b/src/Options/InteractiveMessage.php index be5fc19..8d7b8a1 100644 --- a/src/Options/InteractiveMessage.php +++ b/src/Options/InteractiveMessage.php @@ -33,7 +33,7 @@ use Slack\Message; * [attachment_id] => 3 * [token] => Oow8S2EFvrZoS9z8N4nwf9Jo */ -class InteractiveMessage extends Base +abstract class InteractiveMessage extends Base { protected const LOGKEY = 'OIM'; @@ -49,25 +49,4 @@ class InteractiveMessage extends Base return parent::__get($key); } } - - /** - * Interactive messages can return their output in the incoming HTTP post - * - * @return Message - * @throws \Exception - */ - public function respond(): Message - { - Log::info(sprintf('%s:Interactive Option - Callback [%s] Name [%s] Value [%s]',static::LOGKEY,$this->callback_id,$this->name,$this->value),['m'=>__METHOD__]); - - if (preg_match('/^(.*)\|([0-9]+)/',$this->callback_id)) { - [$action,$id] = explode('|',$this->callback_id,2); - - } else { - // If we get here, its an action that we dont know about. - Log::notice(sprintf('%s:Unhandled CALLBACK [%s]',static::LOGKEY,$this->callback_id),['m'=>__METHOD__]); - } - - return (new Message)->blank(); - } } \ No newline at end of file diff --git a/src/Options/Unknown.php b/src/Options/Unknown.php index ad732fa..30329a6 100644 --- a/src/Options/Unknown.php +++ b/src/Options/Unknown.php @@ -4,6 +4,8 @@ namespace Slack\Options; use Illuminate\Support\Facades\Log; +use Slack\Message; + /** * Catch all unknown slack event that we havent specifically programmed for. * @@ -11,10 +13,27 @@ use Illuminate\Support\Facades\Log; */ final class Unknown extends Base { + protected const LOGKEY = 'OU-'; + public function __construct(array $request) { Log::notice(sprintf('SOU:UNKNOWN Slack Interaction Option received [%s]',get_class($this)),['m'=>__METHOD__]); parent::__construct($request); } + + /** + * Interactive messages can return their output in the incoming HTTP post + * + * @return Message + * @throws \Exception + */ + public function respond(): Message + { + Log::info(sprintf('%s:Unknown Option - Callback [%s] Name [%s] Value [%s]',static::LOGKEY,$this->callback_id,$this->name,$this->value),['m'=>__METHOD__]); + + Log::notice(sprintf('%s:Unhandled CALLBACK [%s]',static::LOGKEY,$this->callback_id),['m'=>__METHOD__]); + + return Message::blank(); + } } \ No newline at end of file