10000 [Notifier] Add Novu Notifier Bridge · symfony/symfony@bf65025 · GitHub
[go: up one dir, main page]

Skip to content

Commit bf65025

Browse files
committed
[Notifier] Add Novu Notifier Bridge
1 parent 27154d0 commit bf65025

16 files changed

+538
-2
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -2741,6 +2741,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
27412741
NotifierBridge\MessageMedia\MessageMediaTransportFactory::class => 'notifier.transport_factory.message-media',
27422742
NotifierBridge\MicrosoftTeams\MicrosoftTeamsTransportFactory::class => 'notifier.transport_factory.microsoft-teams',
27432743
NotifierBridge\Mobyt\MobytTransportFactory::class => 'notifier.transport_factory.mobyt',
2744+
NotifierBridge\Novu\NovuTransportFactory::class => 'notifier.transport_factory.novu',
27442745
NotifierBridge\Octopush\OctopushTransportFactory::class => 'notifier.transport_factory.octopush',
27452746
NotifierBridge\OneSignal\OneSignalTransportFactory::class => 'notifier.transport_factory.one-signal',
27462747
NotifierBridge\OrangeSms\OrangeSmsTransportFactory::class => 'notifier.transport_factory.orange-sms',

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -279,13 +279,17 @@
279279
->set('notifier.transport_factory.simple-textin', Bridge\SimpleTextin\SimpleTextinTransportFactory::class)
280280
->parent('notifier.transport_factory.abstract')
281281
->tag('texter.transport_factory')
282-
282+
283283
->set('notifier.transport_factory.click-send', Bridge\ClickSend\ClickSendTransportFactory::class)
284284
->parent('notifier.transport_factory.abstract')
285285
->tag('texter.transport_factory')
286-
286+
287287
->set('notifier.transport_factory.smsmode', Bridge\Smsmode\SmsmodeTransportFactory::class)
288288
->parent('notifier.transport_factory.abstract')
289289
->tag('texter.transport_factory')
290+
291+
->set('notifier.transport_factory.novu', Bridge\Novu\NovuTransportFactory::class)
292+
->parent('notifier.transport_factory.abstract')
293+
->tag('texter.transport_factory')
290294
;
291295
};
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vendor/
2+
composer.lock
3+
phpunit.xml
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CHANGELOG
2+
=========
3+
4+
6.3
5+
---
6+
7+
* Add the bridge
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2023-present 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.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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\Novu;
13+
14+
use Symfony\Component\Notifier\Message\MessageOptionsInterface;
15+
16+
/**
17+
* @author Wouter van der Loop <woutervdl@toppy.nl>
18+
*/
19+
class NovuOptions implements MessageOptionsInterface
20+
{
21+
public function __construct(
22+
private readonly string|null $subscriberId = null,
23+
private readonly string|null $firstName = null,
24+
private readonly string|null $lastName = null,
25+
private readonly string|null $email = null,
26+
private readonly string|null $phone = null,
27+
private readonly string|null $avatar = null,
28+
private readonly string|null $locale = null,
29+
private readonly array $options = [],
30+
) {
31+
}
32+
33+
public function toArray(): array
34+
{
35+
return array_merge($this->options, [
36+
'firstName' => $this->firstName,
37+
'lastName' => $this->lastName,
38+
'email' => $this->email,
39+
'phone' => $this->phone,
40+
'avatar' => $this->avatar,
41+
'locale' => $this->locale,
42+
]);
43+
}
44+
45+
public function getRecipientId(): ?string
46+
{
47+
return $this->subscriberId ?? null;
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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\Novu;
13+
14+
use Symfony\Component\Notifier\Recipient\RecipientInterface;
15+
16+
/**
17+
* @author Wouter van der Loop <woutervdl@toppy.nl>
18+
*/
19+
class NovuSubscriberRecipient implements RecipientInterface
20+
{
21+
public function __construct(
22+
private readonly string $subscriberId,
23+
private readonly string|null $firstName = null,
24+
private readonly string|null $lastName = null,
25+
private readonly string|null $email = null,
26+
private readonly string|null $phone = null,
27+
private readonly string|null $avatar = null,
28+
private readonly string|null $locale = null,
29+
) {
30+
}
31+
32+
public function getSubscriberId(): string
33+
{
34+
return $this->subscriberId;
35+
}
36+
37+
public function getFirstName(): ?string
38+
{
39+
return $this->firstName;
40+
}
41+
42+
public function getLastName(): ?string
43+
{
44+
return $this->lastName;
45+
}
46+
47+
public function getEmail(): ?string
48+
{
49+
return $this->email;
50+
}
51+
52+
public function getPhone(): ?string
53+
{
54+
return $this->phone;
55+
}
56+
57+
public function getAvatar(): ?string
58+
{
59+
return $this->avatar;
60+
}
61+
62+
public function getLocale(): ?string
63+
{
64+
return $this->locale;
65+
}
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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\Novu;
13+
14+
use Symfony\Component\Notifier\Exception\TransportException;
15+
use Symfony\Component\Notifier\Exception\TransportExceptionInterface;
16+
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
17+
use Symfony\Component\Notifier\Message\MessageInterface;
18+
use Symfony\Component\Notifier\Message\PushMessage;
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 Wouter van der Loop <woutervdl@toppy.nl>
26+
*/
27+
class NovuTransport extends AbstractTransport
28+
{
29+
protected const HOST = 'web.novu.co';
30+
31+
private string $apiKey;
32+
33+
public function __construct(
34+
#[\SensitiveParameter] string $apiKey,
35+
HttpClientInterface $client = null,
36+
EventDispatcherInterface $dispatcher = null
37+
) {
38+
$this->apiKey = $apiKey;
39+
parent::__construct($client, $dispatcher);
40+
}
41+
42+
public function __toString(): string
43+
{
44+
return sprintf('novu://%s', $this->getEndpoint());
45+
}
46+
47+
public function supports(MessageInterface $message): bool
48+
{
49+
return $message instanceof PushMessage && (null === $message->getOptions() || $message->getOptions() instanceof NovuOptions);
50+
}
51+
52+
protected function doSend(MessageInterface $message): SentMessage
53+
{
54+
if (!$message instanceof PushMessage) {
55+
throw new UnsupportedMessageTypeException(__CLASS__, PushMessage::class, $message);
56+
}
57+
58+
$options = $message->getOptions()?->toArray() ?? [];
59+
60+
$body = [
61+
'name' => $message->getSubject(),
62+
'to' => [
63+
'subscriberId' => $message->getRecipientId(),
64+
'firstName' => $options['firstName'],
65+
'lastName' => $options['lastName'],
66+
'email' => $options['email'],
67+
'phone' => $options['phone'],
68+
'avatar' => $options['avatar'],
69+
'locale' => $options['locale'],
70+
],
71+
'payload' => json_decode($message->getContent()),
72+
];
73+
74+
$endpoint = sprintf('https://%s/v1/events/trigger', $this->getEndpoint());
75+
$response = $this->client->request('POST', $endpoint, [
76+
'body' => $body,
77+
'headers' => [
78+
'Authorization' => sprintf('ApiKey %s', $this->apiKey),
79+
'Content-Type' => 'application/json',
80+
],
81+
]);
82+
83+
try {
84+
$statusCode = $response->getStatusCode();
85+
} catch (TransportExceptionInterface $e) {
86+
throw new TransportException('Could not reach the remote Novu server.', $response, 0, $e);
87+
}
88+
89+
if (201 !== $statusCode) {
90+
$originalContent = $message->getSubject();
91+
$result = $response->toArray(false);
92+
$error = $result['message'];
93+
throw new TransportException(sprintf('Unable to post the Novu message: "%s" (%d: "%s").', $originalContent, $statusCode, $error), $response);
94+
}
95+
96+
return new SentMessage($message, (string) $this);
97+
}
98+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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\Novu;
13+
14+
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
15+
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
16+
use Symfony\Component\Notifier\Transport\Dsn;
17+
18+
/**
19+
* @author Wouter van der Loop <woutervdl@toppy.nl>
20+
*/
21+
class NovuTransportFactory extends AbstractTransportFactory
22+
{
23+
private const SCHEME = 'novu';
24+
25+
protected function getSupportedSchemes(): array
26+
{
27+
return [self::SCHEME];
28+
}
29+
30+
public function create(Dsn $dsn): NovuTransport
31+
{
32+
$scheme = $dsn->getScheme();
33+
if (self::SCHEME !== $scheme) {
34+
throw new UnsupportedSchemeException($dsn, self::SCHEME, $this->getSupportedSchemes());
35+
}
36+
37+
$key = $this->getUser($dsn);
38+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
39+
$port = $dsn->getPort();
40+
41+
return (new NovuTransport($key, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Novu Notifier
2+
=================
3+
4+
Provides [Novu](https://novu.co/) integration for Symfony Notifier.
5+
6+
DSN example
7+
-----------
8+
9+
```
10+
NOVU_DSN=novu://API_KEY@default
11+
```
12+
13+
Resources
14+
---------
15+
16+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
17+
* [Report issues](https://github.com/symfony/symfony/issues) and
18+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
19+
in the [main Symfony repository](https://github.com/symfony/symfony)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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\Novu\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Notifier\Bridge\Novu\NovuOptions;
16+
17+
class NovuOptionsTest extends TestCase
18+
{
19+
/**
20+
* @group legacy
21+
*/
22+
public function testToArray()
23+
{
24+
$options = new NovuOptions(
25+
123,
26+
'Joe',
27+
'Smith',
28+
'test@example.com',
29+
null,
30+
null,
31+
null,
32+
[],
33+
);
34+
35+
$expected = [
36+
'firstName' => 'Joe',
37+
'lastName' => 'Smith',
38+
'email' => 'test@example.com',
39+
'phone' => null,
40+
'avatar' => null,
41+
'locale' => null,
42+
];
43+
44+
$this->assertSame($expected, $options->toArray());
45+
}
46+
}

0 commit comments

Comments
 (0)
0