vendor/pimcore/pimcore/models/Tool/TmpStore.php line 103

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\Tool;
  15. use Pimcore\Model;
  16. /**
  17.  * @method bool getById(string $id)
  18.  * @method \Pimcore\Model\Tool\TmpStore\Dao getDao()
  19.  */
  20. final class TmpStore extends Model\AbstractModel
  21. {
  22.     /**
  23.      * @internal
  24.      *
  25.      * @var string
  26.      */
  27.     protected $id;
  28.     /**
  29.      * @internal
  30.      *
  31.      * @var string
  32.      */
  33.     protected $tag;
  34.     /**
  35.      * @internal
  36.      *
  37.      * @var mixed
  38.      */
  39.     protected $data;
  40.     /**
  41.      * @internal
  42.      *
  43.      * @var int
  44.      */
  45.     protected $date;
  46.     /**
  47.      * @internal
  48.      *
  49.      * @var int
  50.      */
  51.     protected $expiryDate;
  52.     /**
  53.      * @internal
  54.      *
  55.      * @var bool
  56.      */
  57.     protected $serialized false;
  58.     /**
  59.      * @internal
  60.      *
  61.      * @var self|null
  62.      */
  63.     protected static ?self $instance null;
  64.     /**
  65.      * @return self
  66.      */
  67.     private static function getInstance(): self
  68.     {
  69.         if (!self::$instance) {
  70.             self::$instance = new self();
  71.         }
  72.         return self::$instance;
  73.     }
  74.     /**
  75.      * @return int
  76.      */
  77.     private static function getDefaultLifetime()
  78.     {
  79.         return 86400 7;
  80.     }
  81.     /**
  82.      * @param string $id
  83.      * @param mixed $data
  84.      * @param string|null $tag
  85.      * @param int|null $lifetime
  86.      *
  87.      * @return bool
  88.      */
  89.     public static function add($id$data$tag null$lifetime null)
  90.     {
  91.         $instance self::getInstance();
  92.         if (!$lifetime) {
  93.             $lifetime self::getDefaultLifetime();
  94.         }
  95.         if (self::get($id)) {
  96.             return true;
  97.         }
  98.         return $instance->getDao()->add($id$data$tag$lifetime);
  99.     }
  100.     /**
  101.      * @param string $id
  102.      * @param mixed $data
  103.      * @param string|null $tag
  104.      * @param int|null $lifetime
  105.      *
  106.      * @return bool
  107.      */
  108.     public static function set($id$data$tag null$lifetime null)
  109.     {
  110.         $instance self::getInstance();
  111.         if (!$lifetime) {
  112.             $lifetime self::getDefaultLifetime();
  113.         }
  114.         return $instance->getDao()->add($id$data$tag$lifetime);
  115.     }
  116.     /**
  117.      * @param string $id
  118.      *
  119.      * @return void
  120.      */
  121.     public static function delete($id)
  122.     {
  123.         $instance self::getInstance();
  124.         $instance->getDao()->delete($id);
  125.     }
  126.     /**
  127.      * @param string $id
  128.      *
  129.      * @return null|TmpStore
  130.      */
  131.     public static function get($id)
  132.     {
  133.         $item = new self();
  134.         if ($item->getById($id)) {
  135.             if ($item->getExpiryDate() < time()) {
  136.                 self::delete($id);
  137.             } else {
  138.                 return $item;
  139.             }
  140.         }
  141.         return null;
  142.     }
  143.     /**
  144.      * @param string $tag
  145.      *
  146.      * @return array
  147.      */
  148.     public static function getIdsByTag($tag)
  149.     {
  150.         $instance self::getInstance();
  151.         $items $instance->getDao()->getIdsByTag($tag);
  152.         return $items;
  153.     }
  154.     /**
  155.      * @return string
  156.      */
  157.     public function getId()
  158.     {
  159.         return $this->id;
  160.     }
  161.     /**
  162.      * @param string $id
  163.      */
  164.     public function setId($id)
  165.     {
  166.         $this->id $id;
  167.     }
  168.     /**
  169.      * @return string
  170.      */
  171.     public function getTag()
  172.     {
  173.         return $this->tag;
  174.     }
  175.     /**
  176.      * @param string $tag
  177.      */
  178.     public function setTag($tag)
  179.     {
  180.         $this->tag $tag;
  181.     }
  182.     /**
  183.      * @return mixed
  184.      */
  185.     public function getData()
  186.     {
  187.         return $this->data;
  188.     }
  189.     /**
  190.      * @param mixed $data
  191.      */
  192.     public function setData($data)
  193.     {
  194.         $this->data $data;
  195.     }
  196.     /**
  197.      * @return int
  198.      */
  199.     public function getDate()
  200.     {
  201.         return $this->date;
  202.     }
  203.     /**
  204.      * @param int $date
  205.      */
  206.     public function setDate($date)
  207.     {
  208.         $this->date $date;
  209.     }
  210.     /**
  211.      * @return bool
  212.      */
  213.     public function isSerialized()
  214.     {
  215.         return $this->serialized;
  216.     }
  217.     /**
  218.      * @param bool $serialized
  219.      */
  220.     public function setSerialized($serialized)
  221.     {
  222.         $this->serialized $serialized;
  223.     }
  224.     /**
  225.      * @return int
  226.      */
  227.     public function getExpiryDate()
  228.     {
  229.         return $this->expiryDate;
  230.     }
  231.     /**
  232.      * @param int $expiryDate
  233.      */
  234.     public function setExpiryDate($expiryDate)
  235.     {
  236.         $this->expiryDate $expiryDate;
  237.     }
  238.     /**
  239.      * @param int|null $lifetime
  240.      *
  241.      * @return bool
  242.      */
  243.     public function update($lifetime null)
  244.     {
  245.         if (!$lifetime) {
  246.             $lifetime 86400;
  247.         }
  248.         return $this->getDao()->add($this->getId(), $this->getData(), $this->getTag(), $lifetime);
  249.     }
  250. }