Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7b2c4d1

Browse files
committed
Add Discord bridge notifier
1 parent 67948a7 commit 7b2c4d1

File tree

14 files changed

+378
-0
lines changed

14 files changed

+378
-0
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
use Symfony\Component\Messenger\Transport\TransportInterface;
9292
use Symfony\Component\Mime\MimeTypeGuesserInterface;
9393
use Symfony\Component\Mime\MimeTypes;
94+
use Symfony\Component\Notifier\Bridge\Discord\DiscordTransportFactory;
9495
use Symfony\Component\Notifier\Bridge\Firebase\FirebaseTransportFactory;
9596
use Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory;
9697
use Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory;
@@ -2046,6 +2047,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
20462047
FirebaseTransportFactory::class => 'notifier.transport_factory.firebase',
20472048
OvhCloudTransportFactory::class => 'notifier.transport_factory.ovhcloud',
20482049
SinchTransportFactory::class => 'notifier.transport_factory.sinch',
2050+
DiscordTransportFactory::class => 'notifier.transport_factory.discord',
20492051
];
20502052

20512053
foreach ($classToServices as $class => $service) {

src/Symfony/Bundle/FrameworkBundle/Resources/config/notifier_transports.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@
4646
<tag name="texter.transport_factory" />
4747
</service>
4848

49+
<service id="notifier.transport_factory.discord" class="Symfony\Component\Notifier\Bridge\Discord\DiscordTransportFactory" parent="notifier.transport_factory.abstract">
50+
<tag name="chatter.transport_factory" />
51+
</service>
52+
4953
<service id="notifier.transport_factory.null" class="Symfony\Component\Notifier\Transport\NullTransportFactory" parent="notifier.transport_factory.abstract">
5054
<tag name="chatter.transport_factory" />
5155
<tag name="texter.transport_factory" />
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/Tests export-ignore
2+
/phpunit.xml.dist export-ignore
3+
/.gitignore export-ignore
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CHANGELOG
2+
=========
3+
4+
5.1.0
5+
-----
6+
7+
* Added the bridge
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\Discord;
13+
14+
use Symfony\Component\Notifier\Exception\LogicException;
15+
use Symfony\Component\Notifier\Exception\TransportException;
16+
use Symfony\Component\Notifier\Message\ChatMessage;
17+
use Symfony\Component\Notifier\Message\MessageInterface;
18+
use Symfony\Component\Notifier\Transport\AbstractTransport;
19+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
20+
use Symfony\Contracts\HttpClient\HttpClientInterface;
21+
22+
/**
23+
* DiscordTransport.
24+
*
25+
* @author Mathieu Piot <[email protected]>
26+
*
27+
* @internal
28+
*
29+
* @experimental in 5.0
30+
*/
31+
final class DiscordTransport extends AbstractTransport
32+
{
33+
protected const HOST = 'discordapp.com';
34+
35+
private $token;
36+
private $chatChannel;
37+
38+
public function __construct(string $token, string $channel = null, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
39+
{
40+
$this->token = $token;
41+
$this->chatChannel = $channel;
42+
$this->client = $client;
43+
44+
parent::__construct($client, $dispatcher);
45+
}
46+
47+
public function __toString(): string
48+
{
49+
return sprintf('discord://%s?channel=%s', $this->getEndpoint(), $this->chatChannel);
50+
}
51+
52+
public function supports(MessageInterface $message): bool
53+
{
54+
return $message instanceof ChatMessage;
55+
}
56+
57+
/**
58+
* @see https://discordapp.com/developers/docs/resources/webhook
59+
*/
60+
protected function doSend(MessageInterface $message): void
61+
{
62+
if (!$message instanceof ChatMessage) {
63+
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" (instance of "%s" given).', __CLASS__, ChatMessage::class, get_debug_type($message)));
64+
}
65+
66+
$endpoint = sprintf('https://%s/api/webhooks/%s/%s', $this->getEndpoint(), $this->token, $this->chatChannel);
67+
$options['content'] = $message->getSubject();
68+
$response = $this->client->request('POST', $endpoint, [
69+
'json' => array_filter($options),
70+
]);
71+
72+
if (204 !== $response->getStatusCode()) {
73+
$result = $response->toArray(false);
74+
75+
throw new TransportException(sprintf('Unable to post the Discord message: "%s" (%s).', $result['message'], $result['code']), $response);
76+
}
77+
}
78+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\Discord;
13+
14+
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
15+
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
16+
use Symfony\Component\Notifier\Transport\Dsn;
17+
use Symfony\Component\Notifier\Transport\TransportInterface;
18+
19+
/**
20+
* @author Mathieu Piot <[email protected]>
21+
*
22+
* @experimental in 5.0
23+
*/
24+
final class DiscordTransportFactory extends AbstractTransportFactory
25+
{
26+
/**
27+
* @return DiscordTransport
28+
*/
29+
public function create(Dsn $dsn): TransportInterface
30+
{
31+
$scheme = $dsn->getScheme();
32+
$token = $this->getUser($dsn);
33+
$channel = $dsn->getOption('channel');
34+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
35+
$port = $dsn->getPort();
36+
37+
if ('discord' === $scheme) {
38+
return (new DiscordTransport($token, $channel, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
39+
}
40+
41+
throw new UnsupportedSchemeException($dsn, 'discord', $this->getSupportedSchemes());
42+
}
43+
44+
protected function getSupportedSchemes(): array
45+
{
46+
return ['discord'];
47+
}
48+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2019-2020 Fabien Potencier
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is furnished
8+
to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Discord Notifier
2+
================
3+
4+
Provides Discord integration for Symfony Notifier.
5+
6+
Resources
7+
---------
8+
9+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
10+
* [Report issues](https://github.com/symfony/symfony/issues) and
11+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
12+
in the [main Symfony repository](https://github.com/symfony/symfony)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\Discord\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Notifier\Bridge\Discord\DiscordTransportFactory;
16+
use Symfony\Component\Notifier\Exception\IncompleteDsnException;
17+
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
18+
use Symfony\Component\Notifier\Transport\Dsn;
19+
20+
final class DiscordTransportFactoryTest extends TestCase
21+
{
22+
public function testCreateWithDsn(): void
23+
{
24+
$factory = new DiscordTransportFactory();
25+
26+
$host = 'testHost';
27+
$channel = 'testChannel';
28+
29+
$transport = $factory->create(Dsn::fromString(sprintf('discord://%s@%s/?channel=%s', 'token', $host, $channel)));
30+
31+
$this->assertSame(sprintf('discord://%s?channel=%s', $host, $channel), (string) $transport);
32+
}
33+
34+
public function testCreateWithNoTokenThrowsMalformed(): void
35+
{
36+
$factory = new DiscordTransportFactory();
37+
38+
$this->expectException(IncompleteDsnException::class);
39+
$factory->create(Dsn::fromString(sprintf('discord://%s/?channel=%s', 'testHost', 'testChannel')));
40+
}
41+
42+
public function testSupportsDiscordScheme(): void
43+
{
44+
$factory = new DiscordTransportFactory();
45+
46+
$this->assertTrue($factory->supports(Dsn::fromString('discord://host/?channel=testChannel')));
47+
$this->assertFalse($factory->supports(Dsn::fromString('somethingElse://host/?channel=testChannel')));
48+
}
49+
50+
public function testNonDiscordSchemeThrows(): void
51+
{
52+
$factory = new DiscordTransportFactory();
53+
54+
$this->expectException(UnsupportedSchemeException::class);
55+
$factory->create(Dsn::fromString('somethingElse://token@host/?channel=testChannel'));
56+
}
57+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\Discord\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\HttpClient\MockHttpClient;
16+
use Symfony\Component\Notifier\Bridge\Discord\DiscordTransport;
17+
use Symfony\Component\Notifier\Exception\LogicException;
18+
use Symfony\Component\Notifier\Exception\TransportException;
19+
use Symfony\Component\Notifier\Message\ChatMessage;
20+
use Symfony\Component\Notifier\Message\MessageInterface;
21+
use Symfony\Contracts\HttpClient\HttpClientInterface;
22+
use Symfony\Contracts\HttpClient\ResponseInterface;
23+
24+
final class DiscordTransportTest extends TestCase
25+
{
26+
public function testToStringContainsProperties(): void
27+
{
28+
$channel = 'testChannel';
29+
30+
$transport = new DiscordTransport('testToken', $channel, $this->createMock(HttpClientInterface::class));
31+
$transport->setHost('testHost');
32+
33+
$this->assertSame(sprintf('discord://%s?channel=%s', 'testHost', $channel), (string) $transport);
34+
}
35+
36+
public function testSupportsChatMessage(): void
37+
{
38+
$transport = new DiscordTransport('testToken', 'testChannel', $this->createMock(HttpClientInterface::class));
39+
40+
$this->assertTrue($transport->supports(new ChatMessage('testChatMessage')));
41+
$this->assertFalse($transport->supports($this->createMock(MessageInterface::class)));
42+
}
43+
44+
public function testSendNonChatMessageThrows(): void
45+
{
46+
$this->expectException(LogicException::class);
47+
$transport = new DiscordTransport('testToken', 'testChannel', $this->createMock(HttpClientInterface::class));
48+
49+
$transport->send($this->createMock(MessageInterface::class));
50+
}
51+
52+
public function testSendWithErrorResponseThrows(): void
53+
{
54+
$this->expectException(TransportException::class);
55+
$this->expectExceptionMessageMatches('/testDescription.+testErrorCode/');
56+
57+
$response = $this->createMock(ResponseInterface::class);
58+
$response->expects($this->exactly(2))
59+
->method('getStatusCode')
60+
->willReturn(400);
61+
$response->expects($this->once())
62+
->method('getContent')
63+
->willReturn(json_encode(['message' => 'testDescription', 'code' => 'testErrorCode']));
64+
65+
$client = new MockHttpClient(static function () use ($response): ResponseInterface {
66+
return $response;
67+
});
68+
69+
$transport = new DiscordTransport('testToken', 'testChannel', $client);
70+
71+
$transport->send(new ChatMessage('testMessage'));
72+
}
73+
}

0 commit comments

Comments
 (0)