Skip to content

Commit

Permalink
[SonataExtraBundle] Sonata Notifier
Browse files Browse the repository at this point in the history
  • Loading branch information
mpoiriert committed Jan 13, 2024
1 parent e951dc6 commit f3fc407
Show file tree
Hide file tree
Showing 14 changed files with 565 additions and 310 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@
"phpstan/phpstan-phpunit": "^1.1",
"scheb/2fa-email": "^6.0",
"colinodell/psr-testlogger": "^1.1",
"cweagans/composer-patches": "^1.7"
"cweagans/composer-patches": "^1.7",
"symfony/notifier": "^6.4.0"
},
"replace": {
"draw/application": "self.version",
Expand Down
597 changes: 337 additions & 260 deletions composer.lock

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions config/packages/notifier.yaml
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] }
14 changes: 14 additions & 0 deletions packages/sonata-extra-bundle/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Notifier\NotifierInterface;

class Configuration implements ConfigurationInterface
{
Expand All @@ -22,6 +24,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->append($this->createCanSecurityHandlerNode())
->append($this->createFixMenuDepthNode())
->append($this->createListFieldPriorityNode())
->append($this->createNotifierNode())
->append($this->createPreventDeleteExtensionNode())
->append($this->createSessionTimeoutNode())
->end();
Expand All @@ -35,6 +38,17 @@ private function createAutoHelpNode(): ArrayNodeDefinition
->canBeEnabled();
}

private function createNotifierNode(): ArrayNodeDefinition
{
$node = new ArrayNodeDefinition('notifier');

ContainerBuilder::willBeAvailable('symfony/notifier', NotifierInterface::class, [])
? $node->canBeDisabled()
: $node->canBeEnabled();

return $node;
}

private function createPreventDeleteExtensionNode(): ArrayNodeDefinition
{
return (new ArrayNodeDefinition('prevent_delete_extension'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Draw\Bundle\SonataExtraBundle\Extension\AutoActionExtension;
use Draw\Bundle\SonataExtraBundle\Extension\ListFieldPriorityExtension;
use Draw\Bundle\SonataExtraBundle\FieldDescriptionFactory\SubClassFieldDescriptionFactory;
use Draw\Bundle\SonataExtraBundle\Notifier\Channel\SonataChannel;
use Draw\Bundle\SonataExtraBundle\PreventDelete\Extension\PreventDeleteExtension;
use Draw\Bundle\SonataExtraBundle\PreventDelete\PreventDelete;
use Draw\Bundle\SonataExtraBundle\PreventDelete\PreventDeleteRelationLoader;
Expand Down Expand Up @@ -57,6 +58,10 @@ public function load(array $configs, ContainerBuilder $container): void
->setArgument('$restrictToRole', $config['prevent_delete_extension']['restrict_to_role']);
}

if (!($config['notifier']['enabled'] ?? false)) {
$container->removeDefinition(SonataChannel::class);
}

if (!($config['can_security_handler']['enabled'] ?? false)) {
$container->removeDefinition(CanSecurityHandler::class);
$container->removeDefinition(DefaultCanVoter::class);
Expand Down
53 changes: 53 additions & 0 deletions packages/sonata-extra-bundle/Notifier/Channel/SonataChannel.php
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;
}
}
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;
}
}
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;
}
1 change: 1 addition & 0 deletions packages/sonata-extra-bundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

<prototype namespace="Draw\Bundle\SonataExtraBundle\" resource="../..">
<exclude>../../{vendor,Annotation,Builder,Configuration,DependencyInjection,Entity,Event,Tests}</exclude>
<exclude>../../Notifier/Notification</exclude>
<exclude>../../{DrawSonataExtraBundle.php}</exclude>
</prototype>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ public function getDefaultConfiguration(): array
'enabled' => false,
'restrict_to_role' => null,
],
'notifier' => [
'enabled' => false,
],
'session_timeout' => [
'enabled' => false,
'delay' => 3600,
Expand Down
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];
}
}
3 changes: 2 additions & 1 deletion packages/sonata-extra-bundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"draw/security": "^0.11",
"phpunit/phpunit": "^9.0 || ^10.0",
"sonata-project/admin-bundle": "^4.8",
"sonata-project/doctrine-orm-admin-bundle": "^4.2"
"sonata-project/doctrine-orm-admin-bundle": "^4.2",
"symfony/notifier": "^6.4"
},
"minimum-stability": "dev",
"prefer-stable": true,
Expand Down
12 changes: 12 additions & 0 deletions symfony.lock
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,18 @@
"config/packages/monolog.yaml"
]
},
"symfony/notifier": {
"version": "6.4",
"recipe": {
"repo": "github.com/symfony/recipes",
"branch": "main",
"version": "5.0",
"ref": "178877daf79d2dbd62129dd03612cb1a2cb407cc"
},
"files": [
"config/packages/notifier.yaml"
]
},
"symfony/options-resolver": {
"version": "v5.3.7"
},
Expand Down
Loading

0 comments on commit f3fc407

Please sign in to comment.