Skip to content

Commit

Permalink
[TASK] Update for php7.4/8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Woeler committed Jun 30, 2021
1 parent 33be76a commit 7e1610a
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 138 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/.idea
/.ddev
/.php_cs.cache
/composer.lock
/vendor
Expand Down
6 changes: 1 addition & 5 deletions .php_cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@ $finder = PhpCsFixer\Finder::create()
return PhpCsFixer\Config::create()
->setRules([
'@Symfony' => true,
'binary_operator_spaces' => [
'align_double_arrow' => true,
'align_equals' => true
],
'ordered_imports' => true,
'array_syntax' => [
'syntax' => 'short'
],
'ordered_class_elements' => ['use_trait', 'constant_public', 'constant_protected', 'constant_private', 'property_public', 'property_protected', 'property_private', 'construct', 'destruct', 'magic', 'phpunit', 'method_public', 'method_protected', 'method_private'],
])
->setFinder($finder);
->setFinder($finder);
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
}
],
"require": {
"php": ">=7.2.0",
"php": "^7.4|^8.0",
"ext-curl": "*",
"ext-json": "*"
},
Expand All @@ -25,7 +25,7 @@
]
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.16"
"friendsofphp/php-cs-fixer": "^2.19"
},
"config": {
"bin-dir": "bin/"
Expand Down
135 changes: 34 additions & 101 deletions src/Message/DiscordEmbedMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,107 +8,40 @@

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

/**
* @var bool
*/
private $tts = false;
private ?string $content = null;
private ?string $avatar = null;
private ?string $username = null;
private ?string $title = null;
private ?string $description = null;
private ?string $url = null;
private ?int $color = null;
private ?\DateTimeInterface $timestamp = null;
private ?string $footer_icon = null;
private ?string $footer_text = null;
private ?string $thumbnail = null;
private ?string $image = null;
private ?string $author_name = null;
private ?string $author_url = null;
private ?string $author_icon = null;
private array $fields = [];
private bool $tts = false;

public function toArray(): array
{
return [
'username' => $this->username,
'content' => $this->content,
'username' => $this->username,
'content' => $this->content,
'avatar_url' => $this->avatar,
'tts' => $this->tts,
'embeds' => [[
'title' => $this->title,
'tts' => $this->tts,
'embeds' => [[
'title' => $this->title,
'description' => $this->description,
'timestamp' => null === $this->timestamp ? null : $this->timestamp->format(\DateTime::ATOM),
'url' => $this->url,
'color' => $this->color,
'author' => [
'name' => $this->author_name,
'url' => $this->author_url,
'timestamp' => null === $this->timestamp ? null : $this->timestamp->format('Y-m-d\TH:i:sP'),
'url' => $this->url,
'color' => $this->color,
'author' => [
'name' => $this->author_name,
'url' => $this->author_url,
'icon_url' => $this->author_icon,
],
'image' => [
Expand All @@ -119,7 +52,7 @@ public function toArray(): array
],
'fields' => $this->fields,
'footer' => [
'text' => $this->footer_text,
'text' => $this->footer_text,
'icon_url' => $this->footer_icon,
],
]],
Expand Down Expand Up @@ -332,14 +265,14 @@ public function setTts(bool $tts): self

public function toJson(): string
{
return json_encode($this->toArray());
return json_encode($this->toArray(), JSON_THROW_ON_ERROR);
}

public function addField(string $title, string $value, bool $inLine = false): self
{
$this->fields[] = [
'name' => $title,
'value' => $value,
'name' => $title,
'value' => $value,
'inline' => $inLine,
];

Expand All @@ -366,13 +299,13 @@ public function getField(string $title): array

public function setColorWithHexValue(string $hexValue): self
{
$hexValue = str_replace('#', '', $hexValue);
$hexValue = str_replace('#', '', $hexValue);
$this->color = hexdec($hexValue);

return $this;
}

public function jsonSerialize()
public function jsonSerialize(): array
{
return $this->toArray();
}
Expand Down
31 changes: 8 additions & 23 deletions src/Message/DiscordTextMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,10 @@

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

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

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

/**
* @var bool
*/
private $tts = false;
private ?string $content;
private ?string $avatar;
private ?string $username;
private bool $tts = false;

public function setContent(string $content): self
{
Expand Down Expand Up @@ -77,14 +62,14 @@ public function setTts(bool $tts): self
public function toArray(): array
{
return [
'content' => $this->content,
'content' => $this->content,
'avatar_url' => $this->avatar,
'username' => $this->username,
'tts' => $this->tts,
'username' => $this->username,
'tts' => $this->tts,
];
}

public function jsonSerialize()
public function jsonSerialize(): array
{
return $this->toArray();
}
Expand Down
11 changes: 4 additions & 7 deletions src/Webhook/DiscordWebhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ class DiscordWebhook
*/
public const DISCORD_WEBHOOK_URL_PREFIX = 'https://discordapp.com/api/webhooks/';

/**
* @var string
*/
private $webhookUrl;
private string $webhookUrl;

public function __construct(string $webhookUrl)
{
Expand All @@ -34,15 +31,15 @@ public function send(AbstractDiscordMessage $message): int
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_POSTFIELDS, json_encode($message, JSON_THROW_ON_ERROR));
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);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if (429 === $code) {
$response = json_decode($response, false);
$response = json_decode($response, false, 512, JSON_THROW_ON_ERROR);
usleep($response->retry_after * 1000);
} else {
$sent = true;
Expand Down

0 comments on commit 7e1610a

Please sign in to comment.