-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Notifier] Add Pusher Notifier Bridge #48203
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
0ec6455
feat: add Pusher symfony notifier bridge
bitgandtter a8a7775
fix: composer.json package name
bitgandtter 0d18b48
fix: LICENSE and CHANGELOG.md
bitgandtter 1fbbcfe
chore: apply fabbot
bitgandtter c582df8
chore: apply fabbot
bitgandtter 8c00bad
chore: apply fabbot
bitgandtter 073986f
chore: work on PR comments
bitgandtter 8b701a7
chore: drop declare strict types
bitgandtter c67f28a
chore: small format changes
bitgandtter dcd029f
fix: add pusher package to main composer.json to fix tests
bitgandtter 88bad27
chore: set PusherRecipientInterface as typehint of asPushMessage method
bitgandtter dee00b6
chore: set RecipientInterface as typehint of asPushMessage method
bitgandtter 45ad1c6
Remove calls to AnnotationRegistry::registerLoader()
derrabus 6cd8b6e
[PhpUnitBridge] Revert "minor #48725 Remove calls to `AnnotationRegis…
nicolas-grekas 0d251c5
Remove calls to AnnotationRegistry::registerLoader()
derrabus f7165d7
[PhpUnitBridge] Revert "minor #48725 Remove calls to `AnnotationRegis…
nicolas-grekas a17252a
Remove calls to AnnotationRegistry::registerLoader()
derrabus dc3e77d
chore: fabbot
bitgandtter 4771f05
fix: LICENSE
bitgandtter 60ec41e
chore: rebase for #49385
bitgandtter a4c4401
chore: apply comments from #49385
bitgandtter 7429e4d
feat: drop autoconfigure tag from transport factory
bitgandtter 2787214
chore: drop extra - on CHANGELOG.md
bitgandtter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 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. |
28 changes: 28 additions & 0 deletions
28
src/Symfony/Component/Notifier/Bridge/Pusher/PusherNotification.php
This file contains hidden or 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,28 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Notifier\Bridge\Pusher; | ||
|
||
use Symfony\Component\Notifier\Message\PushMessage; | ||
use Symfony\Component\Notifier\Notification\Notification; | ||
use Symfony\Component\Notifier\Notification\PushNotificationInterface; | ||
use Symfony\Component\Notifier\Recipient\RecipientInterface; | ||
|
||
/** | ||
* @author Yasmany Cubela Medina <[email protected]> | ||
*/ | ||
class PusherNotification extends Notification implements PushNotificationInterface | ||
{ | ||
public function asPushMessage(RecipientInterface $recipient, string $transport = null): ?PushMessage | ||
{ | ||
return new PushMessage($this->getSubject(), $this->getContent(), new PusherOptions($recipient instanceof PusherRecipientInterface ? $recipient->getChannels() : [])); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/Symfony/Component/Notifier/Bridge/Pusher/PusherOptions.php
This file contains hidden or 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,42 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Notifier\Bridge\Pusher; | ||
|
||
use Symfony\Component\Notifier\Message\MessageOptionsInterface; | ||
|
||
/** | ||
* @author Yasmany Cubela Medina <[email protected]> | ||
*/ | ||
final class PusherOptions implements MessageOptionsInterface | ||
{ | ||
private array $channels; | ||
|
||
public function __construct(array $channels) | ||
{ | ||
$this->channels = $channels; | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
return $this->channels; | ||
} | ||
|
||
public function getRecipientId(): ?string | ||
{ | ||
return null; | ||
} | ||
|
||
public function getChannels(): array | ||
{ | ||
return $this->channels; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/Symfony/Component/Notifier/Bridge/Pusher/PusherRecipient.php
This file contains hidden or 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,30 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Notifier\Bridge\Pusher; | ||
|
||
/** | ||
* @author Yasmany Cubela Medina <[email protected]> | ||
*/ | ||
class PusherRecipient implements PusherRecipientInterface | ||
{ | ||
private array $channels; | ||
|
||
public function __construct(array $channels) | ||
{ | ||
$this->channels = $channels; | ||
} | ||
|
||
public function getChannels(): array | ||
{ | ||
return $this->channels; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/Symfony/Component/Notifier/Bridge/Pusher/PusherRecipientInterface.php
This file contains hidden or 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,22 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Notifier\Bridge\Pusher; | ||
|
||
use Symfony\Component\Notifier\Recipient\RecipientInterface; | ||
|
||
/** | ||
* @author Yasmany Cubela Medina <[email protected]> | ||
*/ | ||
interface PusherRecipientInterface extends RecipientInterface | ||
{ | ||
public function getChannels(): array; | ||
} |
72 changes: 72 additions & 0 deletions
72
src/Symfony/Component/Notifier/Bridge/Pusher/PusherTransport.php
This file contains hidden or 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,72 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Notifier\Bridge\Pusher; | ||
|
||
use Pusher\Pusher; | ||
use Symfony\Component\Notifier\Exception\LogicException; | ||
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; | ||
use Throwable; | ||
|
||
/** | ||
* @author Yasmany Cubela Medina <[email protected]> | ||
*/ | ||
final class PusherTransport extends AbstractTransport | ||
{ | ||
private $pusherClient; | ||
|
||
public function __construct(Pusher $pusherClient, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null) | ||
{ | ||
$this->pusherClient = $pusherClient; | ||
|
||
parent::__construct($client, $dispatcher); | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
$settings = $this->pusherClient->getSettings(); | ||
preg_match('/api-([\w]+)\.pusher\.com$/m', $settings['host'], $server); | ||
|
||
return sprintf('pusher://%s?server=%s', $settings['app_id'], $server[1]); | ||
} | ||
|
||
public function supports(MessageInterface $message): bool | ||
{ | ||
return $message instanceof PushMessage && (null === $message->getOptions() || $message->getOptions() instanceof PusherOptions); | ||
} | ||
|
||
protected function doSend(MessageInterface $message): SentMessage | ||
{ | ||
if (!$message instanceof PushMessage) { | ||
throw new UnsupportedMessageTypeException(__CLASS__, PushMessage::class, $message); | ||
} | ||
|
||
$options = $message->getOptions(); | ||
|
||
if (!$options instanceof PusherOptions) { | ||
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" for options.', __CLASS__, PusherOptions::class)); | ||
} | ||
|
||
try { | ||
$this->pusherClient->trigger($options->getChannels(), $message->getSubject(), $message->getContent(), [], true); | ||
} catch (Throwable) { | ||
throw new \RuntimeException('An error occurred at Pusher Notifier Transport.'); | ||
} | ||
|
||
return new SentMessage($message, $this->__toString()); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/Symfony/Component/Notifier/Bridge/Pusher/PusherTransportFactory.php
This file contains hidden or 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,50 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Notifier\Bridge\Pusher; | ||
|
||
use Pusher\Pusher; | ||
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure; | ||
use Symfony\Component\Notifier\Exception\MissingRequiredOptionException; | ||
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException; | ||
use Symfony\Component\Notifier\Transport\AbstractTransportFactory; | ||
use Symfony\Component\Notifier\Transport\Dsn; | ||
use Symfony\Component\Notifier\Transport\TransportInterface; | ||
|
||
/** | ||
* @author Yasmany Cubela Medina <[email protected]> | ||
*/ | ||
final class PusherTransportFactory extends AbstractTransportFactory | ||
{ | ||
public function create(Dsn $dsn): TransportInterface | ||
{ | ||
if ('pusher' !== $dsn->getScheme()) { | ||
throw new UnsupportedSchemeException($dsn, 'pusher', $this->getSupportedSchemes()); | ||
} | ||
|
||
if (null === $dsn->getUser() || null === $dsn->getPassword() || null === $dsn->getOption('server')) { | ||
throw new MissingRequiredOptionException('Pusher needs a APP_KEY, APP_SECRET AND SERVER specified.'); | ||
} | ||
|
||
$options = [ | ||
'cluster' => $dsn->getOption('server', 'mt1'), | ||
]; | ||
|
||
$pusherClient = new Pusher($dsn->getUser(), $dsn->getPassword(), $dsn->getHost(), $options); | ||
|
||
return new PusherTransport($pusherClient, $this->client, $this->dispatcher); | ||
} | ||
|
||
protected function getSupportedSchemes(): array | ||
{ | ||
return ['pusher']; | ||
} | ||
} |
This file contains hidden or 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,40 @@ | ||
Pusher Notifier | ||
============== | ||
|
||
Provides [Pusher](https://pusher.com) integration for Symfony Notifier. | ||
|
||
DSN example | ||
----------- | ||
|
||
``` | ||
PUSHER_DSN=pusher://APP_KEY:APP_SECRET@APP_ID?server=SERVER | ||
``` | ||
|
||
where: | ||
|
||
- `APP_KEY` is your app unique key | ||
- `APP_SECRET` is your app unique and secret password | ||
- `APP_ID` is your app unique id | ||
- `SERVER` is your app server | ||
|
||
valid DSN's are: | ||
|
||
``` | ||
PUSHER_DSN=pusher://as8d09a0ds8:as8d09a8sd0a8sd0@123123123?server=mt1 | ||
``` | ||
|
||
invalid DSN's are: | ||
|
||
``` | ||
PUSHER_DSN=pusher://asdasdasd@asdasdasd?server=invalid-server | ||
PUSHER_DSN=pusher://:asdasdasd@asdasdasd?server=invalid-server | ||
PUSHER_DSN=pusher://asdadasdasd:asdasdasd@asdasdasd?server=invalid-server | ||
``` | ||
bitgandtter marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.