Skip to content

Commit 28fbc34

Browse files
committed
decouple Message from Client. Closes #15, fixes maknz#70
1 parent 53a102b commit 28fbc34

5 files changed

Lines changed: 171 additions & 155 deletions

File tree

README.md

Lines changed: 72 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ A simple PHP package for sending messages to [Slack](https://slack.com)
1010
with [incoming webhooks](https://my.slack.com/services/new/incoming-webhook),
1111
focused on ease-of-use and elegant syntax.
1212

13-
**supports:** PHP `7.0`, `7.1`, `7.2`
13+
**supports:** PHP `7.1`, `7.2`, `7.3`, `7.4` or `8.0`
1414
**require:** `guzzlehttp/guzzle` any of versions `~6.0|~5.0|~4.0`
1515

1616
> This is the fork of popular, great, but abandoned package [`maknz/slack`](https://github.com/maknz/slack)
@@ -60,10 +60,10 @@ $client = new Maknz\Slack\Client('https://hooks.slack.com/...');
6060
// use response_type (in_channel | ephemeral) to denote whether the message will be visible
6161
// to others in the channel.
6262
$settings = [
63-
'username' => 'Cyril',
64-
'channel' => '#accounting',
65-
'reponse_type' => 'in_channel',
66-
'link_names' => true
63+
'username' => 'Cyril',
64+
'channel' => '#accounting',
65+
'reponse_type' => 'in_channel',
66+
'link_names' => true
6767
];
6868

6969
$client = new Maknz\Slack\Client('https://hooks.slack.com/...', $settings);
@@ -121,66 +121,69 @@ $client->to('#accounting')->withIcon('http://example.com/accounting.png')->send(
121121

122122
```php
123123
$client->to('#operations')->attach([
124-
'fallback' => 'Server health: good',
125-
'text' => 'Server health: good',
126-
'color' => 'danger',
124+
'fallback' => 'Server health: good',
125+
'text' => 'Server health: good',
126+
'color' => 'danger',
127127
])->send('New alert from the monitoring system'); // no message, but can be provided if you'd like
128128
```
129129

130130
#### Send an attachment with fields ([preview](https://goo.gl/264mhU))
131131

132132
```php
133133
$client->to('#operations')->attach([
134-
'fallback' => 'Current server stats',
135-
'text' => 'Current server stats',
136-
'color' => 'danger',
137-
'fields' => [
138-
[
139-
'title' => 'CPU usage',
140-
'value' => '90%',
141-
'short' => true // whether the field is short enough to sit side-by-side other fields, defaults to false
142-
],
143-
[
144-
'title' => 'RAM usage',
145-
'value' => '2.5GB of 4GB',
146-
'short' => true
147-
]
148-
]
134+
'fallback' => 'Current server stats',
135+
'text' => 'Current server stats',
136+
'color' => 'danger',
137+
'fields' => [
138+
[
139+
'title' => 'CPU usage',
140+
'value' => '90%',
141+
'short' => true // whether the field is short enough to sit side-by-side other fields, defaults to false
142+
],
143+
[
144+
'title' => 'RAM usage',
145+
'value' => '2.5GB of 4GB',
146+
'short' => true
147+
]
148+
]
149149
])->send('New alert from the monitoring system'); // no message, but can be provided if you'd like
150150
```
151151

152152
#### Send an attachment with an author ([preview](https://goo.gl/CKd1zJ))
153153

154154
```php
155155
$client->to('@regan')->attach([
156-
'fallback' => 'Keep up the great work! I really love how the app works.',
157-
'text' => 'Keep up the great work! I really love how the app works.',
158-
'author_name' => 'Jane Appleseed',
159-
'author_link' => 'https://yourapp.com/feedback/5874601',
160-
'author_icon' => 'https://static.pexels.com/photos/61120/pexels-photo-61120-large.jpeg'
156+
'fallback' => 'Keep up the great work! I really love how the app works.',
157+
'text' => 'Keep up the great work! I really love how the app works.',
158+
'author_name' => 'Jane Appleseed',
159+
'author_link' => 'https://yourapp.com/feedback/5874601',
160+
'author_icon' => 'https://static.pexels.com/photos/61120/pexels-photo-61120-large.jpeg'
161161
])->send('New user feedback');
162162
```
163163

164164
#### Using blocks ([Block Kit](https://api.slack.com/block-kit))
165165

166166
```php
167-
$client->to('@regan')->withBlock([
168-
'type' => 'section',
169-
'text' => 'Do you love the app?'
170-
])->withBlock([
171-
'type' => 'actions',
172-
'elements' => [[
173-
'type' => 'button',
174-
'text' => 'Love it',
175-
'style' => 'primary',
176-
'action_id' => 'love',
177-
], [
178-
'type' => 'button',
179-
'text' => 'Hate it',
180-
'style' => 'danger',
181-
'action_id' => 'hate',
182-
],
183-
])->send('Notification fallback message');
167+
$client->to('@regan')
168+
->withBlock([
169+
'type' => 'section',
170+
'text' => 'Do you love the app?'
171+
])
172+
->withBlock([
173+
'type' => 'actions',
174+
'elements' => [[
175+
'type' => 'button',
176+
'text' => 'Love it',
177+
'style' => 'primary',
178+
'action_id' => 'love',
179+
], [
180+
'type' => 'button',
181+
'text' => 'Hate it',
182+
'style' => 'danger',
183+
'action_id' => 'hate',
184+
],]
185+
])
186+
->send('Notification fallback message');
184187
```
185188

186189
## Advanced usage
@@ -201,11 +204,11 @@ $client->to('#general')->enableMarkdown()->send('Enable _markdown_ just for this
201204

202205
```php
203206
$client->to('#operations')->attach([
204-
'fallback' => 'It is all broken, man',
205-
'text' => 'It is _all_ broken, man',
206-
'pretext' => 'From user: *JimBob*',
207-
'color' => 'danger',
208-
'mrkdwn_in' => ['pretext', 'text']
207+
'fallback' => 'It is all broken, man',
208+
'text' => 'It is _all_ broken, man',
209+
'pretext' => 'From user: *JimBob*',
210+
'color' => 'danger',
211+
'mrkdwn_in' => ['pretext', 'text']
209212
])->send('New alert from the monitoring system');
210213
```
211214

@@ -219,10 +222,12 @@ $client->to('@regan')->send('I am sending this implicitly');
219222

220223
// Explicitly
221224
$message = $client->createMessage();
225+
$message
226+
->to('@regan')
227+
->setText('I am sending this explicitly')
228+
;
222229

223-
$message->to('@regan')->setText('I am sending this explicitly');
224-
225-
$message->send();
230+
$client->send($message);
226231
```
227232

228233
### Attachments
@@ -231,34 +236,35 @@ When using attachments, the easiest way is to provide an array of data as shown
231236

232237
```php
233238
$attachment = new Attachment([
234-
'fallback' => 'Some fallback text',
235-
'text' => 'The attachment text'
239+
'fallback' => 'Some fallback text',
240+
'text' => 'The attachment text'
236241
]);
237242

238243
// Explicitly create a message from the client
239244
// rather than using the magic passthrough methods
240245
$message = $client->createMessage();
241246

242247
$message->attach($attachment);
243-
244248
// Explicitly set the message text rather than
245249
// implicitly through the send method
246-
$message->setText('Hello world')->send();
250+
$message->setText('Hello world');
251+
252+
$client->send($message);
247253
```
248254

249255
Each attachment field is also an object, an AttachmentField. They can be used as well instead of their data in array form:
250256

251257
```php
252258
$attachment = new Attachment([
253-
'fallback' => 'Some fallback text',
254-
'text' => 'The attachment text',
255-
'fields' => [
256-
new AttachmentField([
257-
'title' => 'A title',
258-
'value' => 'A value',
259-
'short' => true
260-
])
261-
]
259+
'fallback' => 'Some fallback text',
260+
'text' => 'The attachment text',
261+
'fields' => [
262+
new AttachmentField([
263+
'title' => 'A title',
264+
'value' => 'A value',
265+
'short' => true
266+
])
267+
]
262268
]);
263269
```
264270

src/Client.php

Lines changed: 83 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
use RuntimeException;
66

77
/**
8-
* @method Message to(string $channel)
9-
* @method Message send(string $text = null)
8+
* @mixin Message
109
*/
1110
class Client
1211
{
@@ -108,6 +107,11 @@ class Client
108107
'markdown_in_attachments' => 'setMarkdownInAttachments',
109108
];
110109

110+
/**
111+
* @var Message
112+
*/
113+
protected $message;
114+
111115
/**
112116
* Instantiate a new Client.
113117
*
@@ -139,18 +143,48 @@ private static function getOptionSetter(string $option)
139143
return static::$optionSetter[$option] ?? null;
140144
}
141145

146+
/**
147+
* @param array $options
148+
*
149+
* @return \Maknz\Slack\Client
150+
*/
151+
public function setOptions(array $options)
152+
{
153+
foreach ($options as $option => $value) {
154+
$this->setOption($option, $value);
155+
}
156+
157+
return $this;
158+
}
159+
160+
/**
161+
* @param $option
162+
* @param $value
163+
*/
164+
public function setOption($option, $value)
165+
{
166+
$setter = self::getOptionSetter($option);
167+
if ($setter !== null) {
168+
$this->$setter($value);
169+
}
170+
}
171+
142172
/**
143173
* Pass any unhandled methods through to a new Message
144174
* instance.
145175
*
146176
* @param string $name The name of the method
147177
* @param array $arguments The method arguments
148178
*
149-
* @return \Maknz\Slack\Message
179+
* @return $this|mixed
150180
*/
151181
public function __call($name, $arguments)
152182
{
153-
return call_user_func_array([$this->createMessage(), $name], $arguments);
183+
/** @var Message $message */
184+
$message = $this->getOrCreateMessage();
185+
$value = $message->$name(...$arguments);
186+
187+
return strpos($name, 'get') === 0 ? $value : $this;
154188
}
155189

156190
/**
@@ -381,6 +415,14 @@ public function setMarkdownInAttachments(array $fields)
381415
$this->markdown_in_attachments = $fields;
382416
}
383417

418+
/**
419+
* @return Message
420+
*/
421+
public function getMessage()
422+
{
423+
return $this->message;
424+
}
425+
384426
/**
385427
* Create a new message with defaults.
386428
*
@@ -403,9 +445,19 @@ public function createMessage()
403445
return $message;
404446
}
405447

448+
/**
449+
* @return Message
450+
*/
451+
protected function getOrCreateMessage()
452+
{
453+
return $this->message ?? $this->message = $this->createMessage();
454+
}
455+
406456
/**
407457
* Send a message.
408458
*
459+
* @internal will become protected
460+
*
409461
* @param \Maknz\Slack\Message $message
410462
*
411463
* @throws \RuntimeException
@@ -423,6 +475,33 @@ public function sendMessage(Message $message)
423475
$this->guzzle->post($this->endpoint, ['body' => $encoded]);
424476
}
425477

478+
/**
479+
* Send the message.
480+
*
481+
* @param string|\Maknz\Slack\Message $text The text to send
482+
*
483+
* @return \Maknz\Slack\Message
484+
*
485+
* @throws \RuntimeException
486+
*/
487+
public function send($text = null)
488+
{
489+
if ($text instanceof Message) {
490+
$message = $text;
491+
} else {
492+
$message = $this->getOrCreateMessage();
493+
if ($text !== null) {
494+
$message->setText($text);
495+
}
496+
}
497+
498+
$this->sendMessage($message);
499+
500+
$this->message = null;
501+
502+
return $message;
503+
}
504+
426505
/**
427506
* Prepares the payload to be sent to the webhook.
428507
*
@@ -473,30 +552,4 @@ protected function getAttachmentsAsArrays(Message $message)
473552

474553
return $attachments;
475554
}
476-
477-
/**
478-
* @param array $options
479-
*
480-
* @return \Maknz\Slack\Client
481-
*/
482-
public function setOptions(array $options)
483-
{
484-
foreach ($options as $option => $value) {
485-
$this->setOption($option, $value);
486-
}
487-
488-
return $this;
489-
}
490-
491-
/**
492-
* @param $option
493-
* @param $value
494-
*/
495-
public function setOption($option, $value)
496-
{
497-
$setter = self::getOptionSetter($option);
498-
if ($setter !== null) {
499-
$this->$setter($value);
500-
}
501-
}
502555
}

0 commit comments

Comments
 (0)