Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[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
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
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 Nov 12, 2022
a8a7775
fix: composer.json package name
bitgandtter Nov 12, 2022
0d18b48
fix: LICENSE and CHANGELOG.md
bitgandtter Nov 12, 2022
1fbbcfe
chore: apply fabbot
bitgandtter Nov 12, 2022
c582df8
chore: apply fabbot
bitgandtter Nov 12, 2022
8c00bad
chore: apply fabbot
bitgandtter Nov 12, 2022
073986f
chore: work on PR comments
bitgandtter Nov 14, 2022
8b701a7
chore: drop declare strict types
bitgandtter Nov 14, 2022
c67f28a
chore: small format changes
bitgandtter Nov 14, 2022
dcd029f
fix: add pusher package to main composer.json to fix tests
bitgandtter Nov 14, 2022
88bad27
chore: set PusherRecipientInterface as typehint of asPushMessage method
bitgandtter Dec 2, 2022
dee00b6
chore: set RecipientInterface as typehint of asPushMessage method
bitgandtter Dec 2, 2022
45ad1c6
Remove calls to AnnotationRegistry::registerLoader()
derrabus Dec 20, 2022
6cd8b6e
[PhpUnitBridge] Revert "minor #48725 Remove calls to `AnnotationRegis…
nicolas-grekas Dec 28, 2022
0d251c5
Remove calls to AnnotationRegistry::registerLoader()
derrabus Dec 20, 2022
f7165d7
[PhpUnitBridge] Revert "minor #48725 Remove calls to `AnnotationRegis…
nicolas-grekas Dec 28, 2022
a17252a
Remove calls to AnnotationRegistry::registerLoader()
derrabus Dec 20, 2022
dc3e77d
chore: fabbot
bitgandtter Jan 9, 2023
4771f05
fix: LICENSE
bitgandtter Jan 9, 2023
60ec41e
chore: rebase for #49385
bitgandtter Feb 15, 2023
a4c4401
chore: apply comments from #49385
bitgandtter Feb 15, 2023
7429e4d
feat: drop autoconfigure tag from transport factory
bitgandtter Feb 16, 2023
2787214
chore: drop extra - on CHANGELOG.md
bitgandtter Feb 16, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@
"predis/predis": "~1.1",
"psr/http-client": "^1.0",
"psr/simple-cache": "^1.0|^2.0|^3.0",
"pusher/pusher-php-server": "^7.0",
"symfony/mercure-bundle": "^0.3",
"symfony/phpunit-bridge": "^5.4|^6.0",
"symfony/runtime": "self.version",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
use Symfony\Component\Notifier\Bridge\OvhCloud\OvhCloudTransportFactory;
use Symfony\Component\Notifier\Bridge\PagerDuty\PagerDutyTransportFactory;
use Symfony\Component\Notifier\Bridge\Plivo\PlivoTransportFactory;
use Symfony\Component\Notifier\Bridge\Pusher\PusherTransportFactory;
use Symfony\Component\Notifier\Bridge\RingCentral\RingCentralTransportFactory;
use Symfony\Component\Notifier\Bridge\RocketChat\RocketChatTransportFactory;
use Symfony\Component\Notifier\Bridge\Sendberry\SendberryTransportFactory;
Expand Down Expand Up @@ -331,5 +332,9 @@
->set('notifier.transport_factory.pager-duty', PagerDutyTransportFactory::class)
->parent('notifier.transport_factory.abstract')
->tag('chatter.transport_factory')

->set('notifier.transport_factory.slack', PusherTransportFactory::class)
->parent('notifier.transport_factory.abstract')
->tag('chatter.transport_factory')
;
};
4 changes: 4 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Pusher/.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/Pusher/.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/Pusher/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/Pusher/LICENSE
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.
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 src/Symfony/Component/Notifier/Bridge/Pusher/PusherOptions.php
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;
}
}
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;
}
}
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;
}
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());
}
}
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'];
}
}
40 changes: 40 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Pusher/README.md
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
```

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)
Loading