Skip to content

Commit 4818b28

Browse files
committed
feature #39838 [Notifier] Add Gitter Bridge (christingruber)
This PR was merged into the 5.3-dev branch. Discussion ---------- [Notifier] Add Gitter Bridge | Q | A | ------------- | --- | Branch? | 5.x | Bug fix? | no | New feature? | yes | Deprecations? | no | License | MIT | Doc PR | symfony/symfony-docs#14835 Adds a notifier bridge for https://gitter.im the API documentation https://developer.gitter.im/docs/rest-api. Commits ------- d033677 Add symfony/gitter-notifier bridge
2 parents c6100bc + d033677 commit 4818b28

File tree

14 files changed

+357
-0
lines changed

14 files changed

+357
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
use Symfony\Component\Notifier\Bridge\Firebase\FirebaseTransportFactory;
111111
use Symfony\Component\Notifier\Bridge\FreeMobile\FreeMobileTransportFactory;
112112
use Symfony\Component\Notifier\Bridge\GatewayApi\GatewayApiTransportFactory;
113+
use Symfony\Component\Notifier\Bridge\Gitter\GitterTransportFactory;
113114
use Symfony\Component\Notifier\Bridge\GoogleChat\GoogleChatTransportFactory;
114115
use Symfony\Component\Notifier\Bridge\Infobip\InfobipTransportFactory;
115116
use Symfony\Component\Notifier\Bridge\Iqsms\IqsmsTransportFactory;
@@ -2247,6 +2248,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
22472248
GatewayApiTransportFactory::class => 'notifier.transport_factory.gatewayapi',
22482249
OctopushTransportFactory::class => 'notifier.transport_factory.octopush',
22492250
MercureTransportFactory::class => 'notifier.transport_factory.mercure',
2251+
GitterTransportFactory::class => 'notifier.transport_factory.gitter',
22502252
];
22512253

