-
Notifications
You must be signed in to change notification settings - Fork 1
IBX-8736: Allowed to sent user notification via symfony/notifier #11
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
Merged
Changes from 1 commit
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 hidden or 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
16 changes: 16 additions & 0 deletions
16
src/bundle/Resources/config/services/system_notifications.yaml
This file contains hidden or 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,16 @@ | ||
| services: | ||
| _defaults: | ||
| autowire: true | ||
| autoconfigure: true | ||
| public: false | ||
|
|
||
| Ibexa\Notifications\SystemNotification\SystemNotificationChannel: | ||
| tags: | ||
| - name: notifier.channel | ||
| channel: ibexa | ||
|
|
||
| Ibexa\Notifications\SystemNotification\SystemNotificationRenderer: | ||
| tags: | ||
| - name: ibexa.notification.renderer | ||
| alias: system | ||
|
|
Empty file.
26 changes: 26 additions & 0 deletions
26
src/bundle/Resources/views/themes/admin/notification/system_notification.html.twig
This file contains hidden or 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,26 @@ | ||
| {% extends '@ibexadesign/account/notifications/list_item.html.twig' %} | ||
|
|
||
| {% trans_default_domain 'ibexa_notification' %} | ||
|
|
||
| {% block icon %} | ||
| {% set icon = icon|default('info') %} | ||
| <span class="type__icon"> | ||
| <svg class="ibexa-icon ibexa-icon--{{ icon }}"> | ||
| <use xlink:href="{{ ibexa_icon_path(icon) }}"></use> | ||
| </svg> | ||
| </span> | ||
| {% endblock %} | ||
|
|
||
| {% block notification_type %} | ||
| <span class="type__text"> | ||
| {{ subject|default('') }} | ||
| </span> | ||
| {% endblock %} | ||
|
|
||
| {% block message %} | ||
| {% embed '@ibexadesign/ui/component/table/table_body_cell.html.twig' with { class: 'ibexa-notifications-modal__description' } %} | ||
| {% block content %} | ||
| <p class="description__text">{{ content|default('') }}</p> | ||
| {% endblock %} | ||
| {% endembed %} | ||
| {% endblock %} |
This file contains hidden or 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,97 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
| * @license For full copyright and license information view LICENSE file distributed with this source code. | ||
| */ | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Ibexa\Contracts\Notifications\SystemNotification; | ||
|
|
||
| use Ibexa\Contracts\Core\Repository\Values\User\UserReference; | ||
| use Symfony\Component\Notifier\Message\MessageInterface; | ||
| use Symfony\Component\Notifier\Message\MessageOptionsInterface; | ||
|
|
||
| final class SystemMessage implements MessageInterface | ||
| { | ||
| public const DEFAULT_TYPE = 'system'; | ||
|
|
||
| private UserReference $user; | ||
|
|
||
| private string $type = self::DEFAULT_TYPE; | ||
|
|
||
| /** @var array<string, mixed> */ | ||
| private array $context; | ||
|
|
||
| private string $subject = ''; | ||
|
|
||
| /** | ||
| * @param array<string, mixed> $context | ||
| */ | ||
| public function __construct(UserReference $user, array $context = []) | ||
| { | ||
| $this->user = $user; | ||
| $this->context = $context; | ||
| } | ||
|
|
||
| public function getUser(): UserReference | ||
| { | ||
| return $this->user; | ||
| } | ||
|
|
||
| public function setUser(UserReference $user): void | ||
| { | ||
| $this->user = $user; | ||
| } | ||
|
|
||
| public function getRecipientId(): ?string | ||
| { | ||
| return (string) $this->user->getUserId(); | ||
| } | ||
|
|
||
| public function getSubject(): string | ||
| { | ||
| return $this->subject; | ||
| } | ||
|
|
||
| public function setSubject(string $subject): void | ||
| { | ||
| $this->subject = $subject; | ||
| } | ||
|
|
||
| public function getTransport(): ?string | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| public function getOptions(): ?MessageOptionsInterface | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * @return array<string, mixed> | ||
| */ | ||
| public function getContext(): array | ||
| { | ||
| return $this->context; | ||
| } | ||
|
|
||
| /** | ||
| * @param array<string, mixed> $context | ||
| */ | ||
| public function setContext(array $context): void | ||
| { | ||
| $this->context = $context; | ||
| } | ||
|
|
||
| public function getType(): string | ||
| { | ||
| return $this->type; | ||
| } | ||
|
|
||
| public function setType(string $type): void | ||
| { | ||
| $this->type = $type; | ||
| } | ||
| } |
This file contains hidden or 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,64 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
| * @license For full copyright and license information view LICENSE file distributed with this source code. | ||
| */ | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Ibexa\Contracts\Notifications\SystemNotification; | ||
|
|
||
| use Ibexa\Contracts\Notifications\Value\Recipent\UserRecipientInterface; | ||
| use Ibexa\Core\MVC\Symfony\Routing\RouteReference; | ||
| use Symfony\Component\Notifier\Notification\Notification; | ||
|
|
||
| final class SystemNotification extends Notification implements SystemNotificationInterface | ||
| { | ||
| private ?string $icon = null; | ||
|
|
||
| private ?RouteReference $route = null; | ||
|
|
||
| public function getIcon(): ?string | ||
| { | ||
| return $this->icon; | ||
| } | ||
|
|
||
| public function setIcon(?string $icon): void | ||
| { | ||
| $this->icon = $icon; | ||
| } | ||
|
|
||
| public function getRoute(): ?RouteReference | ||
| { | ||
| return $this->route; | ||
| } | ||
|
|
||
| public function setRoute(?RouteReference $route): void | ||
| { | ||
| $this->route = $route; | ||
| } | ||
|
|
||
| public function setContent(string $content): void | ||
| { | ||
| $this->content($content); | ||
| } | ||
|
|
||
| public function asSystemNotification(UserRecipientInterface $recipient, ?string $transport = null): ?SystemMessage | ||
| { | ||
| $context = [ | ||
| 'subject' => $this->getSubject(), | ||
| 'content' => $this->getContent(), | ||
| ]; | ||
|
|
||
| if ($this->icon !== null) { | ||
| $context['icon'] = $this->icon; | ||
| } | ||
|
|
||
| if ($this->route !== null) { | ||
| $context['route_name'] = $this->route->getRoute(); | ||
| $context['route_params'] = $this->route->getParams(); | ||
| } | ||
|
|
||
| return new SystemMessage($recipient->getUser(), $context); | ||
| } | ||
| } | ||
19 changes: 19 additions & 0 deletions
19
src/contracts/SystemNotification/SystemNotificationInterface.php
This file contains hidden or 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 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
| * @license For full copyright and license information view LICENSE file distributed with this source code. | ||
| */ | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Ibexa\Contracts\Notifications\SystemNotification; | ||
|
|
||
| use Ibexa\Contracts\Notifications\Value\Recipent\UserRecipientInterface; | ||
|
|
||
| interface SystemNotificationInterface | ||
| { | ||
| public function asSystemNotification( | ||
| UserRecipientInterface $recipient, | ||
| ?string $transport = null | ||
| ): ?SystemMessage; | ||
| } |
This file contains hidden or 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 hidden or 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,17 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
| * @license For full copyright and license information view LICENSE file distributed with this source code. | ||
| */ | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Ibexa\Contracts\Notifications\Value\Recipent; | ||
|
|
||
| use Ibexa\Contracts\Core\Repository\Values\User\UserReference; | ||
| use Symfony\Component\Notifier\Recipient\RecipientInterface; | ||
|
|
||
| interface UserRecipientInterface extends RecipientInterface | ||
| { | ||
| public function getUser(): UserReference; | ||
| } |
This file contains hidden or 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,63 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
| * @license For full copyright and license information view LICENSE file distributed with this source code. | ||
| */ | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Ibexa\Notifications\SystemNotification; | ||
|
|
||
| use Ibexa\Contracts\Core\Repository\NotificationService; | ||
| use Ibexa\Contracts\Core\Repository\Repository; | ||
| use Ibexa\Contracts\Core\Repository\Values\Notification\CreateStruct; | ||
| use Ibexa\Contracts\Notifications\SystemNotification\SystemNotificationInterface; | ||
| use Ibexa\Contracts\Notifications\Value\Recipent\UserRecipientInterface; | ||
| use Symfony\Component\Notifier\Channel\ChannelInterface; | ||
| use Symfony\Component\Notifier\Notification\Notification; | ||
| use Symfony\Component\Notifier\Recipient\RecipientInterface; | ||
| use Throwable; | ||
|
|
||
| final class SystemNotificationChannel implements ChannelInterface | ||
| { | ||
| private Repository $repository; | ||
|
|
||
| private NotificationService $notificationService; | ||
|
|
||
| public function __construct(Repository $repository, NotificationService $notificationService) | ||
| { | ||
| $this->repository = $repository; | ||
| $this->notificationService = $notificationService; | ||
| } | ||
|
|
||
| /** | ||
| * @param \Symfony\Component\Notifier\Notification\Notification&\Ibexa\Contracts\Notifications\SystemNotification\SystemNotificationInterface $notification | ||
| * @param \Ibexa\Contracts\Notifications\Value\Recipent\UserRecipientInterface $recipient | ||
| */ | ||
| public function notify(Notification $notification, RecipientInterface $recipient, ?string $transportName = null): void | ||
| { | ||
| $message = $notification->asSystemNotification($recipient, $transportName); | ||
| if ($message === null) { | ||
| return; | ||
| } | ||
|
|
||
| $createStruct = new CreateStruct(); | ||
| $createStruct->ownerId = $message->getUser()->getUserId(); | ||
| $createStruct->type = $message->getType(); | ||
| $createStruct->data = $message->getContext(); | ||
|
|
||
| $this->repository->beginTransaction(); | ||
| try { | ||
| $this->notificationService->createNotification($createStruct); | ||
| $this->repository->commit(); | ||
| } catch (Throwable $e) { | ||
| $this->repository->rollback(); | ||
| throw $e; | ||
| } | ||
| } | ||
|
|
||
| public function supports(Notification $notification, RecipientInterface $recipient): bool | ||
| { | ||
| return $notification instanceof SystemNotificationInterface && $recipient instanceof UserRecipientInterface; | ||
| } | ||
| } |
This file contains hidden or 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,59 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
| * @license For full copyright and license information view LICENSE file distributed with this source code. | ||
| */ | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Ibexa\Notifications\SystemNotification; | ||
|
|
||
| use Ibexa\Contracts\Core\Repository\Values\Notification\Notification; | ||
| use Ibexa\Core\Notification\Renderer\NotificationRenderer; | ||
| use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||
| use Twig\Environment; | ||
|
|
||
| final class SystemNotificationRenderer implements NotificationRenderer | ||
| { | ||
| private const KEY_ICON = 'icon'; | ||
| private const KEY_ROUTE_NAME = 'route_name'; | ||
| private const KEY_ROUTE_PARAMS = 'route_params'; | ||
| private const KEY_CONTENT = 'content'; | ||
| private const KEY_SUBJECT = 'subject'; | ||
|
|
||
| private Environment $twig; | ||
|
|
||
| private UrlGeneratorInterface $urlGenerator; | ||
|
|
||
| public function __construct(Environment $twig, UrlGeneratorInterface $urlGenerator) | ||
| { | ||
| $this->twig = $twig; | ||
| $this->urlGenerator = $urlGenerator; | ||
| } | ||
|
|
||
| public function render(Notification $notification): string | ||
| { | ||
| return $this->twig->render( | ||
| '@ibexadesign/notification/system_notification.html.twig', | ||
| [ | ||
| 'notification' => $notification, | ||
| 'icon' => $notification->data[self::KEY_ICON] ?? null, | ||
| 'content' => $notification->data[self::KEY_CONTENT] ?? null, | ||
| 'subject' => $notification->data[self::KEY_SUBJECT] ?? null, | ||
| 'created_at' => $notification->created, | ||
| ] | ||
| ); | ||
| } | ||
|
|
||
| public function generateUrl(Notification $notification): ?string | ||
| { | ||
| if (!isset($notification->data[self::KEY_ROUTE_NAME])) { | ||
| return null; | ||
| } | ||
|
|
||
| return $this->urlGenerator->generate( | ||
| $notification->data[self::KEY_ROUTE_NAME], | ||
| $notification->data[self::KEY_ROUTE_PARAMS] ?? [] | ||
| ); | ||
| } | ||
| } |
Empty file.
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.
Uh oh!
There was an error while loading. Please reload this page.