Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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),
Expand All @@ -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 ?? ''), '/');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down