Skip to content

Commit 11f1312

Browse files
committed
feature #33875 Add Mattermost notifier bridge (thePanz)
This PR was merged into the 5.1-dev branch. Discussion ---------- Add Mattermost notifier bridge | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | See #33687 | License | MIT | Doc PR | Added a http://mattermost.org (open source Slack alternative) transport for the new Notifier component Commits ------- eaba6a5 Add Mattermost notifier bridge
2 parents 9eb7cb1 + eaba6a5 commit 11f1312

File tree

15 files changed

+246
-4
lines changed

15 files changed

+246
-4
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

+2
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
use Symfony\Component\Messenger\Transport\TransportInterface;
9191
use Symfony\Component\Mime\MimeTypeGuesserInterface;
9292
use Symfony\Component\Mime\MimeTypes;
93+
use Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory;
9394
use Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory;
9495
use Symfony\Component\Notifier\Bridge\Slack\SlackTransportFactory;
9596
use Symfony\Component\Notifier\Bridge\Telegram\TelegramTransportFactory;
@@ -1997,6 +1998,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
19971998
$classToServices = [
19981999
SlackTransportFactory::class => 'notifier.transport_factory.slack',
19992000
TelegramTransportFactory::class => 'notifier.transport_factory.telegram',
2001+
MattermostTransportFactory::class => 'notifier.transport_factory.mattermost',
20002002
NexmoTransportFactory::class => 'notifier.transport_factory.nexmo',
20012003
TwilioTransportFactory::class => 'notifier.transport_factory.twilio',
20022004
];

src/Symfony/Bundle/FrameworkBundle/Resources/config/notifier_transports.xml

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
<tag name="chatter.transport_factory" />
1919
</service>
2020

