From 1aefdb204c6b7082291749b544c1749859810182 Mon Sep 17 00:00:00 2001 From: makinde2013 Date: Tue, 15 Oct 2019 19:46:56 +0100 Subject: [PATCH 1/9] Allow user specify webhook for instance, as well as other smooth configuration --- README.md | 6 ++++++ src/Slack.php | 45 +++++++++++++++++++++++++++++++++++++-------- 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 4636740..f7f1dec 100644 --- a/README.md +++ b/README.md @@ -118,6 +118,12 @@ class User extends Model \Slack::to(User::where('verified', true))->send('Sending message to all verified users!'); ``` +- Send message by specifying webhook: + +```php +\Slack::to('#finance')->webhook('https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX')->send('Hey, finance channel! A new order was created just now!'); +``` + - And more... This package is both under development and underdeveloped. diff --git a/src/Slack.php b/src/Slack.php index 58ec015..1ffa599 100644 --- a/src/Slack.php +++ b/src/Slack.php @@ -35,14 +35,43 @@ class Slack */ private $config; - public function __construct(array $config) - { - $this->anonymousNotifiable = Notification::route('slack', $config['slack_webhook_url']); - $this->recipients = [$config['default_channel']]; - $this->from = $config['application_name']; - $this->image = $config['application_image']; - $this->config = $config; - } + public function __construct( array $config = [] ) { + $config = $this->mergeConfig($config); + $this->anonymousNotifiable = Notification::route( 'slack', $config['slack_webhook_url'] ); + $this->recipients = [ $config['default_channel'] ]; + $this->from = $config['application_name']; + $this->image = $config['application_image']; + $this->config = $config; + } + + /**Use config variables specified by user + * use default variables when not specified + * @param $config + * + * @return mixed + */ + private function mergeConfig($config) { + $defaultConfig = config( 'laravel-slack' ); + + foreach ( $defaultConfig as $key => $value ) { + if ( ! array_key_exists( $key, $config ) ) { + $config[ $key ] = $value; + } + } + + return $config; + } + + /**Allows user specify webhook to use + * for current instance + * @param string $url + * + * @return $this + */ + public function webhook(string $url) { + $this->anonymousNotifiable = Notification::route( 'slack', $url ); + return $this; + } /** * Set the recipients of the message. From d9c32c164af7f3e74896c7705d91dc3027cca2c6 Mon Sep 17 00:00:00 2001 From: makinde2013 Date: Tue, 15 Oct 2019 19:53:15 +0100 Subject: [PATCH 2/9] style code properly --- src/Slack.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Slack.php b/src/Slack.php index 1ffa599..d6d1e48 100644 --- a/src/Slack.php +++ b/src/Slack.php @@ -35,7 +35,8 @@ class Slack */ private $config; - public function __construct( array $config = [] ) { + public function __construct( array $config = [] ) + { $config = $this->mergeConfig($config); $this->anonymousNotifiable = Notification::route( 'slack', $config['slack_webhook_url'] ); $this->recipients = [ $config['default_channel'] ]; @@ -50,7 +51,8 @@ public function __construct( array $config = [] ) { * * @return mixed */ - private function mergeConfig($config) { + private function mergeConfig(array $config) : array + { $defaultConfig = config( 'laravel-slack' ); foreach ( $defaultConfig as $key => $value ) { @@ -68,7 +70,8 @@ private function mergeConfig($config) { * * @return $this */ - public function webhook(string $url) { + public function webhook(string $url) : self + { $this->anonymousNotifiable = Notification::route( 'slack', $url ); return $this; } From 8f108f76ac590b4c7ad005917102a5d9c3b60c09 Mon Sep 17 00:00:00 2001 From: makinde2013 Date: Tue, 15 Oct 2019 20:18:25 +0100 Subject: [PATCH 3/9] Added tests --- composer.json | 3 +- src/Slack.php | 102 ++++++++++++++++++++------------------ tests/SendMessageTest.php | 30 +++++++++++ 3 files changed, 87 insertions(+), 48 deletions(-) diff --git a/composer.json b/composer.json index fb4f457..b8d9ee4 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,8 @@ "php": ">=7.0.0", "guzzlehttp/guzzle": "^6.3", "illuminate/notifications": "^5.5", - "illuminate/support": "^5.5" + "illuminate/support": "^5.5", + "squizlabs/php_codesniffer": "^3.5" }, "require-dev": { "orchestra/testbench": "~3.5", diff --git a/src/Slack.php b/src/Slack.php index d6d1e48..3e90c32 100644 --- a/src/Slack.php +++ b/src/Slack.php @@ -35,51 +35,57 @@ class Slack */ private $config; - public function __construct( array $config = [] ) - { - $config = $this->mergeConfig($config); - $this->anonymousNotifiable = Notification::route( 'slack', $config['slack_webhook_url'] ); - $this->recipients = [ $config['default_channel'] ]; - $this->from = $config['application_name']; - $this->image = $config['application_image']; - $this->config = $config; - } - - /**Use config variables specified by user - * use default variables when not specified - * @param $config - * - * @return mixed - */ - private function mergeConfig(array $config) : array - { - $defaultConfig = config( 'laravel-slack' ); - - foreach ( $defaultConfig as $key => $value ) { - if ( ! array_key_exists( $key, $config ) ) { - $config[ $key ] = $value; - } - } - - return $config; - } - - /**Allows user specify webhook to use - * for current instance - * @param string $url - * - * @return $this - */ - public function webhook(string $url) : self - { - $this->anonymousNotifiable = Notification::route( 'slack', $url ); - return $this; - } + public function __construct( array $config = [] ) + { + $config = $this->mergeConfig($config); + $this->anonymousNotifiable = Notification::route('slack', $config['slack_webhook_url']); + $this->recipients = [ $config['default_channel'] ]; + $this->from = $config['application_name']; + $this->image = $config['application_image']; + $this->config = $config; + } + + /** + * + * Use config variables specified by user + * use default variables when not specified + * + * @param $config + * + * @return mixed + */ + private function mergeConfig(array $config) : array + { + $defaultConfig = config('laravel-slack'); + + foreach ( $defaultConfig as $key => $value ) { + if (! array_key_exists($key, $config) ) { + $config[ $key ] = $value; + } + } + + return $config; + } + + /** + * + * Allows user specify webhook to use + * for current instance + * + * @param string $url + * + * @return $this + */ + public function webhook(string $url) : self + { + $this->anonymousNotifiable = Notification::route('slack', $url); + return $this; + } /** * Set the recipients of the message. * - * @param object|array|string $recipient + * @param object|array|string $recipient * * @return $this */ @@ -91,13 +97,15 @@ public function to($recipient): self $recipients = is_array($recipient) ? $recipient : func_get_args(); - $this->recipients = array_map(function ($recipient) { - if (is_object($recipient)) { - return $recipient->slack_channel; - } + $this->recipients = array_map( + function ($recipient) { + if (is_object($recipient)) { + return $recipient->slack_channel; + } - return $recipient; - }, $recipients); + return $recipient; + }, $recipients + ); return $this; } diff --git a/tests/SendMessageTest.php b/tests/SendMessageTest.php index 4d54dfe..42d4d8d 100644 --- a/tests/SendMessageTest.php +++ b/tests/SendMessageTest.php @@ -6,6 +6,7 @@ use Illuminate\Notifications\AnonymousNotifiable; use Illuminate\Notifications\Messages\SlackMessage; use Pressutto\LaravelSlack\Notifications\SimpleSlack; +use Pressutto\LaravelSlack\Slack; class SendMessageTest extends TestCase { @@ -21,6 +22,35 @@ public function testSendMessageToAChannel() $this->assertEquals('RANDOM', $slackMessageSent->content); } + public function testSendMessageToAChannelWithSpecifiedWebHook() + { + $notification = Notification::fake(); + + \Slack::to('#random')->webhook('https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX')->send('RANDOM'); + + $notification->assertSentTo(new AnonymousNotifiable(), SimpleSlack::class, 1); + $slackMessageSent = $notification->sent(new AnonymousNotifiable(), SimpleSlack::class)->first()->toSlack(); + $this->assertEquals('#random', $slackMessageSent->channel); + $this->assertEquals('RANDOM', $slackMessageSent->content); + } + + public function testSendMessageToAUserWithSpecifiedConfig() + { + $notification = Notification::fake(); + $config = array( + 'slack_webhook_url' => 'https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX' + ); + + $slack = new Slack($config); + + $slack->to('#random')->send('RANDOM'); + + $notification->assertSentTo(new AnonymousNotifiable(), SimpleSlack::class, 1); + $slackMessageSent = $notification->sent(new AnonymousNotifiable(), SimpleSlack::class)->first()->toSlack(); + $this->assertEquals('#random', $slackMessageSent->channel); + $this->assertEquals('RANDOM', $slackMessageSent->content); + } + public function testSendMessageToAnUser() { $notification = Notification::fake(); From 3f3f73c4f0701a477cdb0228785ed4f528cf35f2 Mon Sep 17 00:00:00 2001 From: makinde2013 Date: Tue, 15 Oct 2019 20:19:12 +0100 Subject: [PATCH 4/9] Added tests --- composer.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/composer.json b/composer.json index b8d9ee4..fb4f457 100644 --- a/composer.json +++ b/composer.json @@ -14,8 +14,7 @@ "php": ">=7.0.0", "guzzlehttp/guzzle": "^6.3", "illuminate/notifications": "^5.5", - "illuminate/support": "^5.5", - "squizlabs/php_codesniffer": "^3.5" + "illuminate/support": "^5.5" }, "require-dev": { "orchestra/testbench": "~3.5", From c38b383eb1bf5f097c330141fdcb0205c0b74abe Mon Sep 17 00:00:00 2001 From: makinde2013 Date: Tue, 15 Oct 2019 20:26:44 +0100 Subject: [PATCH 5/9] another attempt to meet styleci standard --- src/Slack.php | 20 +++++++++----------- tests/SendMessageTest.php | 8 ++++---- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/src/Slack.php b/src/Slack.php index 3e90c32..8c3d741 100644 --- a/src/Slack.php +++ b/src/Slack.php @@ -35,18 +35,17 @@ class Slack */ private $config; - public function __construct( array $config = [] ) + public function __construct(array $config = []) { $config = $this->mergeConfig($config); $this->anonymousNotifiable = Notification::route('slack', $config['slack_webhook_url']); - $this->recipients = [ $config['default_channel'] ]; - $this->from = $config['application_name']; - $this->image = $config['application_image']; - $this->config = $config; + $this->recipients = [ $config['default_channel'] ]; + $this->from = $config['application_name']; + $this->image = $config['application_image']; + $this->config = $config; } /** - * * Use config variables specified by user * use default variables when not specified * @@ -58,9 +57,9 @@ private function mergeConfig(array $config) : array { $defaultConfig = config('laravel-slack'); - foreach ( $defaultConfig as $key => $value ) { - if (! array_key_exists($key, $config) ) { - $config[ $key ] = $value; + foreach ($defaultConfig as $key => $value) { + if (! array_key_exists($key, $config)) { + $config[$key] = $value; } } @@ -68,9 +67,8 @@ private function mergeConfig(array $config) : array } /** - * * Allows user specify webhook to use - * for current instance + * for current instance. * * @param string $url * diff --git a/tests/SendMessageTest.php b/tests/SendMessageTest.php index 42d4d8d..b7ea895 100644 --- a/tests/SendMessageTest.php +++ b/tests/SendMessageTest.php @@ -2,11 +2,11 @@ namespace Tests; +use Pressutto\LaravelSlack\Slack; use Illuminate\Support\Facades\Notification; use Illuminate\Notifications\AnonymousNotifiable; use Illuminate\Notifications\Messages\SlackMessage; use Pressutto\LaravelSlack\Notifications\SimpleSlack; -use Pressutto\LaravelSlack\Slack; class SendMessageTest extends TestCase { @@ -37,9 +37,9 @@ public function testSendMessageToAChannelWithSpecifiedWebHook() public function testSendMessageToAUserWithSpecifiedConfig() { $notification = Notification::fake(); - $config = array( - 'slack_webhook_url' => 'https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX' - ); + $config = [ + 'slack_webhook_url' => 'https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX', + ]; $slack = new Slack($config); From 5c3f859b35426282a398ff0395b2247dcf376510 Mon Sep 17 00:00:00 2001 From: makinde2013 Date: Tue, 15 Oct 2019 20:28:20 +0100 Subject: [PATCH 6/9] another attempt to meet styleci standard --- src/Slack.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Slack.php b/src/Slack.php index 8c3d741..c39a6ad 100644 --- a/src/Slack.php +++ b/src/Slack.php @@ -39,7 +39,7 @@ public function __construct(array $config = []) { $config = $this->mergeConfig($config); $this->anonymousNotifiable = Notification::route('slack', $config['slack_webhook_url']); - $this->recipients = [ $config['default_channel'] ]; + $this->recipients = [$config['default_channel']]; $this->from = $config['application_name']; $this->image = $config['application_image']; $this->config = $config; @@ -47,7 +47,7 @@ public function __construct(array $config = []) /** * Use config variables specified by user - * use default variables when not specified + * use default variables when not specified. * * @param $config * @@ -77,6 +77,7 @@ private function mergeConfig(array $config) : array public function webhook(string $url) : self { $this->anonymousNotifiable = Notification::route('slack', $url); + return $this; } From e2505189da8af85163a05ac36266e7b7d23c2cf2 Mon Sep 17 00:00:00 2001 From: makinde2013 Date: Thu, 17 Oct 2019 23:47:53 +0100 Subject: [PATCH 7/9] refactor construct --- src/Slack.php | 24 +----------------------- tests/SendMessageTest.php | 9 ++------- 2 files changed, 3 insertions(+), 30 deletions(-) diff --git a/src/Slack.php b/src/Slack.php index c39a6ad..4639f2e 100644 --- a/src/Slack.php +++ b/src/Slack.php @@ -35,9 +35,8 @@ class Slack */ private $config; - public function __construct(array $config = []) + public function __construct(array $config) { - $config = $this->mergeConfig($config); $this->anonymousNotifiable = Notification::route('slack', $config['slack_webhook_url']); $this->recipients = [$config['default_channel']]; $this->from = $config['application_name']; @@ -45,27 +44,6 @@ public function __construct(array $config = []) $this->config = $config; } - /** - * Use config variables specified by user - * use default variables when not specified. - * - * @param $config - * - * @return mixed - */ - private function mergeConfig(array $config) : array - { - $defaultConfig = config('laravel-slack'); - - foreach ($defaultConfig as $key => $value) { - if (! array_key_exists($key, $config)) { - $config[$key] = $value; - } - } - - return $config; - } - /** * Allows user specify webhook to use * for current instance. diff --git a/tests/SendMessageTest.php b/tests/SendMessageTest.php index b7ea895..8495b2d 100644 --- a/tests/SendMessageTest.php +++ b/tests/SendMessageTest.php @@ -37,17 +37,12 @@ public function testSendMessageToAChannelWithSpecifiedWebHook() public function testSendMessageToAUserWithSpecifiedConfig() { $notification = Notification::fake(); - $config = [ - 'slack_webhook_url' => 'https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX', - ]; - $slack = new Slack($config); - - $slack->to('#random')->send('RANDOM'); + \Pressutto\LaravelSlack\Facades\Slack::to('#fashion')->webhook('https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX')->send('RANDOM'); $notification->assertSentTo(new AnonymousNotifiable(), SimpleSlack::class, 1); $slackMessageSent = $notification->sent(new AnonymousNotifiable(), SimpleSlack::class)->first()->toSlack(); - $this->assertEquals('#random', $slackMessageSent->channel); + $this->assertEquals('#fashion', $slackMessageSent->channel); $this->assertEquals('RANDOM', $slackMessageSent->content); } From 7a6e6b33b2a1acdb944d9b184dc1880681268150 Mon Sep 17 00:00:00 2001 From: makinde2013 Date: Thu, 17 Oct 2019 23:58:20 +0100 Subject: [PATCH 8/9] remove unused use statement --- tests/SendMessageTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/SendMessageTest.php b/tests/SendMessageTest.php index 8495b2d..aa14d27 100644 --- a/tests/SendMessageTest.php +++ b/tests/SendMessageTest.php @@ -2,7 +2,6 @@ namespace Tests; -use Pressutto\LaravelSlack\Slack; use Illuminate\Support\Facades\Notification; use Illuminate\Notifications\AnonymousNotifiable; use Illuminate\Notifications\Messages\SlackMessage; From 92d88de84c8d408ba24da2ebd9cc3a03e40162d8 Mon Sep 17 00:00:00 2001 From: Zacchaeus Bolaji Date: Thu, 22 Apr 2021 11:57:33 +0100 Subject: [PATCH 9/9] Update Slack.php --- src/Slack.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Slack.php b/src/Slack.php index dc46a47..bc1f2f4 100644 --- a/src/Slack.php +++ b/src/Slack.php @@ -52,7 +52,7 @@ public function __construct(array $config) * * @return $this */ - public function webhook(string $url) : self + public function webhook(string $url): self { $this->anonymousNotifiable = Notification::route('slack', $url);