Skip to content

Commit b9f9970

Browse files
committed
Adds Zulip notifier bridge
1 parent 4170346 commit b9f9970

File tree

14 files changed

+319
-1
lines changed

14 files changed

+319
-1
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
use Symfony\Component\Notifier\Bridge\Slack\SlackTransportFactory;
102102
use Symfony\Component\Notifier\Bridge\Telegram\TelegramTransportFactory;
103103
use Symfony\Component\Notifier\Bridge\Twilio\TwilioTransportFactory;
104+
use Symfony\Component\Notifier\Bridge\Zulip\ZulipTransportFactory;
104105
use Symfony\Component\Notifier\Notifier;
105106
use Symfony\Component\Notifier\Recipient\AdminRecipient;
106107
use Symfony\Component\PropertyAccess\PropertyAccessor;
@@ -2047,6 +2048,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
20472048
FreeMobileTransportFactory::class => 'notifier.transport_factory.freemobile',
20482049
OvhCloudTransportFactory::class => 'notifier.transport_factory.ovhcloud',
20492050
SinchTransportFactory::class => 'notifier.transport_factory.sinch',
2051+
ZulipTransportFactory::class => 'notifier.transport_factory.zulip',
20502052
];
20512053

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

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@
5050
<tag name="texter.transport_factory" />
5151
</service>
5252

53+
<service id="notifier.transport_factory.zulip" class="Symfony\Component\Notifier\Bridge\Zulip\ZulipTransportFactory" parent="notifier.transport_factory.abstract">
54+
<tag name="chatter.transport_factory" />
55+
</service>
56+
5357
<service id="notifier.transport_factory.null" class="Symfony\Component\Notifier\Transport\NullTransportFactory" parent="notifier.transport_factory.abstract">
5458
<tag name="chatter.transport_factory" />
5559
<tag name="texter.transport_factory" />
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.1.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) 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.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Zulip Notifier
2+
===================
3+
4+
Provides Zulip 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: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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\Zulip;
13+
14+
use Symfony\Component\Notifier\Message\MessageOptionsInterface;
15+
16+
/**
17+
* @author Mohammad Emran Hasan <phpfour@gmail.com>
18+
*
19+
* @experimental in 5.1
20+
*/
21+
final class ZulipOptions implements MessageOptionsInterface
22+
{
23+
/** @var string|null */
24+
private $topic;
25+
26+
/** @var string|null */
27+
private $recipient;
28+
29+
public function __construct(?string $topic = null, ?string $recipient = null)
30+
{
31+
$this->topic = $topic;
32+
$this->recipient = $recipient;
33+
}
34+
35+
public function toArray(): array
36+
{
37+
return [
38+
'topic' => $this->topic,
39+
'recipient' => $this->recipient,
40+
];
41+
}
42+
43+
public function getRecipientId(): ?string
44+
{
45+
return $this->recipient;
46+
}
47+
48+
public function topic(string $topic): self
49+
{
50+
$this->topic = $topic;
51+
52+
return $this;
53+
}
54+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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\Zulip;
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 Mohammad Emran Hasan <phpfour@gmail.com>
24+
*
25+
* @experimental in 5.1
26+
*/
27+
class ZulipTransport extends AbstractTransport
28+
{
29+
private $email;
30+
private $token;
31+
private $channel;
32+
33+
public function __construct(string $email, string $token, string $channel, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
34+
{
35+
$this->email = $email;
36+
$this->token = $token;
37+
$this->channel = $channel;
38+
39+
parent::__construct($client, $dispatcher);
40+
}
41+
42+
public function __toString(): string
43+
{
44+
return sprintf('zulip://%s?channel=%s', $this->getEndpoint(), $this->channel);
45+
}
46+
47+
public function supports(MessageInterface $message): bool
48+
{
49+
return $message instanceof ChatMessage && (null === $message->getOptions() || $message->getOptions() instanceof ZulipOptions);
50+
}
51+
52+
/**
53+
* @see https://zulipchat.com/api/send-message
54+
*/
55+
protected function doSend(MessageInterface $message): void
56+
{
57+
if (!$message instanceof ChatMessage) {
58+
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" (instance of "%s" given).', __CLASS__, ChatMessage::class, get_debug_type($message)));
59+
}
60+
61+
if (null !== $message->getOptions() && !($message->getOptions() instanceof ZulipOptions)) {
62+
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" for options.', __CLASS__, ZulipOptions::class));
63+
}
64+
65+
$endpoint = sprintf('https://%s/api/v1/messages', $this->getEndpoint());
66+
67+
$options = ($opts = $message->getOptions()) ? $opts->toArray() : [];
68+
$options['content'] = $message->getSubject();
69+
70+
if (null === $message->getRecipientId() && empty($options['topic'])) {
71+
throw new LogicException(sprintf('The "%s" transport requires a topic when posting to streams.', __CLASS__));
72+
}
73+
74+
if (null === $message->getRecipientId()) {
75+
$options['type'] = 'stream';
76+
$options['to'] = $this->channel;
77+
} else {
78+
$options['type'] = 'private';
79+
$options['to'] = $message->getRecipientId();
80+
}
81+
82+
$response = $this->client->request('POST', $endpoint, [
83+
'auth_basic' => $this->email.':'.$this->token,
84+
'body' => $options,
85+
]);
86+
87+
if (200 !== $response->getStatusCode()) {
88+
$result = $response->toArray(false);
89+
90+
throw new TransportException(sprintf('Unable to post the Zulip message: %s (%s).', $result['msg'], $result['code']), $response);
91+
}
92+
}
93+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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\Zulip;
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 Mohammad Emran Hasan <phpfour@gmail.com>
21+
*
22+
* @experimental in 5.1
23+
*/
24+
class ZulipTransportFactory extends AbstractTransportFactory
25+
{
26+
/**
27+
* {@inheritdoc}
28+
*/
29+
public function create(Dsn $dsn): TransportInterface
30+
{
31+
$scheme = $dsn->getScheme();
32+
$email = $this->getUser($dsn);
33+
$token = $this->getPassword($dsn);
34+
$channel = $dsn->getOption('channel');
35+
$host = $dsn->getHost();
36+
$port = $dsn->getPort();
37+
38+
if ('zulip' === $scheme) {
39+
return (new ZulipTransport($email, $token, $channel, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
40+
}
41+
42+
throw new UnsupportedSchemeException($dsn, 'zulip', $this->getSupportedSchemes());
43+
}
44+
45+
/**
46+
* {@inheritdoc}
47+
*/
48+
protected function getSupportedSchemes(): array
49+
{
50+
return ['zulip'];
51+
}
52+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "symfony/zulip-notifier",
3+
"type": "symfony-bridge",
4+
"description": "Symfony Zulip Notifier Bridge",
5+
"keywords": ["zulip", "notifier"],
6+
"homepage": "https://symfony.com",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "Mohammad Emran Hasan",
11+
"email": "phpfour@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\\Zulip\\": "" },
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+
}

0 commit comments

Comments
 (0)