Skip to content

Commit

Permalink
IBX-7057: Provided NotificationService implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
webhdx committed Nov 15, 2023
1 parent 28ce3a2 commit fbe0a3f
Show file tree
Hide file tree
Showing 9 changed files with 219 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/bundle/IbexaNotificationsBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,17 @@

namespace Ibexa\Bundle\Notifications;

use Ibexa\Bundle\Notifications\DependencyInjection\Configuration\Parser\NotificationsConfigParser;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

final class IbexaNotificationsBundle extends Bundle
{
public function build(ContainerBuilder $container): void
{
/** @var \Ibexa\Bundle\Core\DependencyInjection\IbexaCoreExtension $core */
$core = $container->getExtension('ibexa');

$core->addConfigParser(new NotificationsConfigParser());
}
}
3 changes: 2 additions & 1 deletion src/bundle/Resources/config/services.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
imports:
- { resource: services/**/*.yaml }
- { resource: services/papi.yaml }
- { resource: services/subscription_resolver.yaml }
9 changes: 9 additions & 0 deletions src/bundle/Resources/config/services/papi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
services:
_defaults:
autowire: true
autoconfigure: true
public: false

Ibexa\Notifications\Service\NotificationService: ~

Ibexa\Contracts\Notifications\Service\NotificationServiceInterface: '@Ibexa\Notifications\Service\NotificationService'
19 changes: 19 additions & 0 deletions src/contracts/Service/NotificationServiceInterface.php
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\Service;

use Ibexa\Contracts\Notifications\Value\NotificationInterface;

interface NotificationServiceInterface
{
/**
* @param array<\Ibexa\Contracts\Notifications\Value\RecipientInterface> $recipients
*/
public function send(NotificationInterface $notification, array $recipients = []): void;
}
32 changes: 32 additions & 0 deletions src/contracts/Value/Notification/SymfonyNotificationAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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\Notification;

use Ibexa\Contracts\Notifications\Value\NotificationInterface;
use Symfony\Component\Notifier\Notification\Notification;

final class SymfonyNotificationAdapter implements NotificationInterface
{
private Notification $notification;

public function __construct(Notification $notification)
{
$this->notification = $notification;
}

public function getType(): string
{
return get_class($this->notification);
}

public function getNotification(): Notification
{
return $this->notification;
}
}
14 changes: 14 additions & 0 deletions src/contracts/Value/NotificationInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?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;

interface NotificationInterface
{
public function getType(): string;
}
37 changes: 37 additions & 0 deletions src/contracts/Value/Recipent/SymfonyRecipientAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?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\Notifications\Value\RecipientInterface;
use Symfony\Component\Notifier\Recipient\Recipient;

final class SymfonyRecipientAdapter implements RecipientInterface
{
private Recipient $recipient;

public function __construct(Recipient $recipient)
{
$this->recipient = $recipient;
}

public function getMail(): string
{
return $this->recipient->getEmail();
}

public function getPhone(): string
{
return $this->recipient->getPhone();
}

public function getRecipient(): Recipient
{
return $this->recipient;
}
}
16 changes: 16 additions & 0 deletions src/contracts/Value/RecipientInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?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;

interface RecipientInterface
{
public function getMail(): string;

public function getPhone(): string;
}
81 changes: 81 additions & 0 deletions src/lib/Service/NotificationService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?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\Service;

use Ibexa\Contracts\Notifications\Service\NotificationServiceInterface;
use Ibexa\Contracts\Notifications\Value\Notification\SymfonyNotificationAdapter;
use Ibexa\Contracts\Notifications\Value\NotificationInterface;
use Ibexa\Contracts\Notifications\Value\Recipent\SymfonyRecipientAdapter;
use Ibexa\Notifications\SubscriptionResolver\SubscriptionResolverInterface;
use Ibexa\Notifications\Value\ChannelSubscription;
use Symfony\Component\Notifier\Notification\Notification;
use Symfony\Component\Notifier\NotifierInterface;
use Symfony\Component\Notifier\Recipient\Recipient;

final class NotificationService implements NotificationServiceInterface
{
private NotifierInterface $notifier;

private SubscriptionResolverInterface $subscriptionResolver;

public function __construct(
NotifierInterface $notifier,
SubscriptionResolverInterface $subscriptionResolver
) {
$this->notifier = $notifier;
$this->subscriptionResolver = $subscriptionResolver;
}

/**
* @param array<\Ibexa\Contracts\Notifications\Value\RecipientInterface> $recipients
*/
public function send(NotificationInterface $notification, array $recipients = []): void
{
$channels = array_map(
static fn (ChannelSubscription $channelSubscription): string => $channelSubscription->getChannel(),
array_filter(iterator_to_array($this->subscriptionResolver->resolve($notification)))
);

if (empty($channels)) {
return;
}

$symfonyRecipients = $this->getSymfonyRecipients($recipients);
$symfonyNotification = $notification instanceof SymfonyNotificationAdapter
? $notification->getNotification()
: new Notification();

$symfonyNotification->channels($channels);

$this->notifier->send(
$symfonyNotification,
...$symfonyRecipients,
);
}

/**
* @param array<\Ibexa\Contracts\Notifications\Value\RecipientInterface> $recipients
*
* @return array<\Symfony\Component\Notifier\Recipient\RecipientInterface>
*/
private function getSymfonyRecipients(array $recipients): array
{
$symfonyRecipients = [];
foreach ($recipients as $recipient) {
$symfonyRecipients[] = $recipient instanceof SymfonyRecipientAdapter
? $recipient->getRecipient()
: new Recipient(
$recipient->getMail(),
$recipient->getPhone(),
);
}

return $symfonyRecipients;
}
}

0 comments on commit fbe0a3f

Please sign in to comment.