-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IBX-7057: Provided NotificationService implementation
- Loading branch information
Showing
9 changed files
with
219 additions
and
1 deletion.
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
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 } |
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,9 @@ | ||
services: | ||
_defaults: | ||
autowire: true | ||
autoconfigure: true | ||
public: false | ||
|
||
Ibexa\Notifications\Service\NotificationService: ~ | ||
|
||
Ibexa\Contracts\Notifications\Service\NotificationServiceInterface: '@Ibexa\Notifications\Service\NotificationService' |
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 @@ | ||
<?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
32
src/contracts/Value/Notification/SymfonyNotificationAdapter.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,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; | ||
} | ||
} |
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,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; | ||
} |
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,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; | ||
} | ||
} |
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,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; | ||
} |
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,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; | ||
} | ||
} |