src/Products/NotificationsBundle/Entity/NotificationsConfig.php line 22

Open in your IDE?
  1. <?php
  2. namespace Products\NotificationsBundle\Entity;
  3. use App\Util\Locales;
  4. use Cms\TenantBundle\Entity\TenantedEntity;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use App\Model\Query\ConditionQuery\ConditionConfig;
  7. use Symfony\Component\Yaml\Yaml;
  8. /**
  9.  * Class NotificationsConfig
  10.  * @package Products\NotificationsBundle\Entity
  11.  *
  12.  * @ORM\Entity(
  13.  *     repositoryClass = "Products\NotificationsBundle\Doctrine\Repository\Notifications\NotificationsConfigRepository",
  14.  * )
  15.  * @ORM\Table(
  16.  *     name = "notis__notification_config",
  17.  * )
  18.  */
  19. class NotificationsConfig extends TenantedEntity
  20. {
  21.     /**
  22.      * @var string|null
  23.      *
  24.      * @ORM\Column(
  25.      *     type = "text",
  26.      *     nullable = true,
  27.      * )
  28.      */
  29.     protected ?string $yaml null;
  30.     /**
  31.      * This is the customer-editable configuration that is avaible in the UIs to manage.
  32.      *
  33.      * @var ConditionConfig|null
  34.      *
  35.      * @ORM\Column(
  36.      *     type = "condition_config",
  37.      *     nullable = true,
  38.      * )
  39.      */
  40.     protected ?ConditionConfig $customConfig null;
  41.     /**
  42.      * This is the system-generated config that is only managed through code and is not able to be modified by any UI.
  43.      *
  44.      * @var ConditionConfig|null
  45.      *
  46.      * @ORM\Column(
  47.      *     type = "condition_config",
  48.      *     nullable = true,
  49.      * )
  50.      */
  51.     protected ?ConditionConfig $baseConfig null;
  52.     /**
  53.      * @var array $translationLocales
  54.      *
  55.      * @ORM\Column(
  56.      *     type = "simple_array",
  57.      *     nullable = true,
  58.      * )
  59.      */
  60.     protected array $translationLocales = [];
  61.     /**
  62.      * @return ConditionConfig
  63.      */
  64.     protected ?ConditionConfig $config null;
  65.     /**
  66.      * @return string|null
  67.      */
  68.     public function getYaml(): ?string
  69.     {
  70.         return $this->yaml;
  71.     }
  72.     /**
  73.      * @param string|null $yaml
  74.      * @return $this
  75.      */
  76.     public function setYaml(?string $yaml): self
  77.     {
  78.         // set the yaml property
  79.         $this->yaml $yaml ?: null;
  80.         // generate the config for the yaml if it is set to something
  81.         $this->customConfig $yaml ConditionConfig::factory(Yaml::parse($yaml)) : null;
  82.         // need to clear the base config as it has been changed
  83.         $this->config null;
  84.         return $this;
  85.     }
  86.     /**
  87.      * @return ConditionConfig|null
  88.      */
  89.     public function getCustomConfig(): ?ConditionConfig
  90.     {
  91.         return $this->customConfig;
  92.     }
  93.     /**
  94.      * @return string
  95.      */
  96.     public function getCustomConfigYaml(): string
  97.     {
  98.         return $this->getYaml() ?: '';
  99.     }
  100.     /**
  101.      * @return ConditionConfig|null
  102.      */
  103.     public function getBaseConfig(): ?ConditionConfig
  104.     {
  105.         return $this->baseConfig;
  106.     }
  107.     /**
  108.      * @return string
  109.      */
  110.     public function getBaseConfigYaml(): string
  111.     {
  112.         return $this->getBaseConfig() ? Yaml::dump(
  113.             $this->getBaseConfig()->jsonSerialize(),
  114.             10,
  115.             4,
  116.         ) : '';
  117.     }
  118.     /**
  119.      * @param ConditionConfig|null $baseConfig
  120.      * @return $this
  121.      */
  122.     public function setBaseConfig(?ConditionConfig $baseConfig): self
  123.     {
  124.         // set the property
  125.         $this->baseConfig $baseConfig;
  126.         // need to clear the base config as it has been changed
  127.         $this->config null;
  128.         return $this;
  129.     }
  130.     /**
  131.      * This will generate the "effective" or "final" configuration for a customer.
  132.      * Basically takes the custom and the base config and merges them together.
  133.      * The items defined in the base config will override any matching items (like a schema or dictionary) in the custom config.
  134.      *
  135.      * Note that when overriding, for example a dictionary, the merge will not merge the contents of two conflicting dictionaries together.
  136.      * The merge will simply replace the conflicting dictionary entirely with the one being merged in.
  137.      *
  138.      * NOTE: This is generally the method that you want to use when actually needing to utilize a ConditionConfig model to influence a UI, running a query, etc.
  139.      *
  140.      * @return ConditionConfig
  141.      */
  142.     public function getConfig(): ConditionConfig
  143.     {
  144.         if ( ! $this->config) {
  145.             $this->config = new ConditionConfig();
  146.             if ($this->getCustomConfig()) {
  147.                 $this->config->merge($this->getCustomConfig());
  148.             }
  149.             if ($this->getBaseConfig()) {
  150.                 $this->config->merge($this->getBaseConfig());
  151.             }
  152.         }
  153.         return $this->config;
  154.     }
  155.     /**
  156.      * @return string
  157.      */
  158.     public function getConfigYaml(): string
  159.     {
  160.         return Yaml::dump(
  161.             $this->getConfig()->jsonSerialize(),
  162.             10,
  163.             4,
  164.         );
  165.     }
  166.     /**
  167.      * @return array<string>
  168.      */
  169.     public function getTranslationLocales(): array
  170.     {
  171.         return array_filter($this->translationLocales);
  172.     }
  173.     /**
  174.      * @return array
  175.      */
  176.     public function getTranslationLanguages(): array
  177.     {
  178.         $languages = [];
  179.         foreach ($this->getTranslationLocales() as $translationLocale) {
  180.             $languages[] = Locales::CONVERSIONS[$translationLocale];
  181.         }
  182.         return $languages;
  183.     }
  184.     /**
  185.      * @return array
  186.      */
  187.     public function getTranslationLanguagesHumanReadable(): array
  188.     {
  189.         $languages = [];
  190.         foreach ($this->getTranslationLanguages() as $translationLanguage) {
  191.             $languages[$translationLanguage] = Locales::ISO6391_HUMAN_READABLE[$translationLanguage];
  192.         }
  193.         return $languages;
  194.     }
  195.     /**
  196.      * @return array
  197.      */
  198.     public function getTranslationLocalesHumanReadable(): array
  199.     {
  200.         $locales = [];
  201.         foreach ($this->getTranslationLocales() as $translationLocale) {
  202.             $locales[$translationLocale] = Locales::RFC4646_HUMAN_READABLE[$translationLocale];
  203.         }
  204.         return $locales;
  205.     }
  206.     /**
  207.      * @param array<string> $locales
  208.      * @return $this
  209.      */
  210.     public function setTranslationLocales(array $locales): self
  211.     {
  212.         $this->translationLocales = [];
  213.         foreach ($locales as $locale) {
  214.             $this->addTranslationLocale($localefalse);
  215.         }
  216.         sort($this->translationLocales);
  217.         return $this;
  218.     }
  219.     /**
  220.      * @param string $locale
  221.      * @param bool $sort
  222.      * @return $this
  223.      */
  224.     public function addTranslationLocale(string $localebool $sort true): self
  225.     {
  226.         // make sure we have a legit locale
  227.         if ( ! Locales::isLocale($locale)) {
  228.             throw new \RuntimeException(
  229.                 sprintf(
  230.                     'Locale "%s" is not supported, must be one of: %s',
  231.                     $locale,
  232.                     implode(', 'Locales::RFC4646),
  233.                 ),
  234.             );
  235.         }
  236.         // only add it if it is not english (our default/base locale)
  237.         // and if it is not already in our set
  238.         if ($locale !== Locales::RFC4646_DEFAULT && ! in_array($locale$this->translationLocalestrue)) {
  239.             $this->translationLocales[] = $locale;
  240.         }
  241.         // handle sorting
  242.         if ($sort) {
  243.             sort($this->translationLocales);
  244.         }
  245.         return $this;
  246.     }
  247.     /**
  248.      * @param string $locale
  249.      * @param bool $sort
  250.      * @return $this
  251.      */
  252.     public function removeTranslationLocale(string $localebool $sort true): self
  253.     {
  254.         $index array_search($locale$this->translationLocalestrue);
  255.         if ($index !== false) {
  256.             unset($this->translationLocales[$index]);
  257.             if ( ! $sort) {
  258.                 $this->translationLocales array_values($this->translationLocales);
  259.             }
  260.         }
  261.         if ($sort) {
  262.             sort($this->translationLocales);
  263.         }
  264.         return $this;
  265.     }
  266. }