22522254
foreach ($classToServices as $class => $service) {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Notifier\Bridge\Firebase\FirebaseTransportFactory;
1818
use Symfony\Component\Notifier\Bridge\FreeMobile\FreeMobileTransportFactory;
1919
use Symfony\Component\Notifier\Bridge\GatewayApi\GatewayApiTransportFactory;
20+
use Symfony\Component\Notifier\Bridge\Gitter\GitterTransportFactory;
2021
use Symfony\Component\Notifier\Bridge\GoogleChat\GoogleChatTransportFactory;
2122
use Symfony\Component\Notifier\Bridge\Infobip\InfobipTransportFactory;
2223
use Symfony\Component\Notifier\Bridge\Iqsms\IqsmsTransportFactory;
@@ -140,6 +141,10 @@
140141
->parent('notifier.transport_factory.abstract')
141142
->tag('chatter.transport_factory')
142143

144+
->set('notifier.transport_factory.gitter', GitterTransportFactory::class)
145+
->parent('notifier.transport_factory.abstract')
146+
->tag('chatter.transport_factory')
147+
143148
->set('notifier.transport_factory.null', NullTransportFactory::class)
144149
->parent('notifier.transport_factory.abstract')
145150
->tag('chatter.transport_factory')
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/Tests export-ignore
2+
/phpunit.xml.dist export-ignore
3+
/.gitattributes export-ignore
4+
/.gitignore export-ignore
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CHANGELOG
2+
=========
3+
4+
5.3
5+
---
6+
7+
* Add the bridge
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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\Gitter;
13+
14+
use Symfony\Component\HttpFoundation\Response;
15+
use Symfony\Component\Notifier\Exception\TransportException;
16+
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
17+
use Symfony\Component\Notifier\Message\ChatMessage;
18+
use Symfony\Component\Notifier\Message\MessageInterface;
19+
use Symfony\Component\Notifier\Message\SentMessage;
20+
use Symfony\Component\Notifier\Transport\AbstractTransport;
21+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
22+
use Symfony\Contracts\HttpClient\HttpClientInterface;
23+
24+
/**
25+
* @author Christin Gruber <c.gruber@touchdesign.de>
26+
*/
27+
final class GitterTransport extends AbstractTransport
28+
{
29+
protected const HOST = 'api.gitter.im';
30+
31+
private $token;
32+
private $roomId;
33+
34+
public function __construct(string $token, string $roomId, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
35+
{
36+
$this->token = $token;
37+
$this->roomId = $roomId;
38+
39+
parent::__construct($client, $dispatcher);
40+
}
41+
42+
public function __toString(): string
43+
{
44+
return sprintf('gitter://%s?room_id=%s', $this->getEndpoint(), $this->roomId);
45+
}
46+
47+
public function supports(MessageInterface $message): bool
48+
{
49+
return $message instanceof ChatMessage;
50+
}
51+
52+
/**
53+
* @see https://developer.gitter.im/docs/rest-api
54+
*/
55+
protected function doSend(MessageInterface $message): SentMessage
56+
{
57+
if (!$message instanceof ChatMessage) {
58+
throw new UnsupportedMessageTypeException(__CLASS__, ChatMessage::class, $message);
59+
}
60+
61+
$endpoint = sprintf('https://%s/v1/rooms/%s/chatMessages', $this->getEndpoint(), $this->roomId);
62+
63+
$response = $this->client->request('POST', $endpoint, [
64+
'auth_bearer' => $this->token,
65+
'json' => [
66+
'text' => $message->getSubject(),
67+
],
68+
]);
69+
70+
$result = $response->toArray(false);
71+
72+
if (Response::HTTP_OK !== $response->getStatusCode()) {
73+
throw new TransportException(sprintf('Unable to post the Gitter message: "%s".', $result['error']), $response);
74+
}
75+
76+
$sentMessage = new SentMessage($message, (string) $this);
77+
$sentMessage->setMessageId($result['id']);
78+
79+
return $sentMessage;
80+
}
81+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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\Gitter;
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 Christin Gruber <c.gruber@touchdesign.de>
21+
*/
22+
final class GitterTransportFactory extends AbstractTransportFactory
23+
{
24+
public function create(Dsn $dsn): TransportInterface
25+
{
26+
$scheme = $dsn->getScheme();
27+
28+
if ('gitter' !== $scheme) {
29+
throw new UnsupportedSchemeException($dsn, 'gitter', $this->getSupportedSchemes());
30+
}
31+
32+
$token = $this->getUser($dsn);
33+
$roomId = $dsn->getRequiredOption('room_id');
34+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
35+
$port = $dsn->getPort();
36+
37+
return (new GitterTransport($token, $roomId, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
38+
}
39+
40+
protected function getSupportedSchemes(): array
41+
{
42+
return ['gitter'];
43+
}
44+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2021 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.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Gitter Notifier
2+
===============
3+
4+
Provides [Gitter](https://gitter.im) integration for Symfony Notifier.
5+
6+
DSN example
7+
-----------
8+
9+
```
10+
GITTER_DSN=gitter://TOKEN@default?room_id=ROOM_ID
11+
```
12+
13+
where:
14+
- `TOKEN` is your Gitter token
15+
- `ROOM_ID` is your Gitter room id
16+
17+
Resources
18+
---------
19+
20+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
21+
* [Report issues](https://github.com/symfony/symfony/issues) and
22+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
23+
in the [main Symfony repository](https://github.com/symfony/symfony)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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\Gitter\Tests;
13+
14+
use Symfony\Component\Notifier\Bridge\Gitter\GitterTransportFactory;
15+
use Symfony\Component\Notifier\Tests\TransportFactoryTestCase;
16+
use Symfony\Component\Notifier\Transport\TransportFactoryInterface;
17+
18+
/**
19+
* @author Christin Gruber <c.gruber@touchdesign.de>
20+
*/
21+
final class GitterTransportFactoryTest extends TransportFactoryTestCase
22+
{
23+
public function createFactory(): TransportFactoryInterface
24+
{
25+
return new GitterTransportFactory();
26+
}
27+
28+
public function createProvider(): iterable
29+
{
30+
yield [
31+
'gitter://api.gitter.im?room_id=5539a3ee5etest0d3255bfef',
32+
'gitter://token@api.gitter.im?room_id=5539a3ee5etest0d3255bfef',
33+
];
34+
}
35+
36+
public function supportsProvider(): iterable
37+
{
38+
yield [true, 'gitter://token@host?room_id=5539a3ee5etest0d3255bfef'];
39+
yield [false, 'somethingElse://token@host?room_id=5539a3ee5etest0d3255bfef'];
40+
}
41+
42+
public function incompleteDsnProvider(): iterable
43+
{
44+
yield 'missing token' => ['gitter://api.gitter.im?room_id=5539a3ee5etest0d3255bfef'];
45+
}
46+
47+
public function missingRequiredOptionProvider(): iterable
48+
{
49+
yield 'missing option: room_id' => ['gitter://token@host'];
50+
}
51+
52+
public function unsupportedSchemeProvider(): iterable
53+
{
54+
yield ['somethingElse://token@host?room_id=5539a3ee5etest0d3255bfef'];
55+
yield ['somethingElse://token@host'];
56+
}
57+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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\Gitter\Tests;
13+
14+
use Symfony\Component\Notifier\Bridge\Gitter\GitterTransport;
15+
use Symfony\Component\Notifier\Message\ChatMessage;
16+
use Symfony\Component\Notifier\Message\MessageInterface;
17+
use Symfony\Component\Notifier\Message\SmsMessage;
18+
use Symfony\Component\Notifier\Tests\TransportTestCase;
19+
use Symfony\Component\Notifier\Transport\TransportInterface;
20+
use Symfony\Contracts\HttpClient\HttpClientInterface;
21+
22+
/**
23+
* @author Christin Gruber <c.gruber@touchdesign.de>
24+
*/
25+
final class GitterTransportTest extends TransportTestCase
26+
{
27+
public function createTransport(?HttpClientInterface $client = null): TransportInterface
28+
{
29+
return (new GitterTransport('token', '5539a3ee5etest0d3255bfef', $client ?: $this->createMock(HttpClientInterface::class)))->setHost('api.gitter.im');
30+
}
31+
32+
public function toStringProvider(): iterable
33+
{
34+
yield ['gitter://api.gitter.im?room_id=5539a3ee5etest0d3255bfef', $this->createTransport()];
35+
}
36+
37+
public function supportedMessagesProvider(): iterable
38+
{
39+
yield [new ChatMessage('Hello!')];
40+
}
41+
42+
public function unsupportedMessagesProvider(): iterable
43+
{
44+
yield [new SmsMessage('0611223344', 'Hello!')];
45+
yield [$this->createMock(MessageInterface::class)];
46+
}
47+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy