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

Skip to content

[Notifier] Add Pusher.com bridge #53267

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 26 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
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 @@ -147,6 +147,7 @@
"predis/predis": "^1.1|^2.0",
"psr/http-client": "^1.0",
"psr/simple-cache": "^1.0|^2.0|^3.0",
"pusher/pusher-php-server": "^7.0",
"seld/jsonlint": "^1.10",
"symfony/mercure-bundle": "^0.3",
"symfony/phpunit-bridge": "^6.4|^7.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
'mercure' => Bridge\Mercure\MercureTransportFactory::class,
'microsoft-teams' => Bridge\MicrosoftTeams\MicrosoftTeamsTransportFactory::class,
'pager-duty' => Bridge\PagerDuty\PagerDutyTransportFactory::class,
'pusher' => Bridge\Pusher\PusherTransportFactory::class,
'rocket-chat' => Bridge\RocketChat\RocketChatTransportFactory::class,
'slack' => Bridge\Slack\SlackTransportFactory::class,
'telegram' => Bridge\Telegram\TelegramTransportFactory::class,
Expand Down
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
=========

7.1
---

* 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() : []));
}
}
40 changes: 40 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Pusher/PusherOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?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
{
public function __construct(
private readonly array $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,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;

/**
* @author Yasmany Cubela Medina <[email protected]>
*/
class PusherRecipient implements PusherRecipientInterface
{
public function __construct(
private readonly array $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,70 @@
<?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;

/**
* @author Yasmany Cubela Medina <[email protected]>
*/
final class PusherTransport extends AbstractTransport
{
public function __construct(
private readonly Pusher $pusher,
HttpClientInterface $client = null,
EventDispatcherInterface $dispatcher = null,
) {
parent::__construct($client, $dispatcher);
}

public function __toString(): string
{
$settings = $this->pusher->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->pusher->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,47 @@
<?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\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()) {
throw new MissingRequiredOptionException('Pusher needs APP_KEY and APP_SECRET specified.');
}

return new PusherTransport(
new Pusher($dsn->getUser(), $dsn->getPassword(), $dsn->getHost(), ['cluster' => $dsn->getRequiredOption('server'),]),
$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.com Notifier
===================

Provides [Pusher.com](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)
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?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\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Notifier\Bridge\Pusher\PusherOptions;

/**
* @author Yasmany Cubela Medina <[email protected]>
*/
final class PusherOptionsTest extends TestCase
{
/**
* @dataProvider toArrayProvider
* @dataProvider toArraySimpleOptionsProvider
*/
public function testToArray(array $options, array $expected = null)
{
static::assertSame($expected ?? $options, (new PusherOptions($options))->toArray());
}

public function toArrayProvider(): iterable
{
yield 'empty is allowed' => [
[],
[],
];
}

public function toArraySimpleOptionsProvider(): iterable
{
yield [[]];
}

public function setProvider(): iterable
{
yield ['async', 'async', true];
}
}
Loading