vendor/pimcore/pimcore/models/DataObject/Classificationstore/CollectionConfig.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\CollectionConfigEvent;
  19. use Pimcore\Event\Traits\RecursionBlockingEventDispatchHelperTrait;
  20. use Pimcore\Model;
  21. /**
  22.  * @method \Pimcore\Model\DataObject\Classificationstore\CollectionConfig\Dao getDao()
  23.  */
  24. final class CollectionConfig 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.     /**
  38.      * @var string
  39.      */
  40.     protected $name;
  41.     /**
  42.      * The collection description.
  43.      *
  44.      * @var string
  45.      */
  46.     protected $description;
  47.     /**
  48.      * @var int|null
  49.      */
  50.     protected $creationDate;
  51.     /**
  52.      * @var int|null
  53.      */
  54.     protected $modificationDate;
  55.     /**
  56.      * @param int $id
  57.      * @param bool|null $force
  58.      *
  59.      * @return self|null
  60.      */
  61.     public static function getById($id, ?bool $force false)
  62.     {
  63.         $id = (int)$id;
  64.         $cacheKey self::getCacheKey($id);
  65.         try {
  66.             if (!$force && RuntimeCache::isRegistered($cacheKey)) {
  67.                 return RuntimeCache::get($cacheKey);
  68.             }
  69.             if (!$force && $config Cache::load($cacheKey)) {
  70.                 RuntimeCache::set($cacheKey$config);
  71.                 return $config;
  72.             }
  73.             $config = new self();
  74.             $config->getDao()->getById($id);
  75.             RuntimeCache::set($cacheKey$config);
  76.             Cache::save($config$cacheKey);
  77.             return $config;
  78.         } catch (Model\Exception\NotFoundException $e) {
  79.             return null;
  80.         }
  81.     }
  82.     /**
  83.      * @param string $name
  84.      * @param int $storeId
  85.      * @param bool|null $force
  86.      *
  87.      * @return self|null
  88.      *
  89.      * @throws \Exception
  90.      */
  91.     public static function getByName($name$storeId 1, ?bool $force false)
  92.     {
  93.         $cacheKey self::getCacheKey($storeId$name);
  94.         try {
  95.             if (!$force && RuntimeCache::isRegistered($cacheKey)) {
  96.                 return RuntimeCache::get($cacheKey);
  97.             }
  98.             if (!$force && $config Cache::load($cacheKey)) {
  99.                 RuntimeCache::set($cacheKey$config);
  100.                 return $config;
  101.             }
  102.             $config = new self();
  103.             $config->setName($name);
  104.             $config->setStoreId($storeId $storeId 1);
  105.             $config->getDao()->getByName();
  106.             RuntimeCache::set($cacheKey$config);
  107.             Cache::save($config$cacheKey);
  108.             return $config;
  109.         } catch (Model\Exception\NotFoundException $e) {
  110.             return null;
  111.         }
  112.     }
  113.     /**
  114.      * @return Model\DataObject\Classificationstore\CollectionConfig
  115.      */
  116.     public static function create()
  117.     {
  118.         $config = new self();
  119.         $config->save();
  120.         return $config;
  121.     }
  122.     /**
  123.      * @param int $id
  124.      *
  125.      * @return $this
  126.      */
  127.     public function setId($id)
  128.     {
  129.         $this->id = (int) $id;
  130.         return $this;
  131.     }
  132.     /**
  133.      * @return int|null
  134.      */
  135.     public function getId()
  136.     {
  137.         return $this->id;
  138.     }
  139.     /**
  140.      * @param string $name
  141.      *
  142.      * @return $this
  143.      */
  144.     public function setName($name)
  145.     {
  146.         $this->name $name;
  147.         return $this;
  148.     }
  149.     /**
  150.      * @return string
  151.      */
  152.     public function getName()
  153.     {
  154.         return $this->name;
  155.     }
  156.     /**
  157.      * Returns the description.
  158.      *
  159.      * @return string
  160.      */
  161.     public function getDescription()
  162.     {
  163.         return $this->description;
  164.     }
  165.     /**
  166.      * Sets the description.
  167.      *
  168.      * @param string $description
  169.      *
  170.      * @return Model\DataObject\Classificationstore\CollectionConfig
  171.      */
  172.     public function setDescription($description)
  173.     {
  174.         $this->description $description;
  175.         return $this;
  176.     }
  177.     /**
  178.      * Deletes the key value collection configuration
  179.      */
  180.     public function delete()
  181.     {
  182.         $this->dispatchEvent(new CollectionConfigEvent($this), DataObjectClassificationStoreEvents::COLLECTION_CONFIG_PRE_DELETE);
  183.         if ($this->getId()) {
  184.             $this->removeCache();
  185.         }
  186.         $this->getDao()->delete();
  187.         $this->dispatchEvent(new CollectionConfigEvent($this), DataObjectClassificationStoreEvents::COLLECTION_CONFIG_POST_DELETE);
  188.     }
  189.     /**
  190.      * Saves the collection config
  191.      */
  192.     public function save()
  193.     {
  194.         $isUpdate false;
  195.         if ($this->getId()) {
  196.             $this->removeCache();
  197.             $isUpdate true;
  198.             $this->dispatchEvent(new CollectionConfigEvent($this), DataObjectClassificationStoreEvents::COLLECTION_CONFIG_PRE_UPDATE);
  199.         } else {
  200.             $this->dispatchEvent(new CollectionConfigEvent($this), DataObjectClassificationStoreEvents::COLLECTION_CONFIG_PRE_ADD);
  201.         }
  202.         $model $this->getDao()->save();
  203.         if ($isUpdate) {
  204.             $this->dispatchEvent(new CollectionConfigEvent($this), DataObjectClassificationStoreEvents::COLLECTION_CONFIG_POST_UPDATE);
  205.         } else {
  206.             $this->dispatchEvent(new CollectionConfigEvent($this), DataObjectClassificationStoreEvents::COLLECTION_CONFIG_POST_ADD);
  207.         }
  208.         return $model;
  209.     }
  210.     /**
  211.      * @param int $modificationDate
  212.      *
  213.      * @return $this
  214.      */
  215.     public function setModificationDate($modificationDate)
  216.     {
  217.         $this->modificationDate = (int) $modificationDate;
  218.         return $this;
  219.     }
  220.     /**
  221.      * @return int|null
  222.      */
  223.     public function getModificationDate()
  224.     {
  225.         return $this->modificationDate;
  226.     }
  227.     /**
  228.      * @param int $creationDate
  229.      *
  230.      * @return $this
  231.      */
  232.     public function setCreationDate($creationDate)
  233.     {
  234.         $this->creationDate = (int) $creationDate;
  235.         return $this;
  236.     }
  237.     /**
  238.      * @return int|null
  239.      */
  240.     public function getCreationDate()
  241.     {
  242.         return $this->creationDate;
  243.     }
  244.     /**
  245.      * Returns all groups belonging to this collection
  246.      *
  247.      * @return CollectionGroupRelation[]
  248.      */
  249.     public function getRelations()
  250.     {
  251.         $list = new CollectionGroupRelation\Listing();
  252.         $list->setCondition('colId = ' $this->id);
  253.         $list $list->load();
  254.         return $list;
  255.     }
  256.     /**
  257.      * @return int
  258.      */
  259.     public function getStoreId()
  260.     {
  261.         return $this->storeId;
  262.     }
  263.     /**
  264.      * @param int $storeId
  265.      */
  266.     public function setStoreId($storeId)
  267.     {
  268.         $this->storeId $storeId;
  269.     }
  270.     /**
  271.      * Calculate cache key
  272.      *
  273.      * @param int $id
  274.      * @param string|null $name
  275.      *
  276.      * @return string
  277.      */
  278.     private static function getCacheKey(int $idstring $name null): string
  279.     {
  280.         $cacheKey 'cs_collectionconfig_' $id;
  281.         if ($name !== null) {
  282.             $cacheKey .= '_' md5($name);
  283.         }
  284.         return $cacheKey;
  285.     }
  286.     /**
  287.      * @internal
  288.      */
  289.     private function removeCache(): void
  290.     {
  291.         // Remove runtime cache
  292.         RuntimeCache::set(self::getCacheKey($this->getId()), null);
  293.         RuntimeCache::set(self::getCacheKey($this->getStoreId(), $this->getName()), null);
  294.         // Remove persisted cache
  295.         Cache::remove(self::getCacheKey($this->getId()));
  296.         Cache::remove(self::getCacheKey($this->getStoreId(), $this->getName()));
  297.     }
  298. }