8000 [notifier] add RocketChat bridge · symfony/symfony@3ff8906 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3ff8906

Browse files
committed
[notifier] add RocketChat bridge
1 parent 7df0b6c commit 3ff8906

File tree

9 files changed

+308
-0
lines changed

9 files changed

+308
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/Tests export-ignore
2+
/phpunit.xml.dist 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.0.0
5+
-----
6+
7+
* Added the bridge
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2019 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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
RocketChat Notifier
2+
==============
3+
4+
Provides RocketChat 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)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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\RocketChat;
13+
14+
use Symfony\Component\Notifier\Message\MessageOptionsInterface;
15+
16+
/**
17+
* @author Jeroen Spee <spee.jeroen@gmail.com>
18+
*
19+
* @experimental in 5.0
20+
*
21+
* @see https://rocket.chat/docs/administrator-guides/integrations/
22+
*/
23+
final class RocketChatOptions implements MessageOptionsInterface
24+
{
25+
/** @var string|null prefix with '@' for personal messages */
26+
private $channel;
27+
28+
/** @var mixed[] */
29+
private $attachments;
30+
31+
/**
32+
* @param string[] $attachments
33+
*/
34+
public function __construct(array $attachments = [])
35+
{
36+
$this->attachments = $attachments;
37+
}
38+
39+
public function toArray(): array
40+
{
41+
return [
42+
'attachments' => [$this->attachments]
43+
];
44+
}
45+
46+
public function getRecipientId(): ?string
47+
{
48+
return $this->channel;
49+
}
50+
51+
/**
52+
* @return $this
53+
*/
54+
public function channel(string $channel): self
55+
{
56+
$this->channel = $channel;
57+
58+
return $this;
59+
}
60+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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\RocketChat;
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 Jeroen Spee <spee.jeroen@gmail.com>
24+
*
25+
* @internal
26+
*
27+
* @experimental in 5.0
28+
*/
29+
final class RocketChatTransport extends AbstractTransport
30+
{
31+
protected const HOST = 'rocketchat.com';
32+
33+
private $accessToken;
34+
private $chatChannel;
35+
36+
public function __construct(
37+
string $accessToken,
38+
string $chatChannel = null,
39+
HttpClientInterface $client = null,
40+
EventDispatcherInterface $dispatcher = null
41+
) {
42+
$this->accessToken = $accessToken;
43+
$this->chatChannel = $chatChannel;
44+
$this->client = $client;
45+
46+
parent::__construct($client, $dispatcher);
47+
}
48+
49+
public function __toString(): string
50+
{
51+
return sprintf('rocketchat://%s?channel=%s', $this->getEndpoint(), $this->chatChannel);
52+
}
53+
54+
public function supports(MessageInterface $message): bool
55+
{
56+
return $message instanceof ChatMessage && (null === $message->getOptions() || $message->getOptions() instanceof RocketChatOptions);
57+
}
58+
59+
/**
60+
* @see https://rocket.chat/docs/administrator-guides/integrations/
61+
*/
62+
protected function doSend(MessageInterface $message): void
63+
{
64+
if (! $message instanceof ChatMessage) {
65+
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" (instance of "%s" given).', __CLASS__, ChatMessage::class, \get_class($message)));
66+
}
67+
if ($message->getOptions() && ! $message->getOptions() instanceof RocketChatOptions) {
68+
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" for options.', __CLASS__, RocketChatOptions::class));
69+
}
70+
71+
$options = $message->getOptions()->toArray();
72+
73+
if (! isset($options['channel'])) {
74+
$options['channel'] = $message->getRecipientId() ?: $this->chatChannel;
75+
}
76+
$options['text'] = $message->getSubject();
77+
78+
$response = $this->client->request(
79+
'POST',
80+
sprintf('https://%s/hooks/%s', $this->getEndpoint(), $this->accessToken),
81+
[
82+
'json' => array_filter($options),
83+
]
84+
);
85+
86+
if (200 !== $response->getStatusCode()) {
87+
throw new TransportException(sprintf('Unable to post the RocketChat message: %s.', $response->getContent(false)), $response);
88+
}
89+
90+
$result = $response->toArray(false);
91+
if (! $result['success']) {
92+
throw new TransportException(sprintf('Unable to post the RocketChat message: %s.', $result['error']), $response);
93+
}
94+
}
95+
}
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\RocketChat;
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 Jeroen Spee <spee.jeroen@gmail.com>
21+
*
22+
* @experimental in 5.0
23+
*/
24+
final class RocketChatTransportFactory extends AbstractTransportFactory
25+
{
26+
public function create(Dsn $dsn): TransportInterface
27+
{
28+
$scheme = $dsn->getScheme();
29+
$accessToken = $this->getUser($dsn);
30+
$channel = $dsn->getOption('channel');
31+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
32+
$port = $dsn->getPort();
33+
34+
if ('rocketchat' === $scheme) {
35+
return (new RocketChatTransport($accessToken, $channel, $this->client, $this->dispatcher))
36+
->setHost($host)
37+
->setPort($port);
38+
}
39+
40+
throw new UnsupportedSchemeException($dsn, 'rocketchat', $this->getSupportedSchemes());
41+
}
42+
43+
protected function getSupportedSchemes(): array
44+
{
45+
return ['rocketchat'];
46+
}
47+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "symfony/rocketchat-notifier",
3+
"type": "symfony-bridge",
4+
"description": "Symfony RocketChat Notifier Bridge",
5+
"keywords": ["rocketchat", "notifier"],
6+
"homepage": "https://symfony.com",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "Jeroen Spee",
11+
"email": "spee.jeroen@gmail.com"
12+
},
13+
{
14+
"name": "Symfony Community",
15+
"homepage": "https://symfony.com/contributors"
16+
}
17+
],
18+
"require": {
19+
"php": "^7.2.9",
20+
"symfony/http-client": "^4.3|^5.0",
21+
"symfony/notifier": "^5.0"
22+
},
23+
"autoload": {
24+
"psr-4": { "Symfony\\Component\\Notifier\\Bridge\\RocketChat\\": "" },
25+
"exclude-from-classmap": [
26+
"/Tests/"
27+
]
28+
},
29+
"minimum-stability": "dev",
30+
"extra": {
31+
"branch-alias": {
32+
"dev-master": "5.0-dev"
33+
}
34+
}
35+< 10000 div class="diff-text-inner">}
Lines changed: 31 additions & 0 deletions
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 RocketChat 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>

0 commit comments

Comments
 (0)
0