Skip to content

Commit

Permalink
feature #33875 Add Mattermost notifier bridge (thePanz)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fabpot committed Feb 10, 2020
2 parents 9eb7cb1 + eaba6a5 commit 11f1312
Show file tree
Hide file tree
Showing 15 changed files with 246 additions and 4 deletions.
Expand Up @@ -90,6 +90,7 @@
use Symfony\Component\Messenger\Transport\TransportInterface;
use Symfony\Component\Mime\MimeTypeGuesserInterface;
use Symfony\Component\Mime\MimeTypes;
use Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory;
use Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory;
use Symfony\Component\Notifier\Bridge\Slack\SlackTransportFactory;
use Symfony\Component\Notifier\Bridge\Telegram\TelegramTransportFactory;
Expand Down Expand Up @@ -1997,6 +1998,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
$classToServices = [
SlackTransportFactory::class => 'notifier.transport_factory.slack',
TelegramTransportFactory::class => 'notifier.transport_factory.telegram',
MattermostTransportFactory::class => 'notifier.transport_factory.mattermost',
NexmoTransportFactory::class => 'notifier.transport_factory.nexmo',
TwilioTransportFactory::class => 'notifier.transport_factory.twilio',
];
Expand Down
Expand Up @@ -18,6 +18,10 @@
<tag name="chatter.transport_factory" />
</service>

<service id="notifier.transport_factory.mattermost" class="Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory" parent="notifier.transport_factory.abstract">
<tag name="chatter.transport_factory" />
</service>

<service id="notifier.transport_factory.nexmo" class="Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory" parent="notifier.transport_factory.abstract">
<tag name="texter.transport_factory" />
</service>
Expand Down
@@ -0,0 +1,2 @@
/Tests export-ignore
/phpunit.xml.dist export-ignore
7 changes: 7 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Mattermost/CHANGELOG.md
@@ -0,0 +1,7 @@
CHANGELOG
=========

5.1.0
-----

* Added the bridge
19 changes: 19 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Mattermost/LICENSE
@@ -0,0 +1,19 @@
Copyright (c) 2020 Fabien Potencier

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
@@ -0,0 +1,78 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\Mattermost;