21+
<service id="notifier.transport_factory.mattermost" class="Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory" parent="notifier.transport_factory.abstract">
22+
<tag name="chatter.transport_factory" />
23+
</service>
24+
2125
<service id="notifier.transport_factory.nexmo" class="Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory" parent="notifier.transport_factory.abstract">
2226
<tag name="texter.transport_factory" />
2327
</service>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/Tests export-ignore
2+
/phpunit.xml.dist export-ignore
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CHANGELOG
2+
=========
3+
4+
5.1.0
5+
-----
6+
7+
* Added the bridge
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2020 Fabien Potencier
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is furnished
8+
to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\Mattermost;
13+
14+
use Symfony\Component\Notifier\Exception\LogicException;
15+
use Symfony\Component\Notifier\Exception\TransportException;
16+
use Symfony\Component\Notifier\Message\ChatMessage;
17+
use Symfony\Component\Notifier\Message\MessageInterface;
18+
use Symfony\Component\Notifier\Transport\AbstractTransport;
19+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
20+
use Symfony\Contracts\HttpClient\HttpClientInterface;
21+
22+
/**
23+
* @author Emanuele Panzeri <thepanz@gmail.com>
24+
*
25+
* @experimental in 5.1
26+
*/
27+
final class MattermostTransport extends AbstractTransport
28+
{
29+
private $token;
30+
private $channel;
31+
32+
public function __construct(string $token, string $channel, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
33+
{
34+
$this->token = $token;
35+
$this->channel = $channel;
36+
37+
parent::__construct($client, $dispatcher);
38+
}
39+
40+
public function __toString(): string
41+
{
42+
return sprintf('mattermost://%s?channel=%s', $this->getEndpoint(), $this->channel);
43+
}
44+
45+
public function supports(MessageInterface $message): bool
46+
{
47+
return $message instanceof ChatMessage;
48+
}
49+
50+
/**
51+
* @see https://api.mattermost.com
52+
*/
53+
protected function doSend(MessageInterface $message): void
54+
{
55+
if (!$message instanceof ChatMessage) {
56+
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" (instance of "%s" given).', __CLASS__, ChatMessage::class, \get_class($message)));
57+
}
58+
59+
$endpoint = sprintf('https://%s/api/v4/post', $this->getEndpoint());
60+
61+
$options = ($opts = $message->getOptions()) ? $opts->toArray() : [];
62+
$options['message'] = $message->getSubject();
63+
64+
if (!isset($options['channel_id'])) {
65+
$options['channel_id'] = $message->getRecipientId() ?: $this->channel;
66+
}
67+
$response = $this->client->request('POST', $endpoint, [
68+
'bearer' => $this->token,
69+
'json' => array_filter($options),
70+
]);
71+
72+
if (200 !== $response->getStatusCode()) {
73+
$result = $response->toArray(false);
74+
75+
throw new TransportException(sprintf('Unable to post the Mattermost message: %s (%s).', $result['message'], $result['id']), $response);
76+
}
77+
}
78+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\Mattermost;
13+
14+
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
15+
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
16+
use Symfony\Component\Notifier\Transport\Dsn;
17+
use Symfony\Component\Notifier\Transport\TransportInterface;
18+
19+
/**
20+
* @author Emanuele Panzeri <thepanz@gmail.com>
21+
*
22+
* @experimental in 5.1
23+
*/
24+
final class MattermostTransportFactory extends AbstractTransportFactory
25+
{
26+
public function create(Dsn $dsn): TransportInterface
27+
{
28+
$scheme = $dsn->getScheme();
29+
$token = $this->getUser($dsn);
30+
$channel = $dsn->getOption('channel');
31+
$host = $dsn->getHost();
32+
$port = $dsn->getPort();
33+
34+
if ('mattermost' === $scheme) {
35+
return (new MattermostTransport($token, $channel, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
36+
}
37+
38+
throw new UnsupportedSchemeException($dsn, 'mattermost', $this->getSupportedSchemes());
39+
}
40+
41+
protected function getSupportedSchemes(): array
42+
{
43+
return ['mattermost'];
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Mattermost Notifier
2+
===================
3+
4+
Provides Mattermost integration for Symfony Notifier.
5+
6+
Resources
7+
---------
8+
9+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
10+
* [Report issues](https://github.com/symfony/symfony/issues) and
11+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
12+
in the [main Symfony repository](https://github.com/symfony/symfony)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "symfony/mattermost-notifier",
3+
"type": "symfony-bridge",
4+
"description": "Symfony Mattermost Notifier Bridge",
5+
"keywords": ["mattermost", "notifier"],
6+
"homepage": "https://symfony.com",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "Emanuele Panzeri",
11+
"email": "thepanz@gmail.com"
12+
},
13+
{
14+
"name": "Symfony Community",
15+
"homepage": "https://symfony.com/contributors"
16+
}
17+
],
18+
"require": {
19+
"php": "^7.2.5",
20+
"symfony/http-client": "^4.3|^5.0",
21+
"symfony/notifier": "^5.0"
22+
},
23+
"autoload": {
24+
"psr-4": { "Symfony\\Component\\Notifier\\Bridge\\Mattermost\\": "" },
25+
"exclude-from-classmap": [
26+
"/Tests/"
27+
]
28+
},
29+
"minimum-stability": "dev",
30+
"extra": {
31+
"branch-alias": {
32+
"dev-master": "5.1-dev"
33+
}
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/5.2/phpunit.xsd"
5+
backupGlobals="false"
6+
colors="true"
7+
bootstrap="vendor/autoload.php"
8+
failOnRisky="true"
9+
failOnWarning="true"
10+
>
11+
<php>
12+
<ini name="error_reporting" value="-1" />
13+
</php>
14+
15+
<testsuites>
16+
<testsuite name="Symfony Mattermost Notifier Bridge Test Suite">
17+
<directory>./Tests/</directory>
18+
</testsuite>
19+
</testsuites>
20+
21+
<filter>
22+
<whitelist>
23+
<directory>./</directory>
24+
<exclude>
25+
<directory>./Resources</directory>
26+
<directory>./Tests</directory>
27+
<directory>./vendor</directory>
28+
</exclude>
29+
</whitelist>
30+
</filter>
31+
</phpunit>

src/Symfony/Component/Notifier/Bridge/Slack/SlackTransport.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ final class SlackTransport extends AbstractTransport
3333
private $accessToken;
3434
private $chatChannel;
3535

36-
public function __construct(string $accessToken, string $chatChannel = null, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
36+
public function __construct(string $accessToken, string $channel = null, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
3737
{
3838
$this->accessToken = $accessToken;
39-
$this->chatChannel = $chatChannel;
39+
$this->chatChannel = $channel;
4040
$this->client = $client;
4141

4242
parent::__construct($client, $dispatcher);

src/Symfony/Component/Notifier/Bridge/Telegram/TelegramTransport.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ final class TelegramTransport extends AbstractTransport
3838
private $token;
3939
private $chatChannel;
4040

41-
public function __construct(string $token, string $chatChannel = null, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
41+
public function __construct(string $token, string $channel = null, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
4242
{
4343
$this->token = $token;
44-
$this->chatChannel = $chatChannel;
44+
$this->chatChannel = $channel;
4545
$this->client = $client;
4646

4747
parent::__construct($client, $dispatcher);

src/Symfony/Component/Notifier/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
5.1.0
55
-----
66

7+
* Added the Mattermost notifier bridge
78
* [BC BREAK] The `ChatMessage::fromNotification()` method's `$recipient` and `$transport`
89
arguments were removed.
910
* [BC BREAK] The `EmailMessage::fromNotification()` and `SmsMessage::fromNotification()`

src/Symfony/Component/Notifier/Exception/UnsupportedSchemeException.php

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ class UnsupportedSchemeException extends LogicException
3030
'class' => Bridge\Telegram\TelegramTransportFactory::class,
3131
'package' => 'symfony/telegram-notifier',
3232
],
33+
'mattermost' => [
34+
'class' => Bridge\Mattermost\MattermostTransportFactory::class,
35+
'package' => 'symfony/mattermost-notifier',
36+
],
3337
'nexmo' => [
3438
'class' => Bridge\Nexmo\NexmoTransportFactory::class,
3539
'package' => 'symfony/nexmo-notifier',

src/Symfony/Component/Notifier/Transport.php

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Notifier;
1313

14+
use Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory;
1415
use Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory;
1516
use Symfony\Component\Notifier\Bridge\Slack\SlackTransportFactory;
1617
use Symfony\Component\Notifier\Bridge\Telegram\TelegramTransportFactory;
@@ -36,6 +37,7 @@ class Transport
3637
private const FACTORY_CLASSES = [
3738
SlackTransportFactory::class,
3839
TelegramTransportFactory::class,
40+
MattermostTransportFactory::class,
3941
NexmoTransportFactory::class,
4042
TwilioTransportFactory::class,
4143
];

0 commit comments

Comments
 (0)