chill-bundles/src/Bundle/ChillMainBundle/Notification/NotificationHandlerManager.php
2022-01-04 19:02:03 +01:00

56 lines
1.5 KiB
PHP

<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\MainBundle\Notification;
use Chill\MainBundle\Entity\Notification;
use Chill\MainBundle\Notification\Exception\NotificationHandlerNotFound;
use Doctrine\ORM\EntityManagerInterface;
final class NotificationHandlerManager
{
private EntityManagerInterface $em;
private iterable $handlers;
public function __construct(
iterable $handlers,
EntityManagerInterface $em
) {
$this->handlers = $handlers;
$this->em = $em;
}
/**
* @throw NotificationHandlerNotFound if handler is not found
*/
public function getHandler(Notification $notification, array $options = []): NotificationHandlerInterface
{
foreach ($this->handlers as $renderer) {
if ($renderer->supports($notification, $options)) {
return $renderer;
}
}
throw new NotificationHandlerNotFound();
}
public function getTemplate(Notification $notification, array $options = []): string
{
return $this->getHandler($notification, $options)->getTemplate($notification, $options);
}
public function getTemplateData(Notification $notification, array $options = []): array
{
return $this->getHandler($notification, $options)->getTemplateData($notification, $options);
}
}