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

IBX-8736: Allowed to sent user notification via symfony/notifier #11

Merged
merged 2 commits into from
Nov 23, 2024

Conversation

adamwojs
Copy link
Member

@adamwojs adamwojs commented Nov 9, 2024

🎫 Issue IBX-8736

Description:

Allowed to sent user notification via symfony/notifier package.

Basic notifications

Usage example:

use Ibexa\Contracts\Notifications\SystemNotification\SystemNotification;
use Ibexa\Contracts\Notifications\Value\Recipent\UserRecipient;

$user = $userService->loadUserByLogin('[email protected]')

$notification = new SystemNotification('Hello John!', ['ibexa']);
$notification->setIcon('bell');
$notification->setContent("It's works!");
$notification->setRoute(new RouteReference('ibexa.content.view', ['contentId' => 52]));

$notifier->send($notification, new UserRecipient($user));

Custom notification

It's possible to customize notification by implementing \Ibexa\Contracts\Notifications\SystemNotification\SystemNotificationInterface:

namespace Ibexa\Contracts\Notifications\SystemNotification;

use Ibexa\Contracts\Notifications\Value\Recipent\UserRecipientInterface;

interface SystemNotificationInterface
{
    public function asSystemNotification(
        UserRecipientInterface $recipient,
        ?string $transport = null
    ): ?SystemMessage;
}

Usage example:

<?php

declare(strict_types=1);

namespace App\Notification;

use Ibexa\Contracts\Notifications\SystemNotification\SystemMessage;
use Ibexa\Contracts\Notifications\SystemNotification\SystemNotificationInterface;
use Ibexa\Contracts\Notifications\Value\Recipent\UserRecipientInterface;
use Symfony\Component\Notifier\Notification\Notification;

final class WelcomeNotification extends Notification implements SystemNotificationInterface
{
    public function asSystemNotification(UserRecipientInterface $recipient, ?string $transport = null): ?SystemMessage
    {
        $message = new SystemMessage($recipient->getUser());
        $message->setType('welcome');
        $message->setContext([
            'subject' => $this->getSubject(),
            'content' => $this->getContent(),
        ]);

        return $message;
    }
}
use Ibexa\Contracts\Notifications\SystemNotification\SystemNotification;
use Ibexa\Contracts\Notifications\Value\Recipent\UserRecipient;

$user = $userService->loadUserByLogin('[email protected]')

$notification = new WelcomeNotification('Hello!', ['ibexa']);
// ...
$notifier->send($notification, new UserRecipient($user));

For QA:

N/A

Documentation:

N/A

@adamwojs adamwojs changed the base branch from main to 4.6 November 9, 2024 19:19
@adamwojs adamwojs marked this pull request as ready for review November 10, 2024 10:53
@adamwojs adamwojs requested a review from a team November 22, 2024 07:27
@konradoboza konradoboza requested a review from a team November 22, 2024 07:49
@ciastektk ciastektk requested a review from a team November 22, 2024 08:19
src/contracts/SystemNotification/SystemNotification.php Outdated Show resolved Hide resolved
tests/lib/Channel/SystemNotificationChannelTest.php Outdated Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

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

This is placed under integration namespace, while in practice this is a unit test. Do we expect contracts to be placed within this namespace?

Copy link
Member Author

Choose a reason for hiding this comment

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

Alternatively we can move it to tests/lib. Introducing dedicated namespace for contract unit tests seems to be overkill for me.

Please let me know what do you think and we will open follow up PR.

@adamwojs adamwojs merged commit ac7528a into 4.6 Nov 23, 2024
13 checks passed
@adamwojs adamwojs deleted the ibx_9736 branch November 23, 2024 10:15
@mnocon mnocon added the Doc needed The changes require some documentation label Nov 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Doc needed The changes require some documentation Ready for review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants