-
-
Notifications
You must be signed in to change notification settings - Fork 9.5k
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
fabpot
merged 1 commit into
symfony:7.2
from
ahmedghanem00:feature/notifier-desktop-notitications
Aug 27, 2024
Merged
Changes from all commits
Commits
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 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 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 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 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
3 changes: 3 additions & 0 deletions
3
src/Symfony/Component/Notifier/Bridge/JoliNotif/.gitattributes
This file contains 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 @@ | ||
/Tests export-ignore | ||
/phpunit.xml.dist export-ignore | ||
/.git* export-ignore |
This file contains 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 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 | ||
========= | ||
|
||
7.2 | ||
--- | ||
|
||
* Add the bridge |
83 changes: 83 additions & 0 deletions
83
src/Symfony/Component/Notifier/Bridge/JoliNotif/JoliNotifOptions.php
This file contains 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,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; | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
src/Symfony/Component/Notifier/Bridge/JoliNotif/JoliNotifTransport.php
This file contains 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,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; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/Symfony/Component/Notifier/Bridge/JoliNotif/JoliNotifTransportFactory.php
This file contains 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,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'; | ||
|
||
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, | ||
]; | ||
} | ||
} |
This file contains 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) 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. |
This file contains 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,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) |
48 changes: 48 additions & 0 deletions
48
src/Symfony/Component/Notifier/Bridge/JoliNotif/Tests/JoliNotifOptionsTest.php
This file contains 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,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'); | ||
} | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
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 ?There was a problem hiding this comment.
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.