vendor/pimcore/pimcore/models/DataObject/Classificationstore/KeyConfig.php line 28

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Commercial License (PCL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  13.  */
  14. namespace Pimcore\Model\DataObject\Classificationstore;
  15. use Pimcore\Cache;
  16. use Pimcore\Cache\RuntimeCache;
  17. use Pimcore\Event\DataObjectClassificationStoreEvents;
  18. use Pimcore\Event\Model\DataObject\ClassificationStore\KeyConfigEvent;
  19. use Pimcore\Event\Traits\RecursionBlockingEventDispatchHelperTrait;
  20. use Pimcore\Model;
  21. /**
  22.  * @method \Pimcore\Model\DataObject\Classificationstore\KeyConfig\Dao getDao()
  23.  */
  24. final class KeyConfig extends Model\AbstractModel
  25. {
  26.     use RecursionBlockingEventDispatchHelperTrait;
  27.     /**
  28.      * @var int|null
  29.      */
  30.     protected $id;
  31.     /**
  32.      * Store ID
  33.      *
  34.      * @var int
  35.      */
  36.     protected $storeId 1;
  37.     /** The key
  38.      * @var string
  39.      */
  40.     protected $name;
  41.     /** Pseudo column for title
  42.      * @var string|null
  43.      */
  44.     protected $title;
  45.     /**
  46.      * The key description.
  47.      *
  48.      * @var string
  49.      */
  50.     protected $description;
  51.     /**
  52.      * The key type ("text", "number", etc...)
  53.      *
  54.      * @var string
  55.      */
  56.     protected $type;
  57.     /**
  58.      * @var int|null
  59.      */
  60.     protected $creationDate;
  61.     /**
  62.      * @var int|null
  63.      */
  64.     protected $modificationDate;
  65.     /**
  66.      * @var string
  67.      */
  68.     protected $definition;
  69.     /** @var bool */
  70.     protected $enabled;
  71.     /**
  72.      * @param int $id
  73.      * @param null|bool $force
  74.      *
  75.      * @return self|null
  76.      */
  77.     public static function getById($id, ?bool $force false)
  78.     {
  79.         $id = (int)$id;
  80.         $cacheKey self::getCacheKey($id);
  81.         try {
  82.             if (!$force && Cache\RuntimeCache::isRegistered($cacheKey)) {
  83.                 return Cache\RuntimeCache::get($cacheKey);
  84.             }
  85.             if (!$force && $config Cache::load($cacheKey)) {
  86.                 Cache\RuntimeCache::set($cacheKey$config);
  87.                 return $config;
  88.             }
  89.             $config = new self();
  90.             $config->getDao()->getById($id);
  91.             Cache\RuntimeCache::set($cacheKey$config);
  92.             Cache::save($config$cacheKey);
  93.             return $config;
  94.         } catch (Model\Exception\NotFoundException $e) {
  95.             return null;
  96.         }
  97.     }
  98.     /**
  99.      * @param string $name
  100.      * @param int $storeId
  101.      * @param bool $force
  102.      *
  103.      * @return self|null
  104.      *
  105.      * @throws \Exception
  106.      */
  107.     public static function getByName($name$storeId 1, ?bool $force false)
  108.     {
  109.         $cacheKey self::getCacheKey($storeId$name);
  110.         try {
  111.             if (!$force && Cache\RuntimeCache::isRegistered($cacheKey)) {
  112.                 return Cache\RuntimeCache::get($cacheKey);
  113.             }
  114.             if (!$force && ($config Cache::load($cacheKey))) {
  115.                 Cache\RuntimeCache::set($cacheKey$config);
  116.                 return $config;
  117.             }
  118.             $config = new self();
  119.             $config->setName($name);
  120.             $config->setStoreId($storeId $storeId 1);
  121.             $config->getDao()->getByName();
  122.             Cache\RuntimeCache::set($cacheKey$config);
  123.             Cache::save($config$cacheKey);
  124.             return $config;
  125.         } catch (Model\Exception\NotFoundException $e) {
  126.             return null;
  127.         }
  128.     }
  129.     /**
  130.      * @return Model\DataObject\Classificationstore\KeyConfig
  131.      */
  132.     public static function create()
  133.     {
  134.         $config = new self();
  135.         $config->save();
  136.         return $config;
  137.     }
  138.     /**
  139.      * @param int $id
  140.      *
  141.      * @return $this
  142.      */
  143.     public function setId($id)
  144.     {
  145.         $this->id = (int) $id;
  146.         return $this;
  147.     }
  148.     /**
  149.      * @return int|null
  150.      */
  151.     public function getId()
  152.     {
  153.         return $this->id;
  154.     }
  155.     /**
  156.      * @param string $name
  157.      *
  158.      * @return $this
  159.      */
  160.     public function setName($name)
  161.     {
  162.         $this->name $name;
  163.         return $this;
  164.     }
  165.     /**
  166.      * @return string
  167.      */
  168.     public function getName()
  169.     {
  170.         return $this->name;
  171.     }
  172.     /**
  173.      * Returns the description.
  174.      *
  175.      * @return string
  176.      */
  177.     public function getDescription()
  178.     {
  179.         return $this->description;
  180.     }
  181.     /**
  182.      * Sets the description.
  183.      *
  184.      * @param string $description
  185.      *
  186.      * @return Model\DataObject\Classificationstore\KeyConfig
  187.      */
  188.     public function setDescription($description)
  189.     {
  190.         $this->description $description;
  191.         return $this;
  192.     }
  193.     /**
  194.      * Deletes the key value key configuration
  195.      */
  196.     public function delete()
  197.     {
  198.         DefinitionCache::clear($this);
  199.         $this->dispatchEvent(new KeyConfigEvent($this), DataObjectClassificationStoreEvents::KEY_CONFIG_PRE_DELETE);
  200.         if ($this->getId()) {
  201.             self::removeCache();
  202.         }
  203.         $this->getDao()->delete();
  204.         $this->dispatchEvent(new KeyConfigEvent($this), DataObjectClassificationStoreEvents::KEY_CONFIG_POST_DELETE);
  205.     }
  206.     /**
  207.      * Saves the key config
  208.      */
  209.     public function save()
  210.     {
  211.         DefinitionCache::clear($this);
  212.         $isUpdate false;
  213.         $def json_decode($this->definitiontrue);
  214.         if ($def && isset($def['title'])) {
  215.             $this->title $def['title'];
  216.         } else {
  217.             $this->title null;
  218.         }
  219.         if ($this->getId()) {
  220.             self::removeCache();
  221.             $isUpdate true;
  222.             $this->dispatchEvent(new KeyConfigEvent($this), DataObjectClassificationStoreEvents::KEY_CONFIG_PRE_UPDATE);
  223.         } else {
  224.             $this->dispatchEvent(new KeyConfigEvent($this), DataObjectClassificationStoreEvents::KEY_CONFIG_PRE_ADD);
  225.         }
  226.         $this->getDao()->save();
  227.         if ($isUpdate) {
  228.             $this->dispatchEvent(new KeyConfigEvent($this), DataObjectClassificationStoreEvents::KEY_CONFIG_POST_UPDATE);
  229.         } else {
  230.             $this->dispatchEvent(new KeyConfigEvent($this), DataObjectClassificationStoreEvents::KEY_CONFIG_POST_ADD);
  231.         }
  232.     }
  233.     /**
  234.      * @return int|null
  235.      */
  236.     public function getCreationDate()
  237.     {
  238.         return $this->creationDate;
  239.     }
  240.     /**
  241.      * @param int $creationDate
  242.      */
  243.     public function setCreationDate($creationDate)
  244.     {
  245.         $this->creationDate $creationDate;
  246.     }
  247.     /**
  248.      * @return int|null
  249.      */
  250.     public function getModificationDate()
  251.     {
  252.         return $this->modificationDate;
  253.     }
  254.     /**
  255.      * @param int $modificationDate
  256.      */
  257.     public function setModificationDate($modificationDate)
  258.     {
  259.         $this->modificationDate $modificationDate;
  260.     }
  261.     /**
  262.      * @return string
  263.      */
  264.     public function getType()
  265.     {
  266.         return $this->type;
  267.     }
  268.     /**
  269.      * @param string $type
  270.      */
  271.     public function setType($type)
  272.     {
  273.         $this->type $type;
  274.     }
  275.     /**
  276.      * @return string
  277.      */
  278.     public function getDefinition()
  279.     {
  280.         return $this->definition;
  281.     }
  282.     /**
  283.      * @param string $definition
  284.      */
  285.     public function setDefinition($definition)
  286.     {
  287.         $this->definition $definition;
  288.     }
  289.     /**
  290.      * @return bool
  291.      */
  292.     public function getEnabled()
  293.     {
  294.         return $this->enabled;
  295.     }
  296.     /**
  297.      * @param bool $enabled
  298.      */
  299.     public function setEnabled($enabled)
  300.     {
  301.         $this->enabled $enabled;
  302.     }
  303.     /**
  304.      * @return string
  305.      */
  306.     public function getTitle()
  307.     {
  308.         return $this->title;
  309.     }
  310.     /**
  311.      * @param string $title
  312.      */
  313.     public function setTitle($title)
  314.     {
  315.         $this->title $title;
  316.     }
  317.     /**
  318.      * @return int
  319.      */
  320.     public function getStoreId()
  321.     {
  322.         return $this->storeId;
  323.     }
  324.     /**
  325.      * @param int $storeId
  326.      */
  327.     public function setStoreId($storeId)
  328.     {
  329.         $this->storeId $storeId;
  330.     }
  331.     /**
  332.      * Calculate cache key
  333.      *
  334.      * @param int $id
  335.      * @param string|null $name
  336.      *
  337.      * @return string
  338.      */
  339.     private static function getCacheKey(int $idstring $name null): string
  340.     {
  341.         $cacheKey 'cs_keyconfig_' $id;
  342.         if ($name !== null) {
  343.             $cacheKey .= '_' md5($name);
  344.         }
  345.         return $cacheKey;
  346.     }
  347.     /**
  348.      * @internal
  349.      */
  350.     private function removeCache(): void
  351.     {
  352.         // Remove runtime cache
  353.         if (RuntimeCache::getInstance()->offsetExists(self::getCacheKey($this->getId()))) {
  354.             RuntimeCache::getInstance()->offsetUnset(self::getCacheKey($this->getId()));
  355.         }
  356.         if (RuntimeCache::getInstance()->offsetExists(self::getCacheKey($this->getStoreId(), $this->getName()))) {
  357.             RuntimeCache::getInstance()->offsetUnset(self::getCacheKey($this->getStoreId(), $this->getName()));
  358.         }
  359.         // Remove persisted cache
  360.         Cache::remove(self::getCacheKey($this->getId()));
  361.         Cache::remove(self::getCacheKey($this->getStoreId(), $this->getName()));
  362.     }
  363. }