Skip to content

Commit fbd7182

Browse files
committed
[Notifier] Rework/streamline bridges
1 parent 0fa187d commit fbd7182

33 files changed

+511
-96
lines changed

src/Symfony/Component/Notifier/Bridge/Discord/DiscordTransportFactory.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ final class DiscordTransportFactory extends AbstractTransportFactory
3030
public function create(Dsn $dsn): TransportInterface
3131
{
3232
$scheme = $dsn->getScheme();
33+
34+
if ('discord' !== $scheme) {
35+
throw new UnsupportedSchemeException($dsn, 'discord', $this->getSupportedSchemes());
36+
}
37+
3338
$token = $this->getUser($dsn);
3439
$webhookId = $dsn->getOption('webhook_id');
3540

@@ -40,11 +45,7 @@ public function create(Dsn $dsn): TransportInterface
4045
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
4146
$port = $dsn->getPort();
4247

43-
if ('discord' === $scheme) {
44-
return (new DiscordTransport($token, $webhookId, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
45-
}
46-
47-
throw new UnsupportedSchemeException($dsn, 'discord', $this->getSupportedSchemes());
48+
return (new DiscordTransport($token, $webhookId, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
4849
}
4950

5051
protected function getSupportedSchemes(): array

src/Symfony/Component/Notifier/Bridge/Discord/README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
Discord Notifier
22
================
33

4-
Provides Discord integration for Symfony Notifier.
4+
Provides [Discord](https://discord.com) integration for Symfony Notifier.
55

66
DSN example
77
-----------
88

99
```
10-
// .env file
1110
DISCORD_DSN=discord://TOKEN@default?webhook_id=ID
1211
```
1312

1413
where:
1514
- `TOKEN` the secure token of the webhook (returned for Incoming Webhooks)
1615
- `ID` the id of the webhook
1716

18-
1917
Resources
2018
---------
2119

src/Symfony/Component/Notifier/Bridge/Discord/Tests/DiscordTransportFactoryTest.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ public function testCreateWithDsn()
2424
$factory = new DiscordTransportFactory();
2525

2626
$host = 'testHost';
27-
$webhookId = 'testChannel';
27+
$webhookId = 'testWebhookId';
2828

2929
$transport = $factory->create(Dsn::fromString(sprintf('discord://%s@%s/?webhook_id=%s', 'token', $host, $webhookId)));
3030

3131
$this->assertSame(sprintf('discord://%s?webhook_id=%s', $host, $webhookId), (string) $transport);
3232
}
3333

34-
public function testCreateWithNoWebhookIdThrowsMalformed()
34+
public function testCreateWithMissingOptionWebhookIdThrowsIncompleteDsnException()
3535
{
3636
$factory = new DiscordTransportFactory();
3737

@@ -56,11 +56,21 @@ public function testSupportsDiscordScheme()
5656
$this->assertFalse($factory->supports(Dsn::fromString('somethingElse://host/?webhook_id=testChannel')));
5757
}
5858

59-
public function testNonDiscordSchemeThrows()
59+
public function testUnsupportedSchemeThrowsUnsupportedSchemeException()
6060
{
6161
$factory = new DiscordTransportFactory();
6262

6363
$this->expectException(UnsupportedSchemeException::class);
6464
$factory->create(Dsn::fromString('somethingElse://token@host/?webhook_id=testChannel'));
6565
}
66+
67+
public function testUnsupportedSchemeThrowsUnsupportedSchemeExceptionEvenIfRequiredOptionIsMissing()
68+
{
69+
$factory = new DiscordTransportFactory();
70+
71+
$this->expectException(UnsupportedSchemeException::class);
72+
73+
// unsupported scheme and missing "webhook_id" option
74+
$factory->create(Dsn::fromString('somethingElse://token@host'));
75+
}
6676
}

src/Symfony/Component/Notifier/Bridge/Discord/Tests/DiscordTransportTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ final class DiscordTransportTest extends TestCase
2525
{
2626
public function testToStringContainsProperties()
2727
{
28-
$webhookId = 'testChannel';
28+
$webhookId = 'testWebhookId';
2929

3030
$transport = new DiscordTransport('testToken', $webhookId, $this->createMock(HttpClientInterface::class));
3131
$transport->setHost('testHost');
@@ -35,7 +35,7 @@ public function testToStringContainsProperties()
3535

3636
public function testSupportsChatMessage()
3737
{
38-
$transport = new DiscordTransport('testToken', 'testChannel', $this->createMock(HttpClientInterface::class));
38+
$transport = new DiscordTransport('testToken', 'testWebhookId', $this->createMock(HttpClientInterface::class));
3939

4040
$this->assertTrue($transport->supports(new ChatMessage('testChatMessage')));
4141
$this->assertFalse($transport->supports($this->createMock(MessageInterface::class)));
@@ -44,7 +44,7 @@ public function testSupportsChatMessage()
4444
public function testSendNonChatMessageThrows()
4545
{
4646
$this->expectException(LogicException::class);
47-
$transport = new DiscordTransport('testToken', 'testChannel', $this->createMock(HttpClientInterface::class));
47+
$transport = new DiscordTransport('testToken', 'testWebhookId', $this->createMock(HttpClientInterface::class));
4848

4949
$transport->send($this->createMock(MessageInterface::class));
5050
}
@@ -66,7 +66,7 @@ public function testSendWithErrorResponseThrows()
6666
return $response;
6767
});
6868

69-
$transport = new DiscordTransport('testToken', 'testChannel', $client);
69+
$transport = new DiscordTransport('testToken', 'testWebhookId', $client);
7070

7171
$transport->send(new ChatMessage('testMessage'));
7272
}

src/Symfony/Component/Notifier/Bridge/Discord/composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,4 @@
3030
]
3131
},
3232
"minimum-stability": "dev"
33-
3433
}

