notificaiton: load interface automatically

This commit is contained in:
Julien Fastré 2021-12-25 22:53:35 +01:00
parent 5cebcfddb7
commit f6f0786d38
4 changed files with 22 additions and 15 deletions

View File

@ -22,6 +22,7 @@ use Chill\MainBundle\DependencyInjection\CompilerPass\TimelineCompilerClass;
use Chill\MainBundle\DependencyInjection\CompilerPass\WidgetsCompilerPass; use Chill\MainBundle\DependencyInjection\CompilerPass\WidgetsCompilerPass;
use Chill\MainBundle\DependencyInjection\ConfigConsistencyCompilerPass; use Chill\MainBundle\DependencyInjection\ConfigConsistencyCompilerPass;
use Chill\MainBundle\DependencyInjection\RoleProvidersCompilerPass; use Chill\MainBundle\DependencyInjection\RoleProvidersCompilerPass;
use Chill\MainBundle\Notification\NotificationHandlerInterface;
use Chill\MainBundle\Routing\LocalMenuBuilderInterface; use Chill\MainBundle\Routing\LocalMenuBuilderInterface;
use Chill\MainBundle\Search\SearchApiInterface; use Chill\MainBundle\Search\SearchApiInterface;
use Chill\MainBundle\Security\ProvideRoleInterface; use Chill\MainBundle\Security\ProvideRoleInterface;
@ -50,6 +51,8 @@ class ChillMainBundle extends Bundle
->addTag('chill.render_entity'); ->addTag('chill.render_entity');
$container->registerForAutoconfiguration(SearchApiInterface::class) $container->registerForAutoconfiguration(SearchApiInterface::class)
->addTag('chill.search_api_provider'); ->addTag('chill.search_api_provider');
$container->registerForAutoconfiguration(NotificationHandlerInterface::class)
->addTag('chill_main.notification_handler');
$container->addCompilerPass(new SearchableServicesCompilerPass()); $container->addCompilerPass(new SearchableServicesCompilerPass());
$container->addCompilerPass(new ConfigConsistencyCompilerPass()); $container->addCompilerPass(new ConfigConsistencyCompilerPass());

View File

@ -11,29 +11,30 @@ declare(strict_types=1);
namespace Chill\MainBundle\Notification; namespace Chill\MainBundle\Notification;
use Chill\ActivityBundle\Notification\ActivityNotificationRenderer;
use Chill\MainBundle\Entity\Notification; use Chill\MainBundle\Entity\Notification;
use Chill\MainBundle\Notification\Exception\NotificationHandlerNotFound; use Chill\MainBundle\Notification\Exception\NotificationHandlerNotFound;
use Chill\PersonBundle\Notification\AccompanyingPeriodNotificationRenderer; use Doctrine\ORM\EntityManagerInterface;
final class NotificationHandlerManager final class NotificationHandlerManager
{ {
private array $renderers; private EntityManagerInterface $em;
private iterable $handlers;
public function __construct( public function __construct(
AccompanyingPeriodNotificationRenderer $accompanyingPeriodNotificationRenderer, iterable $handlers,
ActivityNotificationRenderer $activityNotificationRenderer EntityManagerInterface $em
) { ) {
// TODO configure automatically $this->handlers = $handlers;
// TODO CREER UNE INTERFACE POUR ETRE SUR QUE LES RENDERERS SONT OK $this->em = $em;
$this->renderers[] = $accompanyingPeriodNotificationRenderer;
$this->renderers[] = $activityNotificationRenderer;
} }
/**
* @throw NotificationHandlerNotFound if handler is not found
*/
public function getHandler(Notification $notification): NotificationHandlerInterface public function getHandler(Notification $notification): NotificationHandlerInterface
{ {
foreach ($this->renderers as $renderer) { foreach ($this->handlers as $renderer) {
if ($renderer->supports($notification)) { if ($renderer->supports($notification)) {
return $renderer; return $renderer;
} }
@ -42,12 +43,12 @@ final class NotificationHandlerManager
throw new NotificationHandlerNotFound(); throw new NotificationHandlerNotFound();
} }
public function getTemplate(Notification $notification) public function getTemplate(Notification $notification): string
{ {
return $this->getHandler($notification)->getTemplate(); return $this->getHandler($notification)->getTemplate();
} }
public function getTemplateData(Notification $notification) public function getTemplateData(Notification $notification): array
{ {
return $this->getHandler($notification)->getTemplateData($notification); return $this->getHandler($notification)->getTemplateData($notification);
} }

View File

@ -4,7 +4,6 @@
<div id="container content"> <div id="container content">
<div class="grid-8 centered"> <div class="grid-8 centered">
<h1>{{ "Notifications list" | trans }}</h1> <h1>{{ "Notifications list" | trans }}</h1>
<!-- TODO : UNREAD & READ -->
{%for data in datas %} {%for data in datas %}
{% set notification = data.notification %} {% set notification = data.notification %}
@ -36,6 +35,8 @@
{% include data.template with data.template_data %} {% include data.template with data.template_data %}
</dd> </dd>
</dl> </dl>
{% else %}
<p class="chill-no-data-statement">{{ notification.Any notification received }}</p>
{% endfor %} {% endfor %}
</div> </div>
</div> </div>

View File

@ -13,4 +13,6 @@ services:
$translator: '@Symfony\Component\Translation\TranslatorInterface' $translator: '@Symfony\Component\Translation\TranslatorInterface'
$routeParameters: '%chill_main.notifications%' $routeParameters: '%chill_main.notifications%'
Chill\MainBundle\Notification\NotificationHandlerManager: ~ Chill\MainBundle\Notification\NotificationHandlerManager:
arguments:
$handlers: !tagged_iterator chill_main.notification_handler