Skip to content

Commit

Permalink
[Notifier] Add Novu Notifier Bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
wouter-toppy committed May 24, 2023
1 parent 27154d0 commit 914e2fc
Show file tree
Hide file tree
Showing 16 changed files with 538 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2741,6 +2741,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
NotifierBridge\MessageMedia\MessageMediaTransportFactory::class => 'notifier.transport_factory.message-media',
NotifierBridge\MicrosoftTeams\MicrosoftTeamsTransportFactory::class => 'notifier.transport_factory.microsoft-teams',
NotifierBridge\Mobyt\MobytTransportFactory::class => 'notifier.transport_factory.mobyt',
NotifierBridge\Novu\NovuTransportFactory::class => 'notifier.transport_factory.novu',
NotifierBridge\Octopush\OctopushTransportFactory::class => 'notifier.transport_factory.octopush',
NotifierBridge\OneSignal\OneSignalTransportFactory::class => 'notifier.transport_factory.one-signal',
NotifierBridge\OrangeSms\OrangeSmsTransportFactory::class => 'notifier.transport_factory.orange-sms',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,13 +279,17 @@
->set('notifier.transport_factory.simple-textin', Bridge\SimpleTextin\SimpleTextinTransportFactory::class)
->parent('notifier.transport_factory.abstract')
->tag('texter.transport_factory')

->set('notifier.transport_factory.click-send', Bridge\ClickSend\ClickSendTransportFactory::class)
->parent('notifier.transport_factory.abstract')
->tag('texter.transport_factory')

->set('notifier.transport_factory.smsmode', Bridge\Smsmode\SmsmodeTransportFactory::class)
->parent('notifier.transport_factory.abstract')
->tag('texter.transport_factory')

->set('notifier.transport_factory.novu', Bridge\Novu\NovuTransportFactory::class)
->parent('notifier.transport_factory.abstract')
->tag('texter.transport_factory')
;
};
4 changes: 4 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Novu/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/Tests export-ignore
/phpunit.xml.dist export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
3 changes: 3 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Novu/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vendor/
composer.lock
phpunit.xml
7 changes: 7 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Novu/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CHANGELOG
=========

6.3
---

* Add the bridge
19 changes: 19 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Novu/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2023-present Fabien Potencier

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
49 changes: 49 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Novu/NovuOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\Novu;

use Symfony\Component\Notifier\Message\MessageOptionsInterface;

/**
* @author Wouter van der Loop <woutervdl@toppy.nl>
*/
class NovuOptions implements MessageOptionsInterface
{
public function __construct(
private readonly string|null $subscriberId = null,
private readonly string|null $firstName = null,
private readonly string|null $lastName = null,
private readonly string|null $email = null,
private readonly string|null $phone = null,
private readonly string|null $avatar = null,
private readonly string|null $locale = null,
private readonly array $options = [],
) {
}

public function toArray(): array
{
return array_merge($this->options, [
'firstName' => $this->firstName,
'lastName' => $this->lastName,
'email' => $this->email,
'phone' => $this->phone,
'avatar' => $this->avatar,
'locale' => $this->locale,
]);
}

public function getRecipientId(): ?string
{
return $this->subscriberId ?? null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\Novu;

use Symfony\Component\Notifier\Recipient\RecipientInterface;

/**
* @author Wouter van der Loop <woutervdl@toppy.nl>
*/
class NovuSubscriberRecipient implements RecipientInterface
{
public function __construct(
private readonly string $subscriberId,
private readonly string|null $firstName = null,
private readonly string|null $lastName = null,
private readonly string|null $email = null,
private readonly string|null $phone = null,
private readonly string|null $avatar = null,
private readonly string|null $locale = null,
) {
}

public function getSubscriberId(): string
{
return $this->subscriberId;
}

public function getFirstName(): ?string
{
return $this->firstName;
}

public function getLastName(): ?string
{
return $this->lastName;
}

public function getEmail(): ?string
{
return $this->email;
}

public function getPhone(): ?string
{
return $this->phone;
}

public function getAvatar(): ?string
{
return $this->avatar;
}

public function getLocale(): ?string
{
return $this->locale;
}
}
98 changes: 98 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Novu/NovuTransport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\Novu;

use Symfony\Component\Notifier\Exception\TransportException;
use Symfony\Component\Notifier\Exception\TransportExceptionInterface;
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
use Symfony\Component\Notifier\Message\MessageInterface;
use Symfony\Component\Notifier\Message\PushMessage;
use Symfony\Component\Notifier\Message\SentMessage;
use Symfony\Component\Notifier\Transport\AbstractTransport;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
* @author Wouter van der Loop <woutervdl@toppy.nl>
*/
class NovuTransport extends AbstractTransport
{
protected const HOST = 'web.novu.co';

private string $apiKey;

public function __construct(
#[\SensitiveParameter] string $apiKey,
HttpClientInterface $client = null,
EventDispatcherInterface $dispatcher = null
) {
$this->apiKey = $apiKey;
parent::__construct($client, $dispatcher);
}

public function __toString(): string
{
return sprintf('novu://%s', $this->getEndpoint());
}

public function supports(MessageInterface $message): bool
{
return $message instanceof PushMessage && (null === $message->getOptions() || $message->getOptions() instanceof NovuOptions);
}

protected function doSend(MessageInterface $message): SentMessage
{
if (!$message instanceof PushMessage) {
throw new UnsupportedMessageTypeException(__CLASS__, PushMessage::class, $message);
}

$options = $message->getOptions()?->toArray() ?? [];

$body = [
'name' => $message->getSubject(),
'to' => [
'subscriberId' => $message->getRecipientId(),
'firstName' => $options['firstName'],
'lastName' => $options['lastName'],
'email' => $options['email'],
'phone' => $options['phone'],
'avatar' => $options['avatar'],
'locale' => $options['locale'],
],
'payload' => json_decode($message->getContent()),
];

$endpoint = sprintf('https://%s/v1/events/trigger', $this->getEndpoint());
$response = $this->client->request('POST', $endpoint, [
'body' => $body,
'headers' => [
'Authorization' => sprintf('ApiKey %s', $this->apiKey),
'Content-Type' => 'application/json',
],
]);

try {
$statusCode = $response->getStatusCode();
} catch (TransportExceptionInterface $e) {
throw new TransportException('Could not reach the remote Novu server.', $response, 0, $e);
}

if (201 !== $statusCode) {
$originalContent = $message->getSubject();
$result = $response->toArray(false);
$error = $result['message'];
throw new TransportException(sprintf('Unable to post the Novu message: "%s" (%d: "%s").', $originalContent, $statusCode, $error), $response);
}

return new SentMessage($message, (string) $this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\Novu;

use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
use Symfony\Component\Notifier\Transport\Dsn;

/**
* @author Wouter van der Loop <woutervdl@toppy.nl>
*/
class NovuTransportFactory extends AbstractTransportFactory
{
private const SCHEME = 'novu';

protected function getSupportedSchemes(): array
{
return [self::SCHEME];
}

public function create(Dsn $dsn): NovuTransport
{
$scheme = $dsn->getScheme();
if (self::SCHEME !== $scheme) {
throw new UnsupportedSchemeException($dsn, self::SCHEME, $this->getSupportedSchemes());
}

$key = $this->getUser($dsn);
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
$port = $dsn->getPort();

return (new NovuTransport($key, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
}
}
19 changes: 19 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Novu/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Novu Notifier
=================

Provides [Novu](https://novu.co/) integration for Symfony Notifier.

DSN example
-----------

```
NOVU_DSN=novu://API_KEY@default
```

Resources
---------

* [Contributing](https://symfony.com/doc/current/contributing/index.html)
* [Report issues](https://github.com/symfony/symfony/issues) and
[send Pull Requests](https://github.com/symfony/symfony/pulls)
in the [main Symfony repository](https://github.com/symfony/symfony)
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\Novu\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Notifier\Bridge\Novu\NovuOptions;

class NovuOptionsTest extends TestCase
{
/**
* @group legacy
*/
public function testToArray()
{
$options = new NovuOptions(
123,
'Joe',
'Smith',
'test@example.com',
null,
null,
null,
[],
);

$expected = [
'firstName' => 'Joe',
'lastName' => 'Smith',
'email' => 'test@example.com',
'phone' => null,
'avatar' => null,
'locale' => null,
];

$this->assertSame($expected, $options->toArray());
}
}

0 comments on commit 914e2fc

Please sign in to comment.