Skip to content

[Notifier] Add exception for deprecated slack dsn #39310

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 8, 2020
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
12 changes: 12 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Slack/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@ Slack Notifier

Provides Slack integration for Symfony Notifier.

DSN example
-----------

```
// .env file
SLACK_DSN=slack://TOKEN@default?channel=CHANNEL
```

where:
- `TOKEN` is your Bot User OAuth Access Token
- `CHANNEL` is a Channel, private group, or IM channel to send message to. Can be an encoded ID, or a name

Resources
---------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Notifier\Bridge\Slack;

use Symfony\Component\Notifier\Exception\IncompleteDsnException;
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
use Symfony\Component\Notifier\Transport\Dsn;
Expand All @@ -28,17 +29,20 @@ final class SlackTransportFactory extends AbstractTransportFactory
*/
public function create(Dsn $dsn): TransportInterface
{
$scheme = $dsn->getScheme();
if ('slack' !== $dsn->getScheme()) {
throw new UnsupportedSchemeException($dsn, 'slack', $this->getSupportedSchemes());
}

if ('/' !== $dsn->getPath()) {
throw new IncompleteDsnException('Support for Slack webhook DSN has been dropped since 5.2 (maybe you haven\'t updated the DSN when upgrading from 5.1).');
}

$accessToken = $this->getUser($dsn);
$channel = $dsn->getOption('channel');
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
$port = $dsn->getPort();

if ('slack' === $scheme) {
return (new SlackTransport($accessToken, $channel, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
}

throw new UnsupportedSchemeException($dsn, 'slack', $this->getSupportedSchemes());
return (new SlackTransport($accessToken, $channel, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
}

protected function getSupportedSchemes(): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\Notifier\Bridge\Slack\SlackTransportFactory;
use Symfony\Component\Notifier\Exception\IncompleteDsnException;
use Symfony\Component\Notifier\Exception\InvalidArgumentException;
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
use Symfony\Component\Notifier\Transport\Dsn;

Expand All @@ -30,6 +31,15 @@ public function testCreateWithDsn(): void
$this->assertSame(sprintf('slack://%s?channel=%s', $host, $channel), (string) $transport);
}

public function testCreateWithDeprecatedDsn(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Support for Slack webhook DSN has been dropped since 5.2 (maybe you haven\'t updated the DSN when upgrading from 5.1).');

$factory = new SlackTransportFactory();
$factory->create(Dsn::fromString('slack://default/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX'));
}

public function testCreateWithNoTokenThrowsMalformed(): void
{
$factory = new SlackTransportFactory();
Expand Down