Skip to content

Commit 0fb67c8

Browse files
Add gitter notifier bridge
1 parent fe49ed2 commit 0fb67c8

File tree

14 files changed

+382
-0
lines changed

14 files changed

+382
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
use Symfony\Component\Notifier\Bridge\Firebase\FirebaseTransportFactory;
108108
use Symfony\Component\Notifier\Bridge\FreeMobile\FreeMobileTransportFactory;
109109
use Symfony\Component\Notifier\Bridge\GatewayApi\GatewayApiTransportFactory;
110+
use Symfony\Component\Notifier\Bridge\Gitter\GitterTransportFactory;
110111
use Symfony\Component\Notifier\Bridge\GoogleChat\GoogleChatTransportFactory;
111112
use Symfony\Component\Notifier\Bridge\Infobip\InfobipTransportFactory;
112113
use Symfony\Component\Notifier\Bridge\Iqsms\IqsmsTransportFactory;
@@ -2242,6 +2243,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
22422243
LinkedInTransportFactory::class => 'notifier.transport_factory.linkedin',
22432244
GatewayApiTransportFactory::class => 'notifier.transport_factory.gatewayapi',
22442245
OctopushTransportFactory::class => 'notifier.transport_factory.octopush',
2246+
GitterTransportFactory::class => 'notifier.transport_factory.gitter',
22452247
];
22462248

22472249
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;
@@ -135,6 +136,10 @@
135136
->parent('notifier.transport_factory.abstract')
136137
->tag('texter.transport_factory')
137138

139+
->set('notifier.transport_factory.gitter', GitterTransportFactory::class)
140+
->parent('notifier.transport_factory.abstract')
141+
->tag('texter.transport_factory')
142+
138143
->set('notifier.transport_factory.null', NullTransportFactory::class)
139144
->parent('notifier.transport_factory.abstract')
140145
->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.0
5+
-----
6+
7+
* Added the bridge
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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 $room;
33+
private $path;
34+
35+
public function __construct(string $token, string $room, ?string $path = null, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
36+
{
37+
$this->token = $token;
38+
$this->room = $room;
39+
$this->path = $path;
40+
41+
parent::__construct($client, $dispatcher);
42+
}
43+
44+
public function __toString(): string
45+
{
46+
return sprintf('gitter://%s?room=%s', $this->getEndpoint(), $this->room);
47+
}
48+
49+
public function supports(MessageInterface $message): bool
50+
{
51+
return $message instanceof ChatMessage;
52+
}
53+
54+
/**
55+
* @see https://developer.gitter.im/docs/rest-api
56+
*/
57+
protected function doSend(MessageInterface $message): SentMessage
58+
{
59+
if (!$message instanceof ChatMessage) {
60+
throw new UnsupportedMessageTypeException(__CLASS__, ChatMessage::class, $message);
61+
}
62+
63+
$options = ($opts = $message->getOptions()) ? $opts->toArray() : [];
64+
$options['text'] = $message->getSubject();
65+
66+
$endpoint = sprintf('https://%s/v1/rooms/%s/chatMessages', $this->getEndpoint(), $this->room);
67+
68+
$response = $this->client->request('POST', $endpoint, [
69+
'auth_bearer' => $this->token,
70+
'json' => array_filter($options),
71+
]);
72+
73+
$result = $response->toArray(false);
74+
75+
if (Response::HTTP_OK !== $response->getStatusCode()) {
76+
throw new TransportException(sprintf('Unable to post the Gitter message: "%s" (%s).', $result['message'], $result['id']), $response);
77+
}
78+
79+
$sentMessage = new SentMessage($message, (string) $this);
80+
$sentMessage->setMessageId($result['id']);
81+
82+
return $sentMessage;
83+
}
84+
85+
protected function getEndpoint(): string
86+
{
87+
return rtrim($this->host.($this->port ? ':'.$this->port : '').($this->path ?? ''), '/');
88+
}
89+
}
Lines changed: 45 additions & 0 deletions
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\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+
$path = $dsn->getPath();
33+
$token = $this->getUser($dsn);
34+
$room = $dsn->getRequiredOption('room');
35+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
36+
$port = $dsn->getPort();
37+
38+
return (new GitterTransport($token, $room, $path, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
39+
}
40+
41+
protected function getSupportedSchemes(): array
42+
{
43+
return ['gitter'];
44+
}
45+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2019-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=ROOM_ID
11+
```
12+
13+
where:
14+
- `TOKEN` is your Gitter token
15+
- `ROOM_ID` is you 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: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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=testRoom',
32+
'gitter://token@api.gitter.im?room=testRoom',
33+
];
34+
35+
yield [
36+
'gitter://api.gitter.im/sub?room=testRoom',
37+
'gitter://token@api.gitter.im/sub?room=testRoom',
38+
];
39+
40+
yield [
41+
'gitter://api.gitter.im?room=testRoom',
42+
'gitter://token@api.gitter.im/?room=testRoom',
43+
];
44+
45+
yield [
46+
'gitter://api.gitter.im/sub?room=testRoom',
47+
'gitter://token@api.gitter.im/sub?room=testRoom',
48+
];
49+
50+
yield [
51+
'gitter://api.gitter.im/sub/sub-2?room=testRoom',
52+
'gitter://token@api.gitter.im/sub/sub-2?room=testRoom',
53+
];
54+
}
55+
56+
public function supportsProvider(): iterable
57+
{
58+
yield [true, 'gitter://token@host?room=testRoom'];
59+
yield [false, 'invalidProvider://token@host?room=testRoom'];
60+
}
61+
62+
public function incompleteDsnProvider(): iterable
63+
{
64+
yield 'missing token' => ['gitter://api.gitter.im?room=testRoom'];
65+
}
66+
67+
public function missingRequiredOptionProvider(): iterable
68+
{
69+
yield 'missing option: channel' => ['gitter://token@host'];
70+
}
71+
72+
public function unsupportedSchemeProvider(): iterable
73+
{
74+
yield ['invalidProvider://token@host?room=testRoom'];
75+
yield ['invalidProvider://token@host'];
76+
}
77+
}
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', 'testRoom', null, $client ?: $this->createMock(HttpClientInterface::class)))->setHost('api.gitter.im');
30+
}
31+
32+
public function toStringProvider(): iterable
33+
{
34+
yield ['gitter://api.gitter.im?room=testRoom', $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