src/Symfony/Component/Notifier/Bridge/Esendex/EsendexTransportFactory.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Notifier\Bridge\Esendex;
1313

14+
use Symfony\Component\Notifier\Exception\IncompleteDsnException;
1415
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
1516
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
1617
use Symfony\Component\Notifier\Transport\Dsn;
@@ -27,17 +28,28 @@ final class EsendexTransportFactory extends AbstractTransportFactory
2728
public function create(Dsn $dsn): TransportInterface
2829
{
2930
$scheme = $dsn->getScheme();
31+
32+
if ('esendex' === $scheme) {
33+
throw new UnsupportedSchemeException($dsn, 'esendex', $this->getSupportedSchemes());
34+
}
35+
3036
$token = $this->getUser($dsn).':'.$this->getPassword($dsn);
3137
$accountReference = $dsn->getOption('accountreference');
38+
39+
if (!$accountReference) {
40+
throw new IncompleteDsnException('Missing accountreference.', $dsn->getOriginalDsn());
41+
}
42+
3243
$from = $dsn->getOption('from');
33-
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
34-
$port = $dsn->getPort();
3544

36-
if ('esendex' === $scheme) {
37-
return (new EsendexTransport($token, $accountReference, $from, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
45+
if (!$from) {
46+
throw new IncompleteDsnException('Missing from.', $dsn->getOriginalDsn());
3847
}
3948

40-
throw new UnsupportedSchemeException($dsn, 'esendex', $this->getSupportedSchemes());
49+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
50+
$port = $dsn->getPort();
51+
52+
return (new EsendexTransport($token, $accountReference, $from, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
4153
}
4254

4355
protected function getSupportedSchemes(): array

src/Symfony/Component/Notifier/Bridge/Esendex/README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
Esendex Notifier
22
================
33

4-
Provides Esendex integration for Symfony Notifier.
4+
Provides [Esendex](https://esendex.com) integration for Symfony Notifier.
55

66
DSN example
77
-----------
88

99
```
10-
// .env file
11-
ESENDEX_DSN='esendex://EMAIL:PASSWORD@default?accountreference=ACCOUNT_REFERENCE&from=FROM'
10+
ESENDEX_DSN=esendex://EMAIL:PASSWORD@default?accountreference=ACCOUNT_REFERENCE&from=FROM
1211
```
1312

1413
where:
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Esendex\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Notifier\Bridge\Esendex\EsendexTransportFactory;
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 EsendexTransportFactoryTest extends TestCase
21+
{
22+
public function testCreateWithDsn()
23+
{
24+
$factory = new EsendexTransportFactory();
25+
26+
$host = 'testHost';
27+
$accountReference = 'ACCOUNTREFERENCE';
28+
$from = 'from';
29+
30+
$transport = $factory->create(Dsn::fromString(sprintf('esendex://%s@%s/?accountreference=%s&from=%s', 'token', $host, $accountReference, $from)));
31+
32+
$this->assertSame(sprintf('discord://%s?accountreference=%s&from=%s', $host, $accountReference, $from), (string) $transport);
33+
}
34+
35+
public function testCreateWithMissingOptionAccountreferenceThrowsIncompleteDsnException()
36+
{
37+
$factory = new EsendexTransportFactory();
38+
39+
$this->expectException(IncompleteDsnException::class);
40+
41+
$dsnIncomplete = 'esendex://token@host/?from=FROM';
42+
$factory->create(Dsn::fromString($dsnIncomplete));
43+
}
44+
45+
public function testCreateWithMissingOptionFromThrowsIncompleteDsnException()
46+
{
47+
$factory = new EsendexTransportFactory();
48+
49+
$this->expectException(IncompleteDsnException::class);
50+
51+
$dsnIncomplete = 'esendex://token@host/?accountreference=ACCOUNTREFERENCE';
52+
$factory->create(Dsn::fromString($dsnIncomplete));
53+
}
54+
55+
public function testUnsupportedSchemeThrowsUnsupportedSchemeException()
56+
{
57+
$factory = new EsendexTransportFactory();
58+
59+
$this->expectException(UnsupportedSchemeException::class);
60+
$factory->create(Dsn::fromString('somethingElse://token@host/?accountreference=REFERENCE&from=FROM'));
61+
}
62+
63+
public function testUnsupportedSchemeThrowsUnsupportedSchemeExceptionEvenIfRequiredOptionIsMissing()
64+
{
65+
$factory = new EsendexTransportFactory();
66+
67+
$this->expectException(UnsupportedSchemeException::class);
68+
69+
// unsupported scheme and missing "from" option
70+
$factory->create(Dsn::fromString('somethingElse://token@host/?accountreference=REFERENCE'));
71+
}
72+
}

src/Symfony/Component/Notifier/Bridge/GoogleChat/GoogleChatTransportFactory.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,21 @@ public function create(Dsn $dsn): TransportInterface
3232
{
3333
$scheme = $dsn->getScheme();
3434

35-
if ('googlechat' === $scheme) {
36-
$space = explode('/', $dsn->getPath())[1];
37-
$accessKey = $this->getUser($dsn);
38-
$accessToken = $this->getPassword($dsn);
39-
$threadKey = $dsn->getOption('threadKey');
40-
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
41-
$port = $dsn->getPort();
42-
43-
return (new GoogleChatTransport($space, $accessKey, $accessToken, $this->client, $this->dispatcher))
44-
->setThreadKey($threadKey)
45-
->setHost($host)
46-
->setPort($port);
35+
if ('googlechat' !== $scheme) {
36+
throw new UnsupportedSchemeException($dsn, 'googlechat', $this->getSupportedSchemes());
4737
}
4838

49-
throw new UnsupportedSchemeException($dsn, 'googlechat', $this->getSupportedSchemes());
39+
$space = explode('/', $dsn->getPath())[1];
40+
$accessKey = $this->getUser($dsn);
41+
$accessToken = $this->getPassword($dsn);
42+
$threadKey = $dsn->getOption('threadKey');
43+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
44+
$port = $dsn->getPort();
45+
46+
return (new GoogleChatTransport($space, $accessKey, $accessToken, $this->client, $this->dispatcher))
47+
->setThreadKey($threadKey)
48+
->setHost($host)
49+
->setPort($port);
5050
}
5151

5252
protected function getSupportedSchemes(): array

src/Symfony/Component/Notifier/Bridge/GoogleChat/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ DSN example
77
-----------
88

99
```
10-
// .env file
11-
INFOBIP_DSN=googlechat://ACCESS_KEY:ACCESS_TOKEN@default/SPACE?threadKey=THREAD_KEY
10+
GOOGLE_CHAT_DSN=googlechat://ACCESS_KEY:ACCESS_TOKEN@default/SPACE?threadKey=THREAD_KEY
1211
```
1312

1413
where:

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy