Skip to content

Commit

Permalink
[TASK] Move interface to abstract class, use jsonserializable
Browse files Browse the repository at this point in the history
  • Loading branch information
Woeler committed Jan 31, 2020
1 parent a6a0b78 commit 73a0c65
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 63 deletions.
12 changes: 12 additions & 0 deletions src/Message/AbstractDiscordMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Woeler\DiscordPhp\Message;

use JsonSerializable;

abstract class AbstractDiscordMessage implements JsonSerializable
{
abstract public function toArray(): array;

abstract public function jsonSerialize();
}
42 changes: 20 additions & 22 deletions src/Message/DiscordEmbedMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,96 +6,96 @@

use DateTimeInterface;

class DiscordEmbedMessage implements DiscordMessageInterface
class DiscordEmbedMessage extends AbstractDiscordMessage
{
/**
* @var string
*/
protected $content;
private $content;

/**
* @var string
*/
protected $avatar;
private $avatar;

/**
* @var string
*/
protected $username;
private $username;

/**
* @var string
*/
protected $title;
private $title;

/**
* @var string
*/
protected $description;
private $description;

/**
* @var string
*/
protected $url;
private $url;

/**
* @var int
*/
protected $color;
private $color;

/**
* @var DateTimeInterface
*/
protected $timestamp;
private $timestamp;

/**
* @var string
*/
protected $footer_icon;
private $footer_icon;

/**
* @var string
*/
protected $footer_text;
private $footer_text;

/**
* @var string
*/
protected $thumbnail;
private $thumbnail;

/**
* @var string
*/
protected $image;
private $image;

/**
* @var string
*/
protected $author_name;
private $author_name;

/**
* @var string
*/
protected $author_url;
private $author_url;

/**
* @var string
*/
protected $author_icon;
private $author_icon;

/**
* @var array
*/
protected $fields = [];
private $fields = [];

/**
* @var bool
*/
protected $tts = false;
private $tts = false;

public function toArray(): array
{
$data = [
return [
'username' => $this->username,
'content' => $this->content,
'avatar_url' => $this->avatar,
Expand Down Expand Up @@ -124,8 +124,6 @@ public function toArray(): array
],
]],
];

return $data;
}

public function getContent(): ?string
Expand Down Expand Up @@ -374,7 +372,7 @@ public function setColorWithHexValue(string $hexValue): self
return $this;
}

public function formatForDiscord(): array
public function jsonSerialize()
{
return $this->toArray();
}
Expand Down
14 changes: 0 additions & 14 deletions src/Message/DiscordMessageInterface.php

This file was deleted.

19 changes: 7 additions & 12 deletions src/Message/DiscordTextMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@

namespace Woeler\DiscordPhp\Message;

class DiscordTextMessage implements DiscordMessageInterface
class DiscordTextMessage extends AbstractDiscordMessage
{
/**
* @var string
*/
protected $content;
private $content;

/**
* @var string
*/
protected $avatar;
private $avatar;

/**
* @var string
*/
protected $username;
private $username;

/**
* @var bool
*/
protected $tts = false;
private $tts = false;

public function setContent(string $content): self
{
Expand Down Expand Up @@ -74,11 +74,6 @@ public function setTts(bool $tts): self
return $this;
}

public function formatForDiscord(): array
{
return $this->toArray();
}

public function toArray(): array
{
return [
Expand All @@ -89,8 +84,8 @@ public function toArray(): array
];
}

public function toJson(): string
public function jsonSerialize()
{
return json_encode($this->toArray());
return $this->toArray();
}
}
39 changes: 24 additions & 15 deletions src/Webhook/DiscordWebhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Woeler\DiscordPhp\Webhook;

use Woeler\DiscordPhp\Exception\DiscordInvalidResponseException;
use Woeler\DiscordPhp\Message\DiscordMessageInterface;
use Woeler\DiscordPhp\Message\AbstractDiscordMessage;

class DiscordWebhook
{
Expand All @@ -17,7 +17,7 @@ class DiscordWebhook
/**
* @var string
*/
protected $webhookUrl;
private $webhookUrl;

public function __construct(string $webhookUrl)
{
Expand All @@ -27,20 +27,29 @@ public function __construct(string $webhookUrl)
/**
* @throws DiscordInvalidResponseException
*/
public function send(DiscordMessageInterface $message): int
public function send(AbstractDiscordMessage $message): int
{
$ch = curl_init($this->webhookUrl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $message->toJson());
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['content-type: application/json']);
curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if ($code < 200 || $code >= 400) {
throw new DiscordInvalidResponseException('Discord Webhook returned invalid response: '.$code.'.', $code);
$sent = false;

while (!$sent) {
$ch = curl_init($this->webhookUrl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($message));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['content-type: application/json']);
$response = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if (429 === $code) {
$response = json_decode($response, false);
usleep($response->retry_after * 1000);
} else {
$sent = true;
if ($code < 200 || $code >= 400) {
throw new DiscordInvalidResponseException('Discord Webhook returned invalid response: '.$code.'.', $code);
}
}
}

return $code;
Expand Down

0 comments on commit 73a0c65

Please sign in to comment.