8000 feature #36616 [Notifier] Add Zulip notifier bridge (phpfour) · symfony/symfony@558dfa8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 558dfa8

Browse files
committed
feature #36616 [Notifier] Add Zulip notifier bridge (phpfour)
This PR was merged into the 5.2-dev branch. Discussion ---------- [Notifier] Add Zulip notifier bridge | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | See #33687 | License | MIT | Doc PR | symfony/symfony-docs#13604 This adds a [Zulip](https://zulipchat.com/) (another popular open source Slack alternative) transport for the new Notifier component. #### Sample DSN ```ZULIP_DSN=zulip://test-bot@zulipchat.com:api-key@example.zulipchat.com?channel=builds``` #### Configuration ```yml # config/packages/notifier.yaml framework: notifier: chatter_transports: zulip: '%env(ZULIP_DSN)%' ``` #### Example ```php public function index(ChatterInterface $chatter) { $message = (new ChatMessage('New order arrived!', new ZulipOptions('Ordering'))) ->transport('zulip'); $chatter->send($message); ... } ``` Commits ------- bd4fd32 Adds Zulip notifier bridge
2 parents b968497 + bd4fd32 commit 558dfa8

File tree

14 files changed

+328
-1
lines changed

14 files changed

+328
-1
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
use Symfony\Component\Notifier\Bridge\Slack\SlackTransportFactory;
105105
use Symfony\Component\Notifier\Bridge\Telegram\TelegramTransportFactory;
106106
use Symfony\Component\Notifier\Bridge\Twilio\TwilioTransportFactory;
107+
use Symfony\Component\Notifier\Bridge\Zulip\ZulipTransportFactory;
107108
use Symfony\Component\Notifier\Notifier;
108109
use Symfony\Component\Notifier\Recipient\AdminRecip 6D40 ient;
109110
use Symfony\Component\PropertyAccess\PropertyAccessor;
@@ -2082,6 +2083,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
20822083
FreeMobileTransportFactory::class => 'notifier.transport_factory.freemobile',
20832084
OvhCloudTransportFactory::class => 'notifier.transport_factory.ovhcloud',
20842085
SinchTransportFactory::class => 'notifier.transport_factory.sinch',
2086+
ZulipTransportFactory::class => 'notifier.transport_factory.zulip',
20852087
];
20862088

20872089
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
@@ -21,6 +21,7 @@
2121
use Symfony\Component\Notifier\Bridge\Slack\SlackTransportFactory;
2222
use Symfony\Component\Notifier\Bridge\Telegram\TelegramTransportFactory;
2323
use Symfony\Component\Notifier\Bridge\Twilio\TwilioTransportFactory;
24+
use Symfony\Component\Notifier\Bridge\Zulip\ZulipTransportFactory;
2425
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
2526
use Symfony\Component\Notifier\Transport\NullTransportFactory;
2627

@@ -70,6 +71,10 @@
7071
->parent('notifier.transport_factory.abstract')
7172
->tag('texter.transport_factory')
7273

