Skip to content

Commit

Permalink
Add interfaces for client and message (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
4c0n authored May 6, 2020
1 parent 687bf65 commit adffe9e
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 30 deletions.
28 changes: 14 additions & 14 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
namespace Nexy\Slack;

use Nexy\Slack\Exception\SlackApiException;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Client\ClientInterface as HttpClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* @author Sullivan Senechal <soullivaneuh@gmail.com>
*/
final class Client
final class Client implements ClientInterface
{
/**
* @var ErrorResponseHandler
Expand All @@ -40,7 +40,7 @@ final class Client
private $options;

/**
* @var ClientInterface
* @var HttpClientInterface
*/
private $httpClient;

Expand All @@ -58,7 +58,7 @@ final class Client
* @param mixed[] $options
*/
public function __construct(
ClientInterface $httpClient,
HttpClientInterface $httpClient,
RequestFactoryInterface $requestFactory,
StreamFactoryInterface $streamFactory,
string $endpoint,
Expand Down Expand Up @@ -103,9 +103,9 @@ public function __construct(
* @param string $name The name of the method
* @param array $arguments The method arguments
*
* @return \Nexy\Slack\Message
* @return \Nexy\Slack\MessageInterface
*/
public function __call(string $name, array $arguments): Message
public function __call(string $name, array $arguments): MessageInterface
{
return \call_user_func_array([$this->createMessage(), $name], $arguments);
}
Expand All @@ -118,9 +118,9 @@ public function getOptions(): array
/**
* Create a new message with defaults.
*
* @return \Nexy\Slack\Message
* @return \Nexy\Slack\MessageInterface
*/
public function createMessage(): Message
public function createMessage(): MessageInterface
{
return (new Message($this))
->setChannel($this->options['channel'])
Expand All @@ -134,14 +134,14 @@ public function createMessage(): Message
/**
* Send a message.
*
* @param \Nexy\Slack\Message $message
* @param \Nexy\Slack\MessageInterface $message
*
* @throws \RuntimeException
* @throws \Psr\Http\Client\Exception
* @throws SlackApiException
* @throws \Http\Client\Exception
*/
public function sendMessage(Message $message): void
public function sendMessage(MessageInterface $message): void
{
// Ensure the message will always be sent to the default channel if asked for.
if ($this->options['sticky_channel']) {
Expand All @@ -168,9 +168,9 @@ public function sendMessage(Message $message): void
/**
* Prepares the payload to be sent to the webhook.
*
* @param \Nexy\Slack\Message $message The message to send
* @param \Nexy\Slack\MessageInterface $message The message to send
*/
private function preparePayload(Message $message): array
private function preparePayload(MessageInterface $message): array
{
$payload = [
'text' => $message->getText(),
Expand All @@ -194,9 +194,9 @@ private function preparePayload(Message $message): array
/**
* Get the attachments in array form.
*
* @param \Nexy\Slack\Message $message
* @param \Nexy\Slack\MessageInterface $message
*/
private function getAttachmentsAsArrays(Message $message): array
private function getAttachmentsAsArrays(MessageInterface $message): array
{
$attachments = [];

Expand Down
21 changes: 21 additions & 0 deletions src/ClientInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Nexylan packages.
*
* (c) Nexylan SAS <contact@nexylan.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Nexy\Slack;

interface ClientInterface
{
public function createMessage(): MessageInterface;

public function sendMessage(MessageInterface $message): void;
}
32 changes: 16 additions & 16 deletions src/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/**
* @author Sullivan Senechal <soullivaneuh@gmail.com>
*/
final class Message
final class Message implements MessageInterface
{
/**
* Reference to the Slack client responsible for sending
Expand Down Expand Up @@ -114,7 +114,7 @@ public function getText(): ?string
*
* @return $this
*/
public function setText(?string $text): self
public function setText(?string $text): MessageInterface
{
$this->text = $text;

Expand All @@ -131,7 +131,7 @@ public function getChannel(): ?string
*
* @return $this
*/
public function setChannel(?string $channel): self
public function setChannel(?string $channel): MessageInterface
{
$this->channel = $channel;

Expand All @@ -148,7 +148,7 @@ public function getUsername(): ?string
*
* @return $this
*/
public function setUsername(?string $username): self
public function setUsername(?string $username): MessageInterface
{
$this->username = $username;

Expand All @@ -165,7 +165,7 @@ public function getIcon(): ?string
*
* @return $this
*/
public function setIcon(?string $icon): self
public function setIcon(?string $icon): MessageInterface
{
if (null === $icon) {
$this->icon = $this->iconType = null;
Expand Down Expand Up @@ -200,19 +200,19 @@ public function getAllowMarkdown(): bool
*
* @return Message
*/
public function setAllowMarkdown(bool $value): self
public function setAllowMarkdown(bool $value): MessageInterface
{
$this->allowMarkdown = $value;

return $this;
}

public function enableMarkdown(): self
public function enableMarkdown(): MessageInterface
{
return $this->setAllowMarkdown(true);
}

public function disableMarkdown(): self
public function disableMarkdown(): MessageInterface
{
return $this->setAllowMarkdown(false);
}
Expand All @@ -228,7 +228,7 @@ public function getMarkdownInAttachments(): array
*
* @return Message
*/
public function setMarkdownInAttachments(array $fields): self
public function setMarkdownInAttachments(array $fields): MessageInterface
{
$this->markdownInAttachments = $fields;

Expand All @@ -240,7 +240,7 @@ public function setMarkdownInAttachments(array $fields): self
*
* @return $this
*/
public function from(string $username): self
public function from(string $username): MessageInterface
{
return $this->setUsername($username);
}
Expand All @@ -250,7 +250,7 @@ public function from(string $username): self
*
* @return $this
*/
public function to(string $channel): self
public function to(string $channel): MessageInterface
{
return $this->setChannel($channel);
}
Expand All @@ -260,7 +260,7 @@ public function to(string $channel): self
*
* @return $this
*/
public function withIcon(string $icon): self
public function withIcon(string $icon): MessageInterface
{
return $this->setIcon($icon);
}
Expand All @@ -270,7 +270,7 @@ public function withIcon(string $icon): self
*
* @return $this
*/
public function attach(Attachment $attachment): self
public function attach(Attachment $attachment): MessageInterface
{
$this->attachments[] = $attachment;

Expand All @@ -290,7 +290,7 @@ public function getAttachments(): array
*
* @return $this
*/
public function setAttachments(array $attachments): self
public function setAttachments(array $attachments): MessageInterface
{
$this->clearAttachments();

Expand All @@ -306,7 +306,7 @@ public function setAttachments(array $attachments): self
*
* @return $this
*/
public function clearAttachments(): self
public function clearAttachments(): MessageInterface
{
$this->attachments = [];

Expand All @@ -320,7 +320,7 @@ public function clearAttachments(): self
*
* @return Message
*/
public function send(?string $text = null): self
public function send(?string $text = null): MessageInterface
{
if ($text) {
$this->setText($text);
Expand Down
67 changes: 67 additions & 0 deletions src/MessageInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Nexylan packages.
*
* (c) Nexylan SAS <contact@nexylan.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Nexy\Slack;

interface MessageInterface
{
public function getText(): ?string;

public function setText(?string $text): self;

public function getChannel(): ?string;

public function setChannel(?string $channel): self;

public function getUsername(): ?string;

public function setUsername(?string $username): self;

public function getIcon(): ?string;

public function setIcon(?string $icon): self;

public function getIconType(): ?string;

public function getAllowMarkdown(): bool;

public function setAllowMarkdown(bool $value): self;

public function enableMarkdown(): self;

public function disableMarkdown(): self;

public function getMarkdownInAttachments(): array;

public function setMarkdownInAttachments(array $fields): self;

public function from(string $username): self;

public function to(string $channel): self;

public function withIcon(string $icon): self;

public function attach(Attachment $attachment): self;

/** @return Attachment[] */
public function getAttachments(): array;

/**
* @param Attachment[] $attachments
*/
public function setAttachments(array $attachments): self;

public function clearAttachments(): self;

public function send(?string $text = null): self;
}

0 comments on commit adffe9e

Please sign in to comment.