[Notifier] Add exception for deprecated slack dsn

This commit is contained in:
Malte Schlüter 2020-12-04 12:31:09 +01:00
parent 937c403f15
commit 6b56b4c6f9
3 changed files with 32 additions and 6 deletions

View File

@ -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
---------

View File

@ -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;
@ -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

View File

@ -14,6 +14,7 @@ namespace Symfony\Component\Notifier\Bridge\Slack\Tests;
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;
@ -30,6 +31,15 @@ final class SlackTransportFactoryTest extends TestCase
$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();