Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Symfony\Component\Notifier\Bridge\Discord;

use Symfony\Component\Notifier\Exception\LengthException;
use Symfony\Component\Notifier\Exception\LogicException;
use Symfony\Component\Notifier\Exception\TransportException;
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
Expand All @@ -31,8 +30,6 @@ final class DiscordBotTransport extends AbstractTransport
{
protected const HOST = 'discord.com';

private const SUBJECT_LIMIT = 2000;

public function __construct(
#[\SensitiveParameter] private string $token,
?HttpClientInterface $client = null,
Expand Down Expand Up @@ -65,10 +62,6 @@ protected function doSend(MessageInterface $message): SentMessage
$options = $message->getOptions()?->toArray() ?? [];
$options['content'] = $message->getSubject();

if (mb_strlen($options['content'], 'UTF-8') > self::SUBJECT_LIMIT) {
throw new LengthException(\sprintf('The subject length of a Discord message must not exceed %d characters.', self::SUBJECT_LIMIT));
}

$endpoint = \sprintf('https://%s/api/channels/%s/messages', $this->getEndpoint(), $channelId);
$response = $this->client->request('POST', $endpoint, [
'headers' => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Symfony\Component\Notifier\Bridge\Discord;

use Symfony\Component\Notifier\Exception\LengthException;
use Symfony\Component\Notifier\Exception\TransportException;
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
use Symfony\Component\Notifier\Message\ChatMessage;
Expand All @@ -29,8 +28,6 @@ final class DiscordTransport extends AbstractTransport
{
protected const HOST = 'discord.com';

private const SUBJECT_LIMIT = 2000;

public function __construct(
#[\SensitiveParameter] private string $token,
private string $webhookId,
Expand Down Expand Up @@ -62,10 +59,6 @@ protected function doSend(MessageInterface $message): SentMessage
$options = $message->getOptions()?->toArray() ?? [];
$options['content'] = $message->getSubject();

if (mb_strlen($options['content'], 'UTF-8') > self::SUBJECT_LIMIT) {
throw new LengthException(\sprintf('The subject length of a Discord message must not exceed %d characters.', self::SUBJECT_LIMIT));
}

$endpoint = \sprintf('https://%s/api/webhooks/%s/%s', $this->getEndpoint(), $this->webhookId, $this->token);
$response = $this->client->request('POST', $endpoint, [
'json' => array_filter($options),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,16 @@

namespace Symfony\Component\Notifier\Bridge\Discord\Embeds;

use Symfony\Component\Notifier\Exception\LengthException;

/**
* @author Karoly Gossler <connor@connor.hu>
*/
final class DiscordAuthorEmbedObject extends AbstractDiscordEmbedObject
{
private const NAME_LIMIT = 256;

/**
* @return $this
*/
public function name(string $name): static
{
if (mb_strlen($name, 'UTF-8') > self::NAME_LIMIT) {
throw new LengthException(\sprintf('Maximum length for the name is %d characters.', self::NAME_LIMIT));
}

$this->options['name'] = $name;

return $this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,16 @@

namespace Symfony\Component\Notifier\Bridge\Discord\Embeds;

use Symfony\Component\Notifier\Exception\LengthException;

/**
* @author Karoly Gossler <connor@connor.hu>
*/
final class DiscordEmbed extends AbstractDiscordEmbed
{
private const TITLE_LIMIT = 256;
private const DESCRIPTION_LIMIT = 4096;
private const FIELDS_LIMIT = 25;

/**
* @return $this
*/
public function title(string $title): static
{
if (mb_strlen($title, 'UTF-8') > self::TITLE_LIMIT) {
throw new LengthException(\sprintf('Maximum length for the title is %d characters.', self::TITLE_LIMIT));
}

$this->options['title'] = $title;

return $this;
Expand All @@ -41,10 +31,6 @@ public function title(string $title): static
*/
public function description(string $description): static
{
if (mb_strlen($description, 'UTF-8') > self::DESCRIPTION_LIMIT) {
throw new LengthException(\sprintf('Maximum length for the description is %d characters.', self::DESCRIPTION_LIMIT));
}

$this->options['description'] = $description;

return $this;
Expand Down Expand Up @@ -125,10 +111,6 @@ public function author(DiscordAuthorEmbedObject $author): static
*/
public function addField(DiscordFieldEmbedObject $field): static
{
if (self::FIELDS_LIMIT === \count($this->options['fields'] ?? [])) {
throw new LengthException(\sprintf('Maximum number of fields should not exceed %d.', self::FIELDS_LIMIT));
}

if (!isset($this->options['fields'])) {
$this->options['fields'] = [];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,16 @@

namespace Symfony\Component\Notifier\Bridge\Discord\Embeds;

use Symfony\Component\Notifier\Exception\LengthException;

/**
* @author Karoly Gossler <connor@connor.hu>
*/
final class DiscordFieldEmbedObject extends AbstractDiscordEmbedObject
{
private const NAME_LIMIT = 256;
private const VALUE_LIMIT = 1024;

/**
* @return $this
*/
public function name(string $name): static
{
if (mb_strlen($name, 'UTF-8') > self::NAME_LIMIT) {
throw new LengthException(\sprintf('Maximum length for the name is %d characters.', self::NAME_LIMIT));
}

$this->options['name'] = $name;

return $this;
Expand All @@ -40,10 +31,6 @@ public function name(string $name): static
*/
public function value(string $value): static
{
if (mb_strlen($value, 'UTF-8') > self::VALUE_LIMIT) {
throw new LengthException(\sprintf('Maximum length for the value is %d characters.', self::VALUE_LIMIT));
}

$this->options['value'] = $value;

return $this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,16 @@

namespace Symfony\Component\Notifier\Bridge\Discord\Embeds;

use Symfony\Component\Notifier\Exception\LengthException;

/**
* @author Karoly Gossler <connor@connor.hu>
*/
final class DiscordFooterEmbedObject extends AbstractDiscordEmbedObject
{
private const TEXT_LIMIT = 2048;

/**
* @return $this
*/
public function text(string $text): static
{
if (mb_strlen($text, 'UTF-8') > self::TEXT_LIMIT) {
throw new LengthException(\sprintf('Maximum length for the text is %d characters.', self::TEXT_LIMIT));
}

$this->options['text'] = $text;

return $this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Symfony\Component\HttpClient\Response\JsonMockResponse;
use Symfony\Component\Notifier\Bridge\Discord\DiscordBotTransport;
use Symfony\Component\Notifier\Bridge\Discord\DiscordOptions;
use Symfony\Component\Notifier\Exception\LengthException;
use Symfony\Component\Notifier\Exception\LogicException;
use Symfony\Component\Notifier\Exception\TransportException;
use Symfony\Component\Notifier\Message\ChatMessage;
Expand Down Expand Up @@ -57,16 +56,6 @@ public function testSendThrowsWithoutRecipientId()
$transport->send(new ChatMessage('testMessage'));
}

public function testSendChatMessageWithMoreThan2000CharsThrowsLogicException()
{
$transport = self::createTransport();

$this->expectException(LengthException::class);
$this->expectExceptionMessage('The subject length of a Discord message must not exceed 2000 characters.');

$transport->send(new ChatMessage(str_repeat('囍', 2001), (new DiscordOptions())->recipient('channel_id')));
}

public function testSendWithErrorResponseThrows()
{
$response = new JsonMockResponse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\Notifier\Bridge\Discord\DiscordTransport;
use Symfony\Component\Notifier\Exception\LengthException;
use Symfony\Component\Notifier\Exception\TransportException;
use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\Notifier\Message\SmsMessage;
Expand Down Expand Up @@ -45,16 +44,6 @@ public static function unsupportedMessagesProvider(): iterable
yield [new DummyMessage()];
}

public function testSendChatMessageWithMoreThan2000CharsThrowsLogicException()
{
$transport = self::createTransport();

$this->expectException(LengthException::class);
$this->expectExceptionMessage('The subject length of a Discord message must not exceed 2000 characters.');

$transport->send(new ChatMessage(str_repeat('囍', 2001)));
}

public function testSendWithErrorResponseThrows()
{
$response = $this->createMock(ResponseInterface::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Notifier\Bridge\Discord\Embeds\DiscordAuthorEmbedObject;
use Symfony\Component\Notifier\Exception\LengthException;

final class DiscordAuthorEmbedObjectTest extends TestCase
{
Expand All @@ -32,12 +31,4 @@ public function testCanBeInstantiated()
'proxy_icon_url' => 'http://proxy-icon-ur.l',
], $author->toArray());
}

public function testThrowsWhenNameExceedsCharacterLimit()
{
$this->expectException(LengthException::class);
$this->expectExceptionMessage('Maximum length for the name is 256 characters.');

(new DiscordAuthorEmbedObject())->name(str_repeat('š', 257));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\Notifier\Bridge\Discord\Embeds\DiscordEmbed;
use Symfony\Component\Notifier\Bridge\Discord\Embeds\DiscordFieldEmbedObject;
use Symfony\Component\Notifier\Exception\LengthException;

final class DiscordEmbedTest extends TestCase
{
Expand All @@ -39,39 +38,4 @@ public function testCanBeInstantiated()
],
], $embed->toArray());
}

public function testThrowsWhenTitleExceedsCharacterLimit()
{
$this->expectException(LengthException::class);
$this->expectExceptionMessage('Maximum length for the title is 256 characters.');

(new DiscordEmbed())->title(str_repeat('š', 257));
}

public function testThrowsWhenDescriptionExceedsCharacterLimit()
{
$this->expectException(LengthException::class);
$this->expectExceptionMessage('Maximum length for the description is 4096 characters.');

(new DiscordEmbed())->description(str_repeat('š', 4097));
}

public function testThrowsWhenFieldsLimitReached()
{
$embed = new DiscordEmbed();
for ($i = 0; $i < 25; ++$i) {
$embed->addField((new DiscordFieldEmbedObject())
->name('baz')
->value('qux')
);
}

$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Maximum number of fields should not exceed 25.');

$embed->addField((new DiscordFieldEmbedObject())
->name('fail')
->value('fail')
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Notifier\Bridge\Discord\Embeds\DiscordFieldEmbedObject;
use Symfony\Component\Notifier\Exception\LengthException;

final class DiscordFieldEmbedObjectTest extends TestCase
{
Expand All @@ -30,20 +29,4 @@ public function testCanBeInstantiated()
'inline' => true,
], $field->toArray());
}

public function testThrowsWhenNameExceedsCharacterLimit()
{
$this->expectException(LengthException::class);
$this->expectExceptionMessage('Maximum length for the name is 256 characters.');

(new DiscordFieldEmbedObject())->name(str_repeat('š', 257));
}

public function testThrowsWhenValueExceedsCharacterLimit()
{
$this->expectException(LengthException::class);
$this->expectExceptionMessage('Maximum length for the value is 1024 characters.');

(new DiscordFieldEmbedObject())->value(str_repeat('š', 1025));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Notifier\Bridge\Discord\Embeds\DiscordFooterEmbedObject;
use Symfony\Component\Notifier\Exception\LengthException;

final class DiscordFooterEmbedObjectTest extends TestCase
{
Expand All @@ -30,12 +29,4 @@ public function testCanBeInstantiated()
'proxy_icon_url' => 'http://proxy-icon-ur.l',
], $author->toArray());
}

public function testThrowsWhenTextExceedsCharacterLimit()
{
$this->expectException(LengthException::class);
$this->expectExceptionMessage('Maximum length for the text is 2048 characters.');

(new DiscordFooterEmbedObject())->text(str_repeat('š', 2049));
}
}
Loading