src/Products/NotificationsBundle/Service/Notifications/NotificationsConfigService.php line 34

Open in your IDE?
  1. <?php
  2. namespace Products\NotificationsBundle\Service\Notifications;
  3. use App\Model\Query\ConditionQuery\ConditionConfig;
  4. use App\Service\TenantContext;
  5. use Cms\CoreBundle\Util\Doctrine\EntityManager;
  6. use Cms\TenantBundle\Model\SimpleTenantableInterface;
  7. use Products\NotificationsBundle\Entity\NotificationsConfig;
  8. final class NotificationsConfigService
  9. {
  10.     // DI
  11.     protected EntityManager $em;
  12.     protected TenantContext $tc;
  13.     /**
  14.      * @param EntityManager $em
  15.      * @param TenantContext $tc
  16.      */
  17.     public function __construct(
  18.         EntityManager $em,
  19.         TenantContext $tc
  20.     )
  21.     {
  22.         $this->em $em;
  23.         $this->tc $tc;
  24.     }
  25.     /**
  26.      * @param SimpleTenantableInterface|null $tenantable
  27.      * @return NotificationsConfig|null
  28.      */
  29.     public function getNotificationsConfig(?SimpleTenantableInterface $tenantable null): ?NotificationsConfig
  30.     {
  31.         return $this->em->getRepository(NotificationsConfig::class)->findForTenant(
  32.             $this->tc->fallback(
  33.                 $tenantable,
  34.             ),
  35.         );
  36.     }
  37.     /**
  38.      * @param SimpleTenantableInterface $tenantable
  39.      * @param ConditionConfig $baseConfig
  40.      * @return NotificationsConfig
  41.      */
  42.     public function saveNotificationsBaseConfig(
  43.         SimpleTenantableInterface $tenantable,
  44.         ConditionConfig $baseConfig
  45.     ): NotificationsConfig
  46.     {
  47.         $notificationsConfig $this->getNotificationsConfig($tenantable);
  48.         if ( ! $notificationsConfig instanceof NotificationsConfig) {
  49.             $notificationsConfig = (new NotificationsConfig())
  50.                 ->setTenant($tenantable->getTenant());
  51.         }
  52.         $this->em->save(
  53.             $notificationsConfig->setBaseConfig(
  54.                 $baseConfig,
  55.             ),
  56.         );
  57.         return $notificationsConfig;
  58.     }
  59. }