Skip to content

Commit

Permalink
Refactor public API (#7)
Browse files Browse the repository at this point in the history
* Refactor public API

* Rename setFromArray method name to more obvious equivalent

* Protect class properties

* Don't return ResponseInterface to higher levels, add get prefix to accessor methods, remove url accessor
  • Loading branch information
antonkomarev authored Feb 22, 2020
1 parent 3baf41e commit 27da688
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 134 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class TaskCompleted extends Notification

public function toRocketChat($notifiable): RocketChatMessage
{
return RocketChatMessage::create('Test message')
return RocketChatMessage::make('Test message')
->to('channel_name') // optional if set in config
->from('webhook_token'); // optional if set in config
}
Expand All @@ -101,7 +101,7 @@ public function routeNotificationForRocketChat(): string

`alias()`: This will cause the message’s name to appear as the given alias, but your username will still display.

`emoji()`: This will make the avatar on this message be an emoji. (e.g. ":see_no_evil:")
`emoji()`: This will make the avatar on this message be an emoji. (e.g. ':see_no_evil:')

`avatar()`: This will make the avatar use the provided image url.

Expand All @@ -118,12 +118,12 @@ There are several ways to add one ore more attachments to a message
```php
public function toRocketChat($notifiable)
{
return RocketChatMessage::create("Test message")
return RocketChatMessage::make('Test message')
->to('channel_name') // optional if set in config
->from('webhook_token') // optional if set in config
->attachments([
RocketChatAttachment::create()->imageUrl('test'),
RocketChatAttachment::create(['image_url' => 'test']),
RocketChatAttachment::make()->imageUrl('test'),
RocketChatAttachment::make(['image_url' => 'test']),
new RocketChatAttachment(['image_url' => 'test']),
[
'image_url' => 'test'
Expand Down
4 changes: 2 additions & 2 deletions src/Exceptions/CouldNotSendNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class CouldNotSendNotification extends RuntimeException
*/
public static function missingTo(): self
{
return new static('Notification was not sent. Channel identifier is missing.');
return new static('RocketChat notification was not sent. Channel identifier is missing.');
}

/**
Expand All @@ -27,7 +27,7 @@ public static function missingTo(): self
*/
public static function missingFrom(): self
{
return new static('Notification was not sent. Access token is missing.');
return new static('RocketChat notification was not sent. Access token is missing.');
}

/**
Expand Down
37 changes: 13 additions & 24 deletions src/RocketChat.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace NotificationChannels\RocketChat;

use GuzzleHttp\Client as HttpClient;
use Psr\Http\Message\ResponseInterface;

final class RocketChat
{
Expand Down Expand Up @@ -37,47 +36,37 @@ public function __construct(HttpClient $http, string $url, string $token, ?strin
}

/**
* Returns default channel id or name.
*
* @return string|null
*/
public function channel(): ?string
{
return $this->defaultChannel;
}

/**
* Returns RocketChat base url.
* Returns RocketChat token.
*
* @return string
*/
public function url(): string
public function getToken(): string
{
return $this->url;
return $this->token;
}

/**
* Returns RocketChat token.
* Returns default channel id or name.
*
* @return string
* @return string|null
*/
public function token(): string
public function getDefaultChannel(): ?string
{
return $this->token;
return $this->defaultChannel;
}

/**
* Send a message.
*
* @param string $to
* @param array $message
* @return \Psr\Http\Message\ResponseInterface
* @return void
*/
public function sendMessage(string $to, array $message): ResponseInterface
public function sendMessage(string $to, array $message): void
{
$url = sprintf('%s/hooks/%s', $this->url, $this->token);

return $this->post($url, [
$this->post($url, [
'json' => array_merge($message, [
'channel' => $to,
]),
Expand All @@ -89,10 +78,10 @@ public function sendMessage(string $to, array $message): ResponseInterface
*
* @param string $url
* @param array $options
* @return \Psr\Http\Message\ResponseInterface
* @return void
*/
private function post(string $url, array $options): ResponseInterface
private function post(string $url, array $options): void
{
return $this->http->post($url, $options);
$this->http->post($url, $options);
}
}
104 changes: 53 additions & 51 deletions src/RocketChatAttachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,96 +11,78 @@

class RocketChatAttachment
{
/** @var string The color you want the order on the left side to be, any value background-css supports. */
protected $color = '';
/** @var string|null The color you want the order on the left side to be, any value background-css supports. */
protected $color;

/** @var string The text to display for this attachment, it is different than the message’s text. */
protected $text = '';
/** @var string|null The text to display for this attachment, it is different than the message’s text. */
protected $text;

/** @var string Displays the time next to the text portion. */
protected $timestamp = '';
/** @var string|null Displays the time next to the text portion. */
protected $timestamp;

/** @var string An image that displays to the left of the text, looks better when this is relatively small. */
protected $thumbnailUrl = '';
/** @var string|null An image that displays to the left of the text, looks better when this is relatively small. */
protected $thumbnailUrl;

/** @var string Only applicable if the ts is provided, as it makes the time clickable to this link. */
protected $messageLink = '';
/** @var string|null Only applicable if the ts is provided, as it makes the time clickable to this link. */
protected $messageLink;

/** @var bool Causes the image, audio, and video sections to be hiding when collapsed is true. */
protected $collapsed = false;

/** @var string Name of the author. */
protected $authorName = '';
/** @var string|null Name of the author. */
protected $authorName;

/** @var string Providing this makes the author name clickable and points to this link. */
protected $authorLink = '';
/** @var string|null Providing this makes the author name clickable and points to this link. */
protected $authorLink;

/** @var string Displays a tiny icon to the left of the Author’s name. */
protected $authorIcon = '';
/** @var string|null Displays a tiny icon to the left of the Author’s name. */
protected $authorIcon;

/** @var string Title to display for this attachment, displays under the author. */
protected $title = '';
/** @var string|null Title to display for this attachment, displays under the author. */
protected $title;

/** @var string Providing this makes the title clickable, pointing to this link. */
protected $titleLink = '';
/** @var string|null Providing this makes the title clickable, pointing to this link. */
protected $titleLink;

/** @var bool When this is true, a download icon appears and clicking this saves the link to file. */
protected $titleLinkDownload = false;

/** @var string The image to display, will be “big” and easy to see. */
protected $imageUrl = '';
/** @var string|null The image to display, will be “big” and easy to see. */
protected $imageUrl;

/** @var string Audio file to play, only supports what html audio does. */
protected $audioUrl = '';
/** @var string|null Audio file to play, only supports what html audio does. */
protected $audioUrl;

/** @var string Video file to play, only supports what html video does. */
protected $videoUrl = '';
/** @var string|null Video file to play, only supports what html video does. */
protected $videoUrl;

/** @var array An array of Attachment Field Objects. */
protected $fields = [];

/**
* RocketChatAttachment constructor.
*
* @param array|null $config
* @param array $data
*/
public function __construct(array $config = null)
public function __construct(array $data = [])
{
if ($config != null) {
$this->setFromArray($config);
}
$this->setPropertiesFromArray($data);
}

/**
* Create a new instance of RocketChatAttachment.
*
* @param array|null $config
* @return RocketChatAttachment
*/
public static function create(array $config = null)
{
return new self($config);
}

/**
* Set attachment data form array.
*
* @param array $data
* @return \NotificationChannels\RocketChat\RocketChatAttachment
*/
protected function setFromArray(array $data)
public static function make(array $data = [])
{
foreach ($data as $key => $value) {
$method = Str::camel($key);
if (! method_exists($this, $method)) {
continue;
}
$this->{$method}($value);
}
return new self($data);
}

/**
* @param string $color
* @return RocketChatAttachment
* @return \NotificationChannels\RocketChat\RocketChatAttachment
*/
public function color(string $color): self
{
Expand Down Expand Up @@ -134,6 +116,7 @@ public function timestamp($timestamp): self
$date = clone $timestamp;
$timestamp = $date->setTimezone(new DateTimeZone('UTC'))->format('Y-m-d\TH:i:s.v\Z');
}

$this->timestamp = $timestamp;

return $this;
Expand Down Expand Up @@ -323,4 +306,23 @@ public function toArray(): array
'fields' => $this->fields,
]);
}

/**
* Set attachment data from array.
*
* @param array $data
* @return void
*/
private function setPropertiesFromArray(array $data): void
{
foreach ($data as $key => $value) {
$methodName = Str::camel($key);

if (! method_exists($this, $methodName)) {
continue;
}

$this->{$methodName}($value);
}
}
}
30 changes: 20 additions & 10 deletions src/RocketChatMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,34 @@

class RocketChatMessage
{
/** @var string RocketChat channel id. */
public $channel = '';
/** @var string|null RocketChat channel id. */
protected $channel = null;

/** @var string A user or app access token. */
public $from = '';
/** @var string|null A user or app access token. */
protected $from = null;

/** @var string The text content of the message. */
public $content = '';
protected $content;

/** @var string The alias name of the message. */
public $alias = '';
protected $alias;

/** @var string The avatar emoji of the message. */
public $emoji = '';
protected $emoji;

/** @var string The avatar image of the message. */
public $avatar = '';
protected $avatar;

/** @var \NotificationChannels\RocketChat\RocketChatAttachment[] Attachments of the message. */
public $attachments = [];
protected $attachments = [];

/**
* Create a new instance of RocketChatMessage.
*
* @param string $content
* @return static
*/
public static function create(string $content = ''): self
public static function make(string $content = ''): self
{
return new static($content);
}
Expand All @@ -48,6 +48,16 @@ public function __construct(string $content = '')
$this->content($content);
}

public function getChannel(): ?string
{
return $this->channel;
}

public function getFrom(): ?string
{
return $this->from;
}

/**
* Set the sender's access token.
*
Expand Down
15 changes: 8 additions & 7 deletions src/RocketChatWebhookChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use GuzzleHttp\Exception\ClientException;
use Illuminate\Notifications\Notification;
use NotificationChannels\RocketChat\Exceptions\CouldNotSendNotification;
use Psr\Http\Message\ResponseInterface;

final class RocketChatWebhookChannel
{
Expand Down Expand Up @@ -40,12 +39,14 @@ public function send($notifiable, Notification $notification): void
/** @var \NotificationChannels\RocketChat\RocketChatMessage $message */
$message = $notification->toRocketChat($notifiable);

$to = $message->channel ?: $notifiable->routeNotificationFor('RocketChat');
if (! $to = $to ?: $this->rocketChat->channel()) {
$to = $message->getChannel() ?: $notifiable->routeNotificationFor('RocketChat');
$to = $to ?: $this->rocketChat->getDefaultChannel();
if ($to === null) {
throw CouldNotSendNotification::missingTo();
}

if (! $from = $message->from ?: $this->rocketChat->token()) {
$from = $message->getFrom() ?: $this->rocketChat->getToken();
if (! $from) {
throw CouldNotSendNotification::missingFrom();
}

Expand All @@ -61,10 +62,10 @@ public function send($notifiable, Notification $notification): void
/**
* @param string $to
* @param \NotificationChannels\RocketChat\RocketChatMessage $message
* @return \Psr\Http\Message\ResponseInterface
* @return void
*/
private function sendMessage(string $to, RocketChatMessage $message): ResponseInterface
private function sendMessage(string $to, RocketChatMessage $message): void
{
return $this->rocketChat->sendMessage($to, $message->toArray());
$this->rocketChat->sendMessage($to, $message->toArray());
}
}
Loading

0 comments on commit 27da688

Please sign in to comment.