use Symfony\Component\Notifier\Exception\LogicException;
use Symfony\Component\Notifier\Exception\TransportException;
use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\Notifier\Message\MessageInterface;
use Symfony\Component\Notifier\Transport\AbstractTransport;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
* @author Emanuele Panzeri <thepanz@gmail.com>
*
* @experimental in 5.1
*/
final class MattermostTransport extends AbstractTransport
{
private $token;
private $channel;

public function __construct(string $token, string $channel, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
{
$this->token = $token;
$this->channel = $channel;

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

public function __toString(): string
{
return sprintf('mattermost://%s?channel=%s', $this->getEndpoint(), $this->channel);
}

public function supports(MessageInterface $message): bool
{
return $message instanceof ChatMessage;
}

/**
* @see https://api.mattermost.com
*/
protected function doSend(MessageInterface $message): void
{
if (!$message instanceof ChatMessage) {
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" (instance of "%s" given).', __CLASS__, ChatMessage::class, \get_class($message)));
}

$endpoint = sprintf('https://%s/api/v4/post', $this->getEndpoint());

$options = ($opts = $message->getOptions()) ? $opts->toArray() : [];
$options['message'] = $message->getSubject();

if (!isset($options['channel_id'])) {
$options['channel_id'] = $message->getRecipientId() ?: $this->channel;
}
$response = $this->client->request('POST', $endpoint, [
'bearer' => $this->token,
'json' => array_filter($options),
]);

if (200 !== $response->getStatusCode()) {
$result = $response->toArray(false);

throw new TransportException(sprintf('Unable to post the Mattermost message: %s (%s).', $result['message'], $result['id']), $response);
}
}
}
@@ -0,0 +1,45 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\Mattermost;

use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
use Symfony\Component\Notifier\Transport\Dsn;
use Symfony\Component\Notifier\Transport\TransportInterface;

/**
* @author Emanuele Panzeri <thepanz@gmail.com>
*
* @experimental in 5.1
*/
final class MattermostTransportFactory extends AbstractTransportFactory
{
public function create(Dsn $dsn): TransportInterface
{
$scheme = $dsn->getScheme();
$token = $this->getUser($dsn);
$channel = $dsn->getOption('channel');
$host = $dsn->getHost();
$port = $dsn->getPort();

if ('mattermost' === $scheme) {
return (new MattermostTransport($token, $channel, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
}

throw new UnsupportedSchemeException($dsn, 'mattermost', $this->getSupportedSchemes());
}

protected function getSupportedSchemes(): array
{
return ['mattermost'];
}
}
12 changes: 12 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Mattermost/README.md
@@ -0,0 +1,12 @@
Mattermost Notifier
===================

Provides Mattermost integration for Symfony Notifier.

Resources
---------

* [Contributing](https://symfony.com/doc/current/contributing/index.html)
* [Report issues](https://github.com/symfony/symfony/issues) and
[send Pull Requests](https://github.com/symfony/symfony/pulls)
in the [main Symfony repository](https://github.com/symfony/symfony)
35 changes: 35 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Mattermost/composer.json
@@ -0,0 +1,35 @@
{
"name": "symfony/mattermost-notifier",
"type": "symfony-bridge",
"description": "Symfony Mattermost Notifier Bridge",
"keywords": ["mattermost", "notifier"],
"homepage": "https://symfony.com",
"license": "MIT",
"authors": [
{
"name": "Emanuele Panzeri",
"email": "thepanz@gmail.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"require": {
"php": "^7.2.5",
"symfony/http-client": "^4.3|^5.0",
"symfony/notifier": "^5.0"
},
"autoload": {
"psr-4": { "Symfony\\Component\\Notifier\\Bridge\\Mattermost\\": "" },
"exclude-from-classmap": [
"/Tests/"
]
},
"minimum-stability": "dev",
"extra": {
"branch-alias": {
"dev-master": "5.1-dev"
}
}
}
31 changes: 31 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Mattermost/phpunit.xml.dist
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/5.2/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="vendor/autoload.php"
failOnRisky="true"
failOnWarning="true"
>
<php>
<ini name="error_reporting" value="-1" />
</php>

<testsuites>
<testsuite name="Symfony Mattermost Notifier Bridge Test Suite">
<directory>./Tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>./</directory>
<exclude>
<directory>./Resources</directory>
<directory>./Tests</directory>
<directory>./vendor</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
Expand Up @@ -33,10 +33,10 @@ final class SlackTransport extends AbstractTransport
private $accessToken;
private $chatChannel;

public function __construct(string $accessToken, string $chatChannel = null, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
public function __construct(string $accessToken, string $channel = null, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
{
$this->accessToken = $accessToken;
$this->chatChannel = $chatChannel;
$this->chatChannel = $channel;
$this->client = $client;

parent::__construct($client, $dispatcher);
Expand Down
Expand Up @@ -38,10 +38,10 @@ final class TelegramTransport extends AbstractTransport
private $token;
private $chatChannel;

public function __construct(string $token, string $chatChannel = null, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
public function __construct(string $token, string $channel = null, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
{
$this->token = $token;
$this->chatChannel = $chatChannel;
$this->chatChannel = $channel;
$this->client = $client;

parent::__construct($client, $dispatcher);
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Notifier/CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
5.1.0
-----

* Added the Mattermost notifier bridge
* [BC BREAK] The `ChatMessage::fromNotification()` method's `$recipient` and `$transport`
arguments were removed.
* [BC BREAK] The `EmailMessage::fromNotification()` and `SmsMessage::fromNotification()`
Expand Down
Expand Up @@ -30,6 +30,10 @@ class UnsupportedSchemeException extends LogicException
'class' => Bridge\Telegram\TelegramTransportFactory::class,
'package' => 'symfony/telegram-notifier',
],
'mattermost' => [
'class' => Bridge\Mattermost\MattermostTransportFactory::class,
'package' => 'symfony/mattermost-notifier',
],
'nexmo' => [
'class' => Bridge\Nexmo\NexmoTransportFactory::class,
'package' => 'symfony/nexmo-notifier',
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/Notifier/Transport.php
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Notifier;

use Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory;
use Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory;
use Symfony\Component\Notifier\Bridge\Slack\SlackTransportFactory;
use Symfony\Component\Notifier\Bridge\Telegram\TelegramTransportFactory;
Expand All @@ -36,6 +37,7 @@ class Transport
private const FACTORY_CLASSES = [
SlackTransportFactory::class,
TelegramTransportFactory::class,
MattermostTransportFactory::class,
NexmoTransportFactory::class,
TwilioTransportFactory::class,
];
Expand Down

0 comments on commit 11f1312

Please sign in to comment.