Skip to content
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

[Notifier] Support for desktop notifications via jolicode/JoliNotif #57683

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -135,6 +135,7 @@
"dragonmantank/cron-expression": "^3.1",
"egulias/email-validator": "^2.1.10|^3.1|^4",
"guzzlehttp/promises": "^1.4|^2.0",
"jolicode/jolinotif": "^2.7.2",
"league/html-to-markdown": "^5.0",
"league/uri": "^6.5|^7.0",
"masterminds/html5": "^2.7.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2766,6 +2766,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
}
$container->getDefinition('notifier.channel.sms')->setArgument(0, null);
$container->getDefinition('notifier.channel.push')->setArgument(0, null);
$container->getDefinition('notifier.channel.desktop')->setArgument(0, null);
}

$container->getDefinition('notifier.channel_policy')->setArgument(0, $config['channel_policy']);
Expand Down Expand Up @@ -2801,6 +2802,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
NotifierBridge\Infobip\InfobipTransportFactory::class => 'notifier.transport_factory.infobip',
NotifierBridge\Iqsms\IqsmsTransportFactory::class => 'notifier.transport_factory.iqsms',
NotifierBridge\Isendpro\IsendproTransportFactory::class => 'notifier.transport_factory.isendpro',
NotifierBridge\JoliNotif\JoliNotifTransportFactory::class => 'notifier.transport_factory.joli-notif',
NotifierBridge\KazInfoTeh\KazInfoTehTransportFactory::class => 'notifier.transport_factory.kaz-info-teh',
NotifierBridge\LightSms\LightSmsTransportFactory::class => 'notifier.transport_factory.light-sms',
NotifierBridge\LineNotify\LineNotifyTransportFactory::class => 'notifier.transport_factory.line-notify',
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Resources/config/notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Notifier\Channel\BrowserChannel;
use Symfony\Component\Notifier\Channel\ChannelPolicy;
use Symfony\Component\Notifier\Channel\ChatChannel;
use Symfony\Component\Notifier\Channel\DesktopChannel;
use Symfony\Component\Notifier\Channel\EmailChannel;
use Symfony\Component\Notifier\Channel\PushChannel;
use Symfony\Component\Notifier\Channel\SmsChannel;
Expand All @@ -24,6 +25,7 @@
use Symfony\Component\Notifier\EventListener\SendFailedMessageToNotifierListener;
use Symfony\Component\Notifier\FlashMessage\DefaultFlashMessageImportanceMapper;
use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\Notifier\Message\DesktopMessage;
use Symfony\Component\Notifier\Message\PushMessage;
use Symfony\Component\Notifier\Message\SmsMessage;
use Symfony\Component\Notifier\Messenger\MessageHandler;
Expand Down Expand Up @@ -76,6 +78,10 @@
->args([service('texter.transports'), service('messenger.default_bus')->ignoreOnInvalid()])
->tag('notifier.channel', ['channel' => 'push'])

->set('notifier.channel.desktop', DesktopChannel::class)
->args([service('texter.transports'), service('messenger.default_bus')->ignoreOnInvalid()])
->tag('notifier.channel', ['channel' => 'desktop'])

->set('notifier.monolog_handler', NotifierHandler::class)
->args([service('notifier')])

Expand Down Expand Up @@ -126,6 +132,10 @@
->args([service('texter.transports')])
->tag('messenger.message_handler', ['handles' => PushMessage::class])

->set('texter.messenger.desktop_handler', MessageHandler::class)
->args([service('texter.transports')])
->tag('messenger.message_handler', ['handles' => DesktopMessage::class])

->set('notifier.notification_logger_listener', NotificationLoggerListener::class)
->tag('kernel.event_subscriber')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
'infobip' => Bridge\Infobip\InfobipTransportFactory::class,
'iqsms' => Bridge\Iqsms\IqsmsTransportFactory::class,
'isendpro' => Bridge\Isendpro\IsendproTransportFactory::class,
'joli-notif' => Bridge\JoliNotif\JoliNotifTransportFactory::class,
'kaz-info-teh' => Bridge\KazInfoTeh\KazInfoTehTransportFactory::class,
'light-sms' => Bridge\LightSms\LightSmsTransportFactory::class,
'lox24' => Bridge\Lox24\Lox24TransportFactory::class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/Tests export-ignore
/phpunit.xml.dist export-ignore
/.git* export-ignore
3 changes: 3 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/JoliNotif/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vendor/
composer.lock
phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CHANGELOG
=========

7.2
---

* Add the bridge
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?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\JoliNotif;

use Symfony\Component\Notifier\Exception\InvalidArgumentException;
use Symfony\Component\Notifier\Message\MessageOptionsInterface;

