Skip to content

Commit

Permalink
fixup! IBX-8736: Allowed to sent user notification via symfony/notifier
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwojs committed Nov 22, 2024
1 parent 540f66c commit 955bde3
Show file tree
Hide file tree
Showing 10 changed files with 188 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/bundle/Resources/config/services.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
imports:
- { resource: services/channel.yaml }
- { resource: services/mappers.yaml }
- { resource: services/papi.yaml }
- { resource: services/subscription_resolver.yaml }
- { resource: services/system_notifications.yaml }
11 changes: 0 additions & 11 deletions src/bundle/Resources/config/services/channel.yaml

This file was deleted.

16 changes: 16 additions & 0 deletions src/bundle/Resources/config/services/system_notifications.yaml
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.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{% extends '@ibexadesign/account/notifications/list_item.html.twig' %}

{% trans_default_domain 'ibexa_notification' %}

{% block icon %}
<span class="type__icon">
<svg class="ibexa-icon ibexa-icon--review">
<use xlink:href="{{ ibexa_icon_path(icon|default('info')) }}"></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 %}
29 changes: 14 additions & 15 deletions src/contracts/SystemNotification/SystemMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,28 @@
namespace Ibexa\Contracts\Notifications\SystemNotification;

use Ibexa\Contracts\Core\Repository\Values\User\UserReference;
use Ibexa\Contracts\Notifications\Value\Recipent\UserRecipientInterface;
use Symfony\Component\Notifier\Message\MessageInterface;
use Symfony\Component\Notifier\Message\MessageOptionsInterface;
use Symfony\Component\Notifier\Notification\Notification;

final class SystemMessage implements MessageInterface
{
private ?string $transport = null;

private string $subject;
private 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, string $subject, array $context = [])
public function __construct(UserReference $user, array $context = [])
{
$this->user = $user;
$this->subject = $subject;
$this->context = $context;
}

Expand Down Expand Up @@ -62,12 +61,7 @@ public function setSubject(string $subject): void

public function getTransport(): ?string
{
return $this->transport;
}

public function setTransport(?string $transport): void
{
$this->transport = $transport;
return null;
}

public function getOptions(): ?MessageOptionsInterface
Expand All @@ -91,8 +85,13 @@ public function setContext(array $context): void
$this->context = $context;
}

public static function fromNotification(Notification $notification, UserRecipientInterface $recipient): self
public function getType(): string
{
return $this->type;
}

public function setType(string $type): void
{
return new self($recipient->getUser(), $notification->getSubject());
$this->type = $type;
}
}
64 changes: 64 additions & 0 deletions src/contracts/SystemNotification/SystemNotification.php
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
declare(strict_types=1);

namespace Ibexa\Notifications\Channel;
namespace Ibexa\Notifications\SystemNotification;

use Ibexa\Contracts\Core\Repository\NotificationService;
use Ibexa\Contracts\Core\Repository\Repository;
Expand Down Expand Up @@ -43,7 +43,7 @@ public function notify(Notification $notification, RecipientInterface $recipient

$createStruct = new CreateStruct();
$createStruct->ownerId = $message->getUser()->getUserId();
$createStruct->type = $message->getSubject();
$createStruct->type = $message->getType();
$createStruct->data = $message->getContext();

$this->repository->beginTransaction();
Expand Down
59 changes: 59 additions & 0 deletions src/lib/SystemNotification/SystemNotificationRenderer.php
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',
[
'icon' => $notification->data[self::KEY_ICON] ?? null,
'content' => $notification->data[self::KEY_CONTENT] ?? null,
'subject' => $notification->data[self::KEY_SUBJECT] ?? null,
'notification' => $notification,
'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] ?? []
);
}
}
9 changes: 5 additions & 4 deletions tests/lib/Channel/SystemNotificationChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Ibexa\Contracts\Notifications\SystemNotification\SystemMessage;
use Ibexa\Contracts\Notifications\SystemNotification\SystemNotificationInterface;
use Ibexa\Contracts\Notifications\Value\Recipent\UserRecipientInterface;
use Ibexa\Notifications\Channel\SystemNotificationChannel;
use Ibexa\Notifications\SystemNotification\SystemNotificationChannel;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Notifier\Notification\Notification;
use Symfony\Component\Notifier\Recipient\RecipientInterface;
Expand Down Expand Up @@ -92,10 +92,11 @@ public function testNotify(): void
$user = $this->createMock(UserReference::class);
$user->method('getUserId')->willReturn(self::EXAMPLE_USER_ID);

$message = new SystemMessage($user, ['foo' => 'bar']);
$message->setType('example');

$this->channel->notify(
$this->createSupportedNotification(
new SystemMessage($user, 'example', ['foo' => 'bar'])
),
$this->createSupportedNotification($message),
$this->createSupportedRecipient(self::EXAMPLE_USER_ID)
);
}
Expand Down

0 comments on commit 955bde3

Please sign in to comment.