8000 feature #57683 [Notifier] Support for desktop notifications via `joli… · symfony/symfony@825d9bd · GitHub
[go: up one dir, main page]

Skip to content

Commit 825d9bd

Browse files
committed
feature #57683 [Notifier] Support for desktop notifications via jolicode/JoliNotif (ahmedghanem00)
This PR was squashed before being merged into the 7.2 branch. Discussion ---------- [Notifier] Support for desktop notifications via `jolicode/JoliNotif` | Q | A | ------------- | --- | Branch? | 7.2 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | Fix #51485 | License | MIT Per the linked issue, this PR will provide the ability to display desktop notifications using the Symfony-Notifier-Component, via the underlying package `jolicode/JoliNotif` in the meantime, or via any other underlying package that may come or exist in the future (after creating the appropriate bridge and attach it to the `DesktopChannel` of course 😀). Two additional PRs will also be initiated against `symfony/docs` && `symfony/recipes` respectively, once the code review has some positive progress here. Thanks. Commits ------- bcd4677 [Notifier] Support for desktop notifications via `jolicode/JoliNotif`
2 parents 20bda2b + bcd4677 commit 825d9bd

24 files changed

+758
-0
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135
"dragonmantank/cron-expression": "^3.1",
136136
"egulias/email-validator": "^2.1.10|^3.1|^4",
137137
"guzzlehttp/promises": "^1.4|^2.0",
138+
"jolicode/jolinotif": "^2.7.2",
138139
"league/html-to-markdown": "^5.0",
139140
"league/uri": "^6.5|^7.0",
140141
"masterminds/html5": "^2.7.2",

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2766,6 +2766,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
27662766
}
27672767
$container->getDefinition('notifier.channel.sms')->setArgument(0, null);
27682768
$container->getDefinition('notifier.channel.push')->setArgument(0, null);
2769+
$container->getDefinition('notifier.channel.desktop')->setArgument(0, null);
27692770
}
27702771

27712772
$container->getDefinition('notifier.channel_policy')->setArgument(0, $config['channel_policy']);
@@ -2801,6 +2802,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
28012802
NotifierBridge\Infobip\InfobipTransportFactory::class => 'notifier.transport_factory.infobip',
28022803
NotifierBridge\Iqsms\IqsmsTransportFactory::class => 'notifier.transport_factory.iqsms',
28032804
NotifierBridge\Isendpro\IsendproTransportFactory::class => 'notifier.transport_factory.isendpro',
2805+
NotifierBridge\JoliNotif\JoliNotifTransportFactory::class => 'notifier.transport_factory.joli-notif',
28042806
NotifierBridge\KazInfoTeh\KazInfoTehTransportFactory::class => 'notifier.transport_factory.kaz-info-teh',
28052807
NotifierBridge\LightSms\LightSmsTransportFactory::class => 'notifier.transport_factory.light-sms',
28062808
NotifierBridge\LineNotify\LineNotifyTransportFactory::class => 'notifier.transport_factory.line-notify',

src/Symfony/Bundle/FrameworkBundle/Resources/config/notifier.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Notifier\Channel\BrowserChannel;
1616
use Symfony\Component\Notifier\Channel\ChannelPolicy;
1717
use Symfony\Component\Notifier\Channel\ChatChannel;
18+
use Symfony\Component\Notifier\Channel\DesktopChannel;
1819
use Symfony\Component\Notifier\Channel\EmailChannel;
1920
use Symfony\Component\Notifier\Channel\PushChannel;
2021
use Symfony\Component\Notifier\Channel\SmsChannel;
@@ -24,6 +25,7 @@
2425
use Symfony\Component\Notifier\EventListener\SendFailedMessageToNotifierListener;
2526
use Symfony\Component\Notifier\FlashMessage\DefaultFlashMessageImportanceMapper;
2627
use Symfony\Component\Notifier\Message\ChatMessage;
28+
use Symfony\Component\Notifier\Message\DesktopMessage;
2729
use Symfony\Component\Notifier\Message\PushMessage;
2830
use Symfony\Component\Notifier\Message\SmsMessage;
2931
use Symfony\Component\Notifier\Messenger\MessageHandler;
@@ -76,6 +78,10 @@
7678
->args([service('texter.transports'), service('messenger.default_bus')->ignoreOnInvalid()])
7779
->tag('notifier.channel', ['channel' => 'push'])
7880

81+
->set('notifier.channel.desktop', DesktopChannel::class)
82+
->args([service('texter.transports'), service('messenger.default_bus')->ignoreOnInvalid()])
83+
->tag('notifier.channel', ['channel' => 'desktop'])
84+
7985
->set('notifier.monolog_handler', NotifierHandler::class)
8086
->args([service('notifier')])
8187

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

135+
->set('texter.messenger.desktop_handler', MessageHandler::class)
136+
->args([service('texter.transports')])
137+
->tag('messenger.message_handler', ['handles' => DesktopMessage::class])
138+
129139
->set('notifier.notification_logger_listener', NotificationLoggerListener::class)
130140
->tag('kernel.event_subscriber')
131141

src/Symfony/Bundle/FrameworkBundle/Resources/config/notifier_transports.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
'infobip' => Bridge\Infobip\InfobipTransportFactory::class,
7373
'iqsms' => Bridge\Iqsms\IqsmsTransportFactory::class,
7474
'isendpro' => Bridge\Isendpro\IsendproTransportFactory::class,
75+
'joli-notif' => Bridge\JoliNotif\JoliNotifTransportFactory::class,
7576
'kaz-info-teh' => Bridge\KazInfoTeh\KazInfoTehTransportFactory::class,
7677
'light-sms' => Bridge\LightSms\LightSmsTransportFactory::class,
7778
'lox24' => Bridge\Lox24\Lox24TransportFactory::class,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/Tests export-ignore
2+
/phpunit.xml.dist export-ignore
3+
/.git* export-ignore
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vendor/
2+
composer.lock
3+
phpunit.xml
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CHANGELOG
2+
=========
3+
4+
7.2
5+
---
6+
7+
* Add the bridge
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\JoliNotif;
13+
14+
use Symfony\Component\Notifier\Exception\InvalidArgumentException;
15+
use Symfony\Component\Notifier\Message\MessageOptionsInterface;
16+
17+
/**
18+
* @author Ahmed Ghanem <ahmedghanem7361@gmail.com>
19+
*/
20+
final class JoliNotifOptions implements MessageOptionsInterface
21+
{
22+
public function __construct(
23+
private ?string $iconPath = null,
24+
private array $extraOptions = [],
25+
) {
26+
}
27+
28+
public function toArray(): array
29+
{
30+
return [
31+
'icon_path' => $this->iconPath,
32+
'extra_options' => $this->extraOptions,
33+
];
34+
}
35+
36+
public function getRecipientId(): ?string
37+
{
38+
return null;
39+
}
40+
41+
/**
42+
* @return $this
43+
*/
44+
public function setIconPath(string $iconPath): static
45+
{
46+
$this->iconPath = $iconPath;
47+
48+
return $this;
49+
}
50+
51+
public function getIconPath(): ?string
52+
{
53+
return $this->iconPath;
54+
}
55+
56+
/**
57+
* Extra options maybe supported and effective by the JoliNotif package on some operating systems
58+
* while not on others.
59+
* For more details, you can always check the package page on GitHub (https://github.com/jolicode/JoliNotif).
60+
*
61+
* @return $this
62+
*/
63+
public function setExtraOption(string $key, string|int $value): static
64+
{
65+
$this->extraOptions[$key] = $value;
66+
67+
return $this;
68+
}
69+
70+
public function getExtraOption(string $key): string|int
71+
{
72+
if (!isset($this->extraOptions[$key])) {
73+
throw new InvalidArgumentException(\sprintf('The extra option "%s" cannot be fetched as it does not exist.', $key));
74+
}
75+
76+
return $this->extraOptions[$key];
77+
}
78+
79+
public function getExtraOptions(): array
80+
{
81+
return $this->extraOptions;
82+
}
83+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\JoliNotif;
13+
14+
use Joli\JoliNotif\DefaultNotifier as JoliNotifier;
15+
use Joli\JoliNotif\Notification as JoliNotification;
16+
use Symfony\Component\Notifier\Exception\LogicException;
17+
use Symfony\Component\Notifier\Exception\RuntimeException;
18+
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
19+
use Symfony\Component\Notifier\Message\DesktopMessage;
20+
use Symfony\Component\Notifier\Message\MessageInterface;
21+
use Symfony\Component\Notifier\Message\SentMessage;
22+
use Symfony\Component\Notifier\Transport\AbstractTransport;
23+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
24+
25+
/**
26+
* @author Ahmed Ghanem <ahmedghanem7361@gmail.com>
27+
*/
28+
final class JoliNotifTransport extends AbstractTransport
29+
{
30+
public function __construct(
31+
10000 private readonly JoliNotifier $joliNotifier,
32+
?EventDispatcherInterface $dispatcher = null,
33+
) {
34+
parent::__construct(null, $dispatcher);
35+
}
36+
37+
public function __toString(): string
38+
{
39+
return \sprintf('jolinotif://%s', $this->getEndpoint());
40+
}
41+
42+
public function supports(MessageInterface $message): bool
43+
{
44+
return $message instanceof DesktopMessage && (null === $message->getOptions() || $message->getOptions() instanceof JoliNotifOptions);
45+
}
46+
47+
protected function doSend(MessageInterface $message): SentMessage
48+
{
49+
if (!$message instanceof DesktopMessage) {
50+
throw new UnsupportedMessageTypeException(__CLASS__, DesktopMessage::class, $message);
51+
}
52+
53+
if (($options = $message->getOptions()) && !$options instanceof JoliNotifOptions) {
54+
throw new LogicException(\sprintf('The "%s" transport only supports an instance of the "%s" as an option class.', __CLASS__, JoliNotifOptions::class));
55+
}
56+
57+
$joliNotification = $this->buildJoliNotificationObject($message, $options);
58+
59+
if (false === $this->joliNotifier->send($joliNotification)) {
60+
throw new RuntimeException(\sprintf('An error occurred while sending a notification via the "%s" transport.', __CLASS__));
61+
}
62+
63+
return new SentMessage($message, (string) $this);
64+
}
65+
66+
private function buildJoliNotificationObject(DesktopMessage $message, ?JoliNotifOptions $options = null): JoliNotification
67+
{
68+
$joliNotification = new JoliNotification();
69+
70+
$joliNotification->setTitle($message->getSubject());
71+
$joliNotification->setBody($message->getContent());
72+
73+
if ($options) {
74+
if ($iconPath = $options->getIconPath()) {
75+
$joliNotification->setIcon($iconPath);
76+
}
77+
78+
foreach ($options->getExtraOptions() as $extraOptionKey => $extraOptionValue) {
79+
$joliNotification->addOption($extraOptionKey, $extraOptionValue);
80+
}
81+
}
82+
83+
return $joliNotification;
84+
}
85+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\JoliNotif;
13+
14+
use Joli\JoliNotif\DefaultNotifier as JoliNotifier;
15+
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
16+
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
17+
use Symfony\Component\Notifier\Transport\Dsn;
18+
19+
/**
20+
* @author Ahmed Ghanem <ahmedghanem7361@gmail.com>
21+
*/
22+
final class JoliNotifTransportFactory extends AbstractTransportFactory
23+
{
24+
private const SCHEME_NAME = 'jolinotif';
25+
26+
public function create(Dsn $dsn): JoliNotifTransport
27+
{
28+
if (self::SCHEME_NAME !== $dsn->getScheme()) {
29+
throw new UnsupportedSchemeException($dsn, self::SCHEME_NAME, $this->getSupportedSchemes());
30+
}
31+
32+
return (new JoliNotifTransport(new JoliNotifier(), $this->dispatcher))->setHost($dsn->getHost())->setPort($dsn->getPort());
33+
}
34+
35+
/**
36+
* @return string[]
37+
*/
38+
protected function getSupportedSchemes(): array
39+
{
40+
return [
41+
self::SCHEME_NAME,
42+
];
43+
}
44+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2024-present Fabien Potencier
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is furnished
8+
to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
JoliNotif Notifier Bridge
2+
=========================
3+
4+
Provides a [JoliNotif](https://github.com/jolicode/JoliNotif) integration for
5+
the Symfony Notifier Component.
6+
7+
DSN example
8+
-----------
9+
10+
```
11+
JOLINOTIF_DSN=jolinotif://default
12+
```
13+
14+
Resources
15+
---------
16+
17+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
18+
* [Report issues](https://github.com/symfony/symfony/issues) and
19+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
20+
in the [main Symfony repository](https://github.com/symfony/symfony)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\JoliNotif\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Notifier\Bridge\JoliNotif\JoliNotifOptions;
16+
use Symfony\Component\Notifier\Exception\InvalidArgumentException;
17+
18+
/**
19+
* @author Ahmed Ghanem <ahmedghanem7361@gmail.com>
20+
*/
21+
class JoliNotifOptionsTest extends TestCase
22+
{
23+
public function testToArray()
24+
{
25+
$joliOptions = new JoliNotifOptions();
26+
27+
$joliOptions->setIconPath('/sample/icon/path');
28+
$joliOptions->setExtraOption('subtitle', 'This is a subtitle');
29+
$joliOptions->setExtraOption('sound', 'Frog');
30+
31+
$this->assertSame([
32+
'icon_path' => '/sample/icon/path',
33+
'extra_options' => [
34+
'subtitle' => 'This is a subtitle',
35+
'sound' => 'Frog',
36+
],
37+
], $joliOptions->toArray());
38+
}
39+
40+
public function testNonExistExtraOption()
41+
{
42+
$joliOptions = new JoliNotifOptions();
43+
44+
$this->expectException(InvalidArgumentException::class);
45+
46+
$joliOptions->getExtraOption('non-exist-option');
47+
}
48+
}

0 commit comments

Comments
 (0)
0