diff --git a/src/Symfony/Component/Notifier/Bridge/Mattermost/MattermostTransport.php b/src/Symfony/Component/Notifier/Bridge/Mattermost/MattermostTransport.php index 23afec66bb7df..3cd646f612ab0 100644 --- a/src/Symfony/Component/Notifier/Bridge/Mattermost/MattermostTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/Mattermost/MattermostTransport.php @@ -28,11 +28,13 @@ final class MattermostTransport extends AbstractTransport { private $token; private $channel; + private $path; - public function __construct(string $token, string $channel, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null) + public function __construct(string $token, string $channel, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null, string $path = null) { $this->token = $token; $this->channel = $channel; + $this->path = $path; parent::__construct($client, $dispatcher); } @@ -56,14 +58,15 @@ protected function doSend(MessageInterface $message): void throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" (instance of "%s" given).', __CLASS__, ChatMessage::class, get_debug_type($message))); } - $endpoint = sprintf('https://%s/api/v4/posts', $this->getEndpoint()); - $options = ($opts = $message->getOptions()) ? $opts->toArray() : []; $options['message'] = $message->getSubject(); if (!isset($options['channel_id'])) { $options['channel_id'] = $message->getRecipientId() ?: $this->channel; } + + $endpoint = sprintf('https://%s/api/v4/posts', $this->getEndpoint()); + $response = $this->client->request('POST', $endpoint, [ 'auth_bearer' => $this->token, 'json' => array_filter($options), @@ -75,4 +78,9 @@ protected function doSend(MessageInterface $message): void throw new TransportException(sprintf('Unable to post the Mattermost message: %s (%s).', $result['message'], $result['id']), $response); } } + + protected function getEndpoint(): ?string + { + return rtrim($this->host.($this->port ? ':'.$this->port : '').($this->path ?? ''), '/'); + } } diff --git a/src/Symfony/Component/Notifier/Bridge/Mattermost/MattermostTransportFactory.php b/src/Symfony/Component/Notifier/Bridge/Mattermost/MattermostTransportFactory.php index ad057c6151c21..16ef2dd2fff54 100644 --- a/src/Symfony/Component/Notifier/Bridge/Mattermost/MattermostTransportFactory.php +++ b/src/Symfony/Component/Notifier/Bridge/Mattermost/MattermostTransportFactory.php @@ -32,6 +32,7 @@ public function create(Dsn $dsn): TransportInterface throw new UnsupportedSchemeException($dsn, 'mattermost', $this->getSupportedSchemes()); } + $path = $dsn->getPath(); $token = $this->getUser($dsn); $channel = $dsn->getOption('channel'); @@ -42,7 +43,7 @@ public function create(Dsn $dsn): TransportInterface $host = $dsn->getHost(); $port = $dsn->getPort(); - return (new MattermostTransport($token, $channel, $this->client, $this->dispatcher))->setHost($host)->setPort($port); + return (new MattermostTransport($token, $channel, $this->client, $this->dispatcher, $path))->setHost($host)->setPort($port); } protected function getSupportedSchemes(): array diff --git a/src/Symfony/Component/Notifier/Bridge/Mattermost/README.md b/src/Symfony/Component/Notifier/Bridge/Mattermost/README.md index bbe64754eef6a..5341a22cc078d 100644 --- a/src/Symfony/Component/Notifier/Bridge/Mattermost/README.md +++ b/src/Symfony/Component/Notifier/Bridge/Mattermost/README.md @@ -7,11 +7,13 @@ DSN example ----------- ``` -MATTERMOST_DSN=mattermost://ACCESS_TOKEN@default?channel=CHANNEL +MATTERMOST_DSN=mattermost://ACCESS_TOKEN@HOST/PATH?channel=CHANNEL ``` where: - `ACCESS_TOKEN` is your Mattermost access token + - `HOST` is your Mattermost host + - `PATH` is your Mattermost sub-path (optional) - `CHANNEL` is your Mattermost channel Resources diff --git a/src/Symfony/Component/Notifier/Bridge/Mattermost/Tests/MattermostTransportFactoryTest.php b/src/Symfony/Component/Notifier/Bridge/Mattermost/Tests/MattermostTransportFactoryTest.php index daa774130a1f9..a955e22e8cb57 100644 --- a/src/Symfony/Component/Notifier/Bridge/Mattermost/Tests/MattermostTransportFactoryTest.php +++ b/src/Symfony/Component/Notifier/Bridge/Mattermost/Tests/MattermostTransportFactoryTest.php @@ -31,6 +31,24 @@ public function testCreateWithDsn() $this->assertSame('mattermost://host.test?channel=testChannel', (string) $transport); } + public function testCreateWithDsnHostWithSubfolder() + { + $factory = $this->createFactory(); + + $transport = $factory->create(Dsn::fromString('mattermost://accessToken@example.com/sub?channel=testChannel')); + + $this->assertSame('mattermost://example.com/sub?channel=testChannel', (string) $transport); + } + + public function testCreateWithDsnHostWithSubfolderWithTrailingSlash() + { + $factory = $this->createFactory(); + + $transport = $factory->create(Dsn::fromString('mattermost://accessToken@example.com/sub/?channel=testChannel')); + + $this->assertSame('mattermost://example.com/sub?channel=testChannel', (string) $transport); + } + public function testCreateWithMissingOptionChannelThrowsIncompleteDsnException() { $factory = $this->createFactory(); diff --git a/src/Symfony/Component/Notifier/Bridge/Mattermost/Tests/MattermostTransportTest.php b/src/Symfony/Component/Notifier/Bridge/Mattermost/Tests/MattermostTransportTest.php index 7bd203baafb7a..606f610c4db9b 100644 --- a/src/Symfony/Component/Notifier/Bridge/Mattermost/Tests/MattermostTransportTest.php +++ b/src/Symfony/Component/Notifier/Bridge/Mattermost/Tests/MattermostTransportTest.php @@ -16,6 +16,7 @@ use Symfony\Component\Notifier\Exception\LogicException; use Symfony\Component\Notifier\Message\ChatMessage; use Symfony\Component\Notifier\Message\MessageInterface; +use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; use Symfony\Contracts\HttpClient\HttpClientInterface; /**