diff --git a/src/Client.php b/src/Client.php index 75497f4..302bb60 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->channel : $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();