From 2066ab41d14481054de1fa1325e5f2b38cfa13f8 Mon Sep 17 00:00:00 2001 From: Sullivan SENECHAL Date: Wed, 31 Aug 2016 10:36:26 +0200 Subject: [PATCH] Add sticky channel option With this option, all message will be sent to the default channel only. This is useful for dev environment when you want to concentrate all test message to a specific test channel. --- README.md | 1 + src/Client.php | 32 +++++++++++++++++++++++++++++++- tests/ClientFunctionalTest.php | 25 +++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8ecf365..d6b8312 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,7 @@ The default settings are pretty good, but you may wish to set up default behavio Field | Type | Description ----- | ---- | ----------- `channel` | string | The default channel that messages will be sent to +`sticky_channel` | bool | If set to true, all messages will be sent to the default channel only `username` | string | The default username for your bot `icon` | string | The default icon that messages will be sent with, either `:emoji:` or a URL to an image `link_names` | bool | Whether names like `@regan` or `#accounting` should be linked in the message (defaults to false) diff --git a/src/Client.php b/src/Client.php index 75497f4..524c7e2 100644 --- a/src/Client.php +++ b/src/Client.php @@ -21,6 +21,16 @@ class Client */ protected $channel; + /** + * If set to true, all messages will be sent to the default channel, + * even if you specify another one during the runtime. + * + * This is useful for dev environment. + * + * @var bool + */ + protected $sticky_channel = false; + /** * The default username to send messages as. * @@ -95,6 +105,10 @@ public function __construct($endpoint, array $attributes = [], Guzzle $guzzle = $this->setDefaultChannel($attributes['channel']); } + if (isset($attributes['sticky_channel'])) { + $this->setStickyChannel($attributes['sticky_channel']); + } + if (isset($attributes['username'])) { $this->setDefaultUsername($attributes['username']); } @@ -181,6 +195,22 @@ public function setDefaultChannel($channel) $this->channel = $channel; } + /** + * @return bool + */ + public function isStickyChannel() + { + return $this->sticky_channel; + } + + /** + * @param bool $sticky_channel + */ + public function setStickyChannel($sticky_channel) + { + $this->sticky_channel = $sticky_channel; + } + /** * Get the default username messages will be created for. * @@ -385,7 +415,7 @@ public function preparePayload(Message $message) { $payload = [ 'text' => $message->getText(), - 'channel' => $message->getChannel(), + 'channel' => $this->isStickyChannel() ? $this->getDefaultChannel() : $message->getChannel(), 'username' => $message->getUsername(), 'link_names' => $this->getLinkNames() ? 1 : 0, 'unfurl_links' => $this->getUnfurlLinks(), diff --git a/tests/ClientFunctionalTest.php b/tests/ClientFunctionalTest.php index f0fe810..ed9bb24 100644 --- a/tests/ClientFunctionalTest.php +++ b/tests/ClientFunctionalTest.php @@ -317,6 +317,31 @@ public function testMessageWithAttachmentsAndActions() $this->assertEquals($expectedHttpData, $payload); } + public function testMessageWithStickyChannel() + { + $expectedHttpData = [ + 'username' => 'Archer', + 'channel' => 'test', + 'text' => 'Message', + 'link_names' => 0, + 'unfurl_links' => false, + 'unfurl_media' => true, + 'mrkdwn' => true, + 'attachments' => [], + ]; + + $client = new Client('http://fake.endpoint', [ + 'channel' => 'test', + 'sticky_channel' => true, + ]); + + $message = $client->to('@regan')->from('Archer')->setText('Message'); + + $payload = $client->preparePayload($message); + + $this->assertEquals($expectedHttpData, $payload); + } + public function testBadEncodingThrowsException() { $client = $this->getNetworkStubbedClient();