74+
->set('notifier.transport_factory.zulip', ZulipTransportFactory::class)
75+
->parent('notifier.transport_factory.abstract')
76+
->tag('texter.transport_factory')
77+
7378
->set('notifier.transport_factory.null', NullTransportFactory::class)
7479
->parent('notifier.transport_factory.abstract')
7580
->tag('chatter.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.2.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.2
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: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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\Message\SentMessage;
19+
use Symfony\Component\Notifier\Transport\AbstractTransport;
20+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
21+
use Symfony\Contracts\HttpClient\HttpClientInterface;
22+
23+
/**
24+
* @author Mohammad Emran Hasan <phpfour@gmail.com>
25+
*
26+
* @experimental in 5.2
27+
*/
28+
class ZulipTransport extends AbstractTransport
29+
{
30+
private $email;
31+
private $token;
32+
private $channel;
33+
34+
public function __construct(string $email, string $token, string $channel, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
35+
{
36+
$this->email = $email;
37+
$this->token = $token;
38+
$this->channel = $channel;
39+
40+
parent::__construct($client, $dispatcher);
41+
}
42+
43+
public function __toString(): string
44+
{
45+
return sprintf('zulip://%s?channel=%s', $this->getEndpoint(), $this->channel);
46+
}
47+
48+
public function supports(MessageInterface $message): bool
49+
{
50+
return $message instanceof ChatMessage && (null === $message->getOptions() || $message->getOptions() instanceof ZulipOptions);
51+
}
52+
53+
/**
54+
* @see https://zulipchat.com/api/send-message
55+
*/
56+
protected function doSend(MessageInterface $message): SentMessage
57+
{
58+
if (!$message instanceof ChatMessage) {
59+
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" (instance of "%s" given).', __CLASS__, ChatMessage::class, get_debug_type($message)));
60+
}
61+
62+
if (null !== $message->getOptions() && !($message->getOptions() instanceof ZulipOptions)) {
63+
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" for options.', __CLASS__, ZulipOptions::class));
64+
}
65+
66+
$endpoint = sprintf('https://%s/api/v1/messages', $this->getEndpoint());
67+
68+
$options = ($opts = $message->getOptions()) ? $opts->toArray() : [];
69+
$options['content'] = $message->getSubject();
70+
71+
if (null === $message->getRecipientId() && empty($options['topic'])) {
72+
throw new LogicException(sprintf('The "%s" transport requires a topic when posting to streams.', __CLASS__));
73+
}
74+
75+
if (null === $message->getRecipientId()) {
76+
$options['type'] = 'stream';
77+
$options['to'] = $this->channel;
78+
} else {
79+
$options['type'] = 'private';
80+
$options['to'] = $message->getRecipientId();
81+
}
82+
83+
$response = $this->client->request('POST', $endpoint, [
84+
'auth_basic' => $this->email.':'.$this->token,
85+
'body' => $options,
86+
]);
87+
88+
if (200 !== $response->getStatusCode()) {
89+
$result = $response->toArray(false);
90+
91+
throw new TransportException(sprintf('Unable to post the Zulip message: "%s" (%s).', $result['msg'], $result['code']), $response);
92+
}
93+
94+
$success = $response->toArray(false);
95+
96+
$message = new SentMessage($message, (string) $this);
97+
$message->setMessageId($success['id']);
98+
99+
return $message;
100+
}
101+
}
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.2
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.2-dev"
33+
}
34+
}
35+
}
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 Zulip 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>

src/Symfony/Component/Notifier/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ CHANGELOG
55
-----
66

77
* [BC BREAK] The `TransportInterface::send()` and `AbstractTransport::doSend()` methods changed to return a `?SentMessage` instance instead of `void`.
8+
* Added the Zulip notifier bridge
89

910
5.1.0
1011
-----
1112

12-
* Added the Mattermost notifier bridge
13+
* Added the Mattermost notifier bridge
1314
* [BC BREAK] The `ChatMessage::fromNotification()` method's `$recipient` and `$transport`
1415
arguments were removed.
1516
* [BC BREAK] The `EmailMessage::fromNotification()` and `SmsMessage::fromNotification()`

src/Symfony/Component/Notifier/Exception/UnsupportedSchemeException.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ class UnsupportedSchemeException extends LogicException
6262
'class' => Bridge\Sinch\SinchTransportFactory::class,
6363
'package' => 'symfony/sinch-notifier',
6464
],
65+
'zulip' => [
66+
'class' => Bridge\Zulip\ZulipTransportFactory::class,
67+
'package' => 'symfony/zulip-notifier',
68+
],
6569
];
6670

6771
/**

src/Symfony/Component/Notifier/Transport.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Symfony\Component\Notifier\Bridge\Slack\SlackTransportFactory;
2222
use Symfony\Component\Notifier\Bridge\Telegram\TelegramTransportFactory;
2323
use Symfony\Component\Notifier\Bridge\Twilio\TwilioTransportFactory;
24+
use Symfony\Component\Notifier\Bridge\Zulip\ZulipTransportFactory;
2425
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
2526
use Symfony\Component\Notifier\Transport\Dsn;
2627
use Symfony\Component\Notifier\Transport\FailoverTransport;
@@ -50,6 +51,7 @@ class Transport
5051
FirebaseTransportFactory::class,
5152
SinchTransportFactory::class,
5253
FreeMobileTransportFactory::class,
54+
ZulipTransportFactory::class,
5355
];
5456

5557
private $factories;

0 commit comments

Comments
 (0)
0