/**
* @author Ahmed Ghanem <ahmedghanem7361@gmail.com>
*/
final class JoliNotifOptions implements MessageOptionsInterface
{
public function __construct(
private ?string $iconPath = null,
private array $extraOptions = [],
) {
}

public function toArray(): array
{
return [
'icon_path' => $this->iconPath,
'extra_options' => $this->extraOptions,
];
}

public function getRecipientId(): ?string
{
return null;
}

/**
* @return $this
*/
public function setIconPath(string $iconPath): static
{
$this->iconPath = $iconPath;

return $this;
}

public function getIconPath(): ?string
{
return $this->iconPath;
}

/**
* Extra options maybe supported and effective by the JoliNotif package on some operating systems
* while not on others.
* For more details, you can always check the package page on GitHub (https://github.com/jolicode/JoliNotif).
*
* @return $this
*/
public function setExtraOption(string $key, string|int $value): static
{
$this->extraOptions[$key] = $value;

return $this;
}

public function getExtraOption(string $key): string|int
{
if (!isset($this->extraOptions[$key])) {
throw new InvalidArgumentException(\sprintf('The extra option "%s" cannot be fetched as it does not exist.', $key));
}

return $this->extraOptions[$key];
}

public function getExtraOptions(): array
{
return $this->extraOptions;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?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\JoliNotif;

use Joli\JoliNotif\DefaultNotifier as JoliNotifier;
use Joli\JoliNotif\Notification as JoliNotification;
use Symfony\Component\Notifier\Exception\LogicException;
use Symfony\Component\Notifier\Exception\RuntimeException;
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
use Symfony\Component\Notifier\Message\DesktopMessage;
use Symfony\Component\Notifier\Message\MessageInterface;
use Symfony\Component\Notifier\Message\SentMessage;
use Symfony\Component\Notifier\Transport\AbstractTransport;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

/**
* @author Ahmed Ghanem <ahmedghanem7361@gmail.com>
*/
final class JoliNotifTransport extends AbstractTransport
{
public function __construct(
private readonly JoliNotifier $joliNotifier,
?EventDispatcherInterface $dispatcher = null,
) {
parent::__construct(null, $dispatcher);
}

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

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

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

if (($options = $message->getOptions()) && !$options instanceof JoliNotifOptions) {
throw new LogicException(\sprintf('The "%s" transport only supports an instance of the "%s" as an option class.', __CLASS__, JoliNotifOptions::class));
}

$joliNotification = $this->buildJoliNotificationObject($message, $options);

if (false === $this->joliNotifier->send($joliNotification)) {
throw new RuntimeException(\sprintf('An error occurred while sending a notification via the "%s" transport.', __CLASS__));
}

return new SentMessage($message, (string) $this);
}

private function buildJoliNotificationObject(DesktopMessage $message, ?JoliNotifOptions $options = null): JoliNotification
{
$joliNotification = new JoliNotification();

$joliNotification->setTitle($message->getSubject());
$joliNotification->setBody($message->getContent());

if ($options) {
if ($iconPath = $options->getIconPath()) {
$joliNotification->setIcon($iconPath);
}

foreach ($options->getExtraOptions() as $extraOptionKey => $extraOptionValue) {
$joliNotification->addOption($extraOptionKey, $extraOptionValue);
}
}

return $joliNotification;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?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\JoliNotif;

use Joli\JoliNotif\DefaultNotifier as JoliNotifier;
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
use Symfony\Component\Notifier\Transport\Dsn;

/**
* @author Ahmed Ghanem <ahmedghanem7361@gmail.com>
*/
final class JoliNotifTransportFactory extends AbstractTransportFactory
{
private const SCHEME_NAME = 'jolinotif';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should the scheme be joli-notif to be consistent with the package name ?

Copy link
Contributor Author

@ahmedghanem00 ahmedghanem00 Aug 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually feel that it's more readable without the dash, I also have seen many similar bridges following the same pattern in naming the scheme without applying the kebab-case.


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

return (new JoliNotifTransport(new JoliNotifier(), $this->dispatcher))->setHost($dsn->getHost())->setPort($dsn->getPort());
}

/**
* @return string[]
*/
protected function getSupportedSchemes(): array
{
return [
self::SCHEME_NAME,
];
}
}
19 changes: 19 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/JoliNotif/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2024-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.
20 changes: 20 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/JoliNotif/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
JoliNotif Notifier Bridge
=========================

Provides a [JoliNotif](https://github.com/jolicode/JoliNotif) integration for
the Symfony Notifier Component.

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

```
JOLINOTIF_DSN=jolinotif://default
```

Resources
ahmedghanem00 marked this conversation as resolved.
Show resolved Hide resolved
---------

* [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 <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\JoliNotif\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Notifier\Bridge\JoliNotif\JoliNotifOptions;
use Symfony\Component\Notifier\Exception\InvalidArgumentException;

/**
* @author Ahmed Ghanem <ahmedghanem7361@gmail.com>
*/
class JoliNotifOptionsTest extends TestCase
{
public function testToArray()
{
$joliOptions = new JoliNotifOptions();
ahmedghanem00 marked this conversation as resolved.
Show resolved Hide resolved

$joliOptions->setIconPath('/sample/icon/path');
$joliOptions->setExtraOption('subtitle', 'This is a subtitle');
$joliOptions->setExtraOption('sound', 'Frog');

$this->assertSame([
'icon_path' => '/sample/icon/path',
'extra_options' => [
'subtitle' => 'This is a subtitle',
'sound' => 'Frog',
],
], $joliOptions->toArray());
}

public function testNonExistExtraOption()
{
$joliOptions = new JoliNotifOptions();

$this->expectException(InvalidArgumentException::class);

$joliOptions->getExtraOption('non-exist-option');
}
}
Loading
Loading