-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
27154d0
commit 914e2fc
Showing
16 changed files
with
538 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
vendor/ | ||
composer.lock | ||
phpunit.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
CHANGELOG | ||
========= | ||
|
||
6.3 | ||
--- | ||
|
||
* Add the bridge |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
49
src/Symfony/Component/Notifier/Bridge/Novu/NovuOptions.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
src/Symfony/Component/Notifier/Bridge/Novu/NovuSubscriberRecipient.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
98
src/Symfony/Component/Notifier/Bridge/Novu/NovuTransport.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/Symfony/Component/Notifier/Bridge/Novu/NovuTransportFactory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
46 changes: 46 additions & 0 deletions
46
src/Symfony/Component/Notifier/Bridge/Novu/Tests/NovuOptionsTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
Oops, something went wrong.