Skip to content

Commit 26ce5b4

Browse files
Merge branch '5.1' into 5.2
* 5.1: [Notifier] [Mattermost] Host is required
2 parents 7b4af4b + 8797138 commit 26ce5b4

File tree

5 files changed

+35
-5
lines changed

5 files changed

+35
-5
lines changed

src/Symfony/Component/Notifier/Bridge/Mattermost/MattermostTransport.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@ final class MattermostTransport extends AbstractTransport
2929
{
3030
private $token;
3131
private $channel;
32+
private $path;
3233

33-
public function __construct(string $token, string $channel, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
34+
public function __construct(string $token, string $channel, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null, string $path = null)
3435
{
3536
$this->token = $token;
3637
$this->channel = $channel;
38+
$this->path = $path;
3739

3840
parent::__construct($client, $dispatcher);
3941
}
@@ -57,14 +59,15 @@ protected function doSend(MessageInterface $message): SentMessage
5759
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" (instance of "%s" given).', __CLASS__, ChatMessage::class, get_debug_type($message)));
5860
}
5961

60-
$endpoint = sprintf('https://%s/api/v4/posts', $this->getEndpoint());
61-
6262
$options = ($opts = $message->getOptions()) ? $opts->toArray() : [];
6363
$options['message'] = $message->getSubject();
6464

6565
if (!isset($options['channel_id'])) {
6666
$options['channel_id'] = $message->getRecipientId() ?: $this->channel;
6767
}
68+
69+
$endpoint = sprintf('https://%s/api/v4/posts', $this->getEndpoint());
70+
6871
$response = $this->client->request('POST', $endpoint, [
6972
'auth_bearer' => $this->token,
7073
'json' => array_filter($options),
@@ -88,4 +91,9 @@ protected function getEndpoint(): ?string
8891
{
8992
return $this->host.($this->port ? ':'.$this->port : '');
9093
}
94+
95+
protected function getEndpoint(): ?string
96+
{
97+
return rtrim($this->host.($this->port ? ':'.$this->port : '').($this->path ?? ''), '/');
98+
}
9199
}

src/Symfony/Component/Notifier/Bridge/Mattermost/MattermostTransportFactory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public function create(Dsn $dsn): TransportInterface
3232
throw new UnsupportedSchemeException($dsn, 'mattermost', $this->getSupportedSchemes());
3333
}
3434

35+
$path = $dsn->getPath();
3536
$token = $this->getUser($dsn);
3637
$channel = $dsn->getOption('channel');
3738

@@ -42,7 +43,7 @@ public function create(Dsn $dsn): TransportInterface
4243
$host = $dsn->getHost();
4344
$port = $dsn->getPort();
4445

45-
return (new MattermostTransport($token, $channel, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
46+
return (new MattermostTransport($token, $channel, $this->client, $this->dispatcher, $path))->setHost($host)->setPort($port);
4647
}
4748

4849
protected function getSupportedSchemes(): array

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ DSN example
77
-----------
88

99
```
10-
MATTERMOST_DSN=mattermost://ACCESS_TOKEN@default?channel=CHANNEL
10+
MATTERMOST_DSN=mattermost://ACCESS_TOKEN@HOST/PATH?channel=CHANNEL
1111
```
1212

1313
where:
1414
- `ACCESS_TOKEN` is your Mattermost access token
15+
- `HOST` is your Mattermost host
16+
- `PATH` is your Mattermost sub-path (optional)
1517
- `CHANNEL` is your Mattermost channel
1618

1719
Resources

src/Symfony/Component/Notifier/Bridge/Mattermost/Tests/MattermostTransportFactoryTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,24 @@ public function testCreateWithDsn()
3131
$this->assertSame('mattermost://host.test?channel=testChannel', (string) $transport);
3232
}
3333

34+
public function testCreateWithDsnHostWithSubfolder()
35+
{
36+
$factory = $this->createFactory();
37+
38+
$transport = $factory->create(Dsn::fromString('mattermost://accessToken@example.com/sub?channel=testChannel'));
39+
40+
$this->assertSame('mattermost://example.com/sub?channel=testChannel', (string) $transport);
41+
}
42+
43+
public function testCreateWithDsnHostWithSubfolderWithTrailingSlash()
44+
{
45+
$factory = $this->createFactory();
46+
47+
$transport = $factory->create(Dsn::fromString('mattermost://accessToken@example.com/sub/?channel=testChannel'));
48+
49+
$this->assertSame('mattermost://example.com/sub?channel=testChannel', (string) $transport);
50+
}
51+
3452
public function testCreateWithMissingOptionChannelThrowsIncompleteDsnException()
3553
{
3654
$factory = $this->createFactory();

src/Symfony/Component/Notifier/Bridge/Mattermost/Tests/MattermostTransportTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Notifier\Exception\LogicException;
1717
use Symfony\Component\Notifier\Message\ChatMessage;
1818
use Symfony\Component\Notifier\Message\MessageInterface;
19+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
1920
use Symfony\Contracts\HttpClient\HttpClientInterface;
2021

2122
/**

0 commit comments

Comments
 (0)