-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
565 additions
and
310 deletions.
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
Large diffs are not rendered by default.
Oops, something went wrong.
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,12 @@ | ||
framework: | ||
notifier: | ||
chatter_transports: | ||
texter_transports: | ||
channel_policy: | ||
# use chat/slack, chat/telegram, sms/twilio or sms/nexmo | ||
urgent: ['email'] | ||
high: ['email'] | ||
medium: ['email'] | ||
low: ['email'] | ||
admin_recipients: | ||
- { email: [email protected] } |
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
53 changes: 53 additions & 0 deletions
53
packages/sonata-extra-bundle/Notifier/Channel/SonataChannel.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,53 @@ | ||
<?php | ||
|
||
namespace Draw\Bundle\SonataExtraBundle\Notifier\Channel; | ||
|
||
use Draw\Bundle\SonataExtraBundle\Notifier\Notification\SonataNotificationInterface; | ||
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Symfony\Component\HttpFoundation\Session\Session; | ||
use Symfony\Component\Notifier\Channel\ChannelInterface; | ||
use Symfony\Component\Notifier\Notification\Notification; | ||
use Symfony\Component\Notifier\Recipient\RecipientInterface; | ||
|
||
#[AutoconfigureTag( | ||
'notifier.channel', | ||
['channel' => 'sonata'] | ||
)] | ||
class SonataChannel implements ChannelInterface | ||
{ | ||
public function __construct(private RequestStack $stack) | ||
{ | ||
} | ||
|
||
public function notify(Notification $notification, RecipientInterface $recipient, ?string $transportName = null): void | ||
{ | ||
if (null === $request = $this->stack->getCurrentRequest()) { | ||
return; | ||
} | ||
|
||
if (!$request->hasSession(true)) { | ||
return; | ||
} | ||
|
||
$message = $notification->getSubject(); | ||
if ($notification->getEmoji()) { | ||
$message = $notification->getEmoji().' '.$message; | ||
} | ||
|
||
$session = $request->getSession(); | ||
|
||
\assert($session instanceof Session); | ||
|
||
$type = $notification instanceof SonataNotificationInterface | ||
? 'sonata_flash_'.$notification->getSonataFlashType() | ||
: 'sonata_flash_success'; | ||
|
||
$session->getFlashBag()->add($type, $message); | ||
} | ||
|
||
public function supports(Notification $notification, RecipientInterface $recipient): bool | ||
{ | ||
return true; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
packages/sonata-extra-bundle/Notifier/Notification/SonataNotification.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,37 @@ | ||
<?php | ||
|
||
namespace Draw\Bundle\SonataExtraBundle\Notifier\Notification; | ||
|
||
use Symfony\Component\Notifier\Notification\Notification; | ||
|
||
class SonataNotification extends Notification implements SonataNotificationInterface | ||
{ | ||
private string $sonataFlashType = 'success'; | ||
|
||
public static function success(string $message): self | ||
{ | ||
return (new self($message))->setSonataFlashType('success'); | ||
} | ||
|
||
public static function error(string $message): self | ||
{ | ||
return (new self($message))->setSonataFlashType('error'); | ||
} | ||
|
||
public function __construct(string $subject = '', array $channels = ['sonata']) | ||
{ | ||
parent::__construct($subject, $channels); | ||
} | ||
|
||
public function getSonataFlashType(): string | ||
{ | ||
return $this->sonataFlashType; | ||
} | ||
|
||
public function setSonataFlashType(string $sonataFlashType): static | ||
{ | ||
$this->sonataFlashType = $sonataFlashType; | ||
|
||
return $this; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
packages/sonata-extra-bundle/Notifier/Notification/SonataNotificationInterface.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,8 @@ | ||
<?php | ||
|
||
namespace Draw\Bundle\SonataExtraBundle\Notifier\Notification; | ||
|
||
interface SonataNotificationInterface | ||
{ | ||
public function getSonataFlashType(): 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
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
31 changes: 31 additions & 0 deletions
31
...ta-extra-bundle/Tests/DependencyInjection/DrawSonataExtraExtensionNotifierEnabledTest.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,31 @@ | ||
<?php | ||
|
||
namespace DependencyInjection; | ||
|
||
use Draw\Bundle\SonataExtraBundle\DependencyInjection\DrawSonataExtraExtension; | ||
use Draw\Bundle\SonataExtraBundle\Notifier\Channel\SonataChannel; | ||
use Draw\Bundle\SonataExtraBundle\Tests\DependencyInjection\DrawSonataExtraExtensionTest; | ||
use Symfony\Component\DependencyInjection\Extension\Extension; | ||
|
||
class DrawSonataExtraExtensionNotifierEnabledTest extends DrawSonataExtraExtensionTest | ||
{ | ||
public function createExtension(): Extension | ||
{ | ||
return new DrawSonataExtraExtension(); | ||
} | ||
|
||
public function getConfiguration(): array | ||
{ | ||
return [ | ||
'notifier' => [ | ||
'enabled' => true, | ||
], | ||
]; | ||
} | ||
|
||
public static function provideTestHasServiceDefinition(): iterable | ||
{ | ||
yield from parent::provideTestHasServiceDefinition(); | ||
yield [SonataChannel::class]; | ||
} | ||
} |
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
Oops, something went wrong.