<?php
namespace Products\NotificationsBundle\Service\Notifications;
use App\Model\Query\ConditionQuery\ConditionConfig;
use App\Service\TenantContext;
use Cms\CoreBundle\Util\Doctrine\EntityManager;
use Cms\TenantBundle\Model\SimpleTenantableInterface;
use Products\NotificationsBundle\Entity\NotificationsConfig;
final class NotificationsConfigService
{
// DI
protected EntityManager $em;
protected TenantContext $tc;
/**
* @param EntityManager $em
* @param TenantContext $tc
*/
public function __construct(
EntityManager $em,
TenantContext $tc
)
{
$this->em = $em;
$this->tc = $tc;
}
/**
* @param SimpleTenantableInterface|null $tenantable
* @return NotificationsConfig|null
*/
public function getNotificationsConfig(?SimpleTenantableInterface $tenantable = null): ?NotificationsConfig
{
return $this->em->getRepository(NotificationsConfig::class)->findForTenant(
$this->tc->fallback(
$tenantable,
),
);
}
/**
* @param SimpleTenantableInterface $tenantable
* @param ConditionConfig $baseConfig
* @return NotificationsConfig
*/
public function saveNotificationsBaseConfig(
SimpleTenantableInterface $tenantable,
ConditionConfig $baseConfig
): NotificationsConfig
{
$notificationsConfig = $this->getNotificationsConfig($tenantable);
if ( ! $notificationsConfig instanceof NotificationsConfig) {
$notificationsConfig = (new NotificationsConfig())
->setTenant($tenantable->getTenant());
}
$this->em->save(
$notificationsConfig->setBaseConfig(
$baseConfig,
),
);
return $notificationsConfig;
}
}