vendor/pimcore/pimcore/models/Redirect.php line 162

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;
  15. use Pimcore\Config;
  16. use Pimcore\Event\Model\RedirectEvent;
  17. use Pimcore\Event\RedirectEvents;
  18. use Pimcore\Event\Traits\RecursionBlockingEventDispatchHelperTrait;
  19. use Pimcore\Logger;
  20. use Pimcore\Model\Exception\NotFoundException;
  21. use Symfony\Component\HttpFoundation\Request;
  22. /**
  23.  * @method \Pimcore\Model\Redirect\Dao getDao()
  24.  */
  25. final class Redirect extends AbstractModel
  26. {
  27.     use RecursionBlockingEventDispatchHelperTrait;
  28.     const TYPE_ENTIRE_URI 'entire_uri';
  29.     const TYPE_PATH_QUERY 'path_query';
  30.     const TYPE_PATH 'path';
  31.     const TYPE_AUTO_CREATE 'auto_create';
  32.     const TYPES = [
  33.         self::TYPE_ENTIRE_URI,
  34.         self::TYPE_PATH_QUERY,
  35.         self::TYPE_PATH,
  36.         self::TYPE_AUTO_CREATE,
  37.     ];
  38.     /**
  39.      * @var int|null
  40.      */
  41.     protected $id;
  42.     /**
  43.      * @var string
  44.      */
  45.     protected $type;
  46.     /**
  47.      * @var string|null
  48.      */
  49.     protected $source;
  50.     /**
  51.      * @var int|null
  52.      */
  53.     protected $sourceSite;
  54.     /**
  55.      * @var bool
  56.      */
  57.     protected $passThroughParameters false;
  58.     /**
  59.      * @var string|null
  60.      */
  61.     protected $target;
  62.     /**
  63.      * @var int|null
  64.      */
  65.     protected $targetSite;
  66.     /**
  67.      * @var int
  68.      */
  69.     protected $statusCode 301;
  70.     /**
  71.      * @var int
  72.      */
  73.     protected $priority 1;
  74.     /**
  75.      * @var bool|null
  76.      */
  77.     protected $regex;
  78.     /**
  79.      * @var bool
  80.      */
  81.     protected $active true;
  82.     /**
  83.      * @var int|null
  84.      */
  85.     protected $expiry;
  86.     /**
  87.      * @var int|null
  88.      */
  89.     protected $creationDate;
  90.     /**
  91.      * @var int|null
  92.      */
  93.     protected $modificationDate;
  94.     /**
  95.      * ID of the owner user
  96.      *
  97.      * @var int|null
  98.      */
  99.     protected ?int $userOwner null;
  100.     /**
  101.      * ID of the user who make the latest changes
  102.      *
  103.      * @var int|null
  104.      */
  105.     protected $userModification;
  106.     /**
  107.      * @param int $id
  108.      *
  109.      * @return self|null
  110.      */
  111.     public static function getById($id)
  112.     {
  113.         try {
  114.             $redirect = new self();
  115.             $redirect->getDao()->getById($id);
  116.             return $redirect;
  117.         } catch (NotFoundException $e) {
  118.             return null;
  119.         }
  120.     }
  121.     /**
  122.      * @internal
  123.      *
  124.      * @param Request $request
  125.      * @param Site|null $site
  126.      * @param bool $override
  127.      *
  128.      * @return self|null
  129.      */
  130.     public static function getByExactMatch(Request $request, ?Site $site nullbool $override false): ?self
  131.     {
  132.         try {
  133.             $redirect = new self();
  134.             $redirect->getDao()->getByExactMatch($request$site$override);
  135.             return $redirect;
  136.         } catch (NotFoundException $e) {
  137.             return null;
  138.         }
  139.     }
  140.     /**
  141.      * @return Redirect
  142.      */
  143.     public static function create()
  144.     {
  145.         $redirect = new self();
  146.         $redirect->save();
  147.         return $redirect;
  148.     }
  149.     /**
  150.      * @return int|null
  151.      */
  152.     public function getId()
  153.     {
  154.         return $this->id;
  155.     }
  156.     /**
  157.      * @return string|null
  158.      */
  159.     public function getSource()
  160.     {
  161.         return $this->source;
  162.     }
  163.     /**
  164.      * @return string|null
  165.      */
  166.     public function getTarget()
  167.     {
  168.         return $this->target;
  169.     }
  170.     /**
  171.      * @param int $id
  172.      *
  173.      * @return $this
  174.      */
  175.     public function setId($id)
  176.     {
  177.         $this->id = (int) $id;
  178.         return $this;
  179.     }
  180.     /**
  181.      * enum('entire_uri','path_query','path','auto_create')
  182.      *
  183.      * @return string
  184.      */
  185.     public function getType()
  186.     {
  187.         return $this->type;
  188.     }
  189.     /**
  190.      * enum('entire_uri','path_query','path','auto_create')
  191.      *
  192.      * @param string $type
  193.      */
  194.     public function setType($type)
  195.     {
  196.         if (!empty($type) && !in_array($typeself::TYPES)) {
  197.             throw new \InvalidArgumentException(sprintf('Invalid type "%s"'$type));
  198.         }
  199.         $this->type $type;
  200.     }
  201.     /**
  202.      * @param string $source
  203.      *
  204.      * @return $this
  205.      */
  206.     public function setSource($source)
  207.     {
  208.         $this->source $source;
  209.         return $this;
  210.     }
  211.     /**
  212.      * @param string $target
  213.      *
  214.      * @return $this
  215.      */
  216.     public function setTarget($target)
  217.     {
  218.         $this->target $target;
  219.         return $this;
  220.     }
  221.     /**
  222.      * @param int $priority
  223.      *
  224.      * @return $this
  225.      */
  226.     public function setPriority($priority)
  227.     {
  228.         if ($priority) {
  229.             $this->priority $priority;
  230.         }
  231.         return $this;
  232.     }
  233.     /**
  234.      * @return int
  235.      */
  236.     public function getPriority()
  237.     {
  238.         return $this->priority;
  239.     }
  240.     /**
  241.      * @param int $statusCode
  242.      *
  243.      * @return $this
  244.      */
  245.     public function setStatusCode($statusCode)
  246.     {
  247.         if ($statusCode) {
  248.             $this->statusCode $statusCode;
  249.         }
  250.         return $this;
  251.     }
  252.     /**
  253.      * @return int
  254.      */
  255.     public function getStatusCode()
  256.     {
  257.         return $this->statusCode;
  258.     }
  259.     /**
  260.      * @return string
  261.      */
  262.     public function getHttpStatus()
  263.     {
  264.         $statusCode $this->getStatusCode();
  265.         if (empty($statusCode)) {
  266.             $statusCode '301';
  267.         }
  268.         return 'HTTP/1.1 ' $statusCode ' ' $this->getStatusCodes()[$statusCode];
  269.     }
  270.     public function clearDependentCache()
  271.     {
  272.         // this is mostly called in Redirect\Dao not here
  273.         try {
  274.             \Pimcore\Cache::clearTag('redirect');
  275.         } catch (\Exception $e) {
  276.             Logger::crit((string) $e);
  277.         }
  278.     }
  279.     /**
  280.      * @param int|string|null $expiry
  281.      *
  282.      * @return $this
  283.      */
  284.     public function setExpiry($expiry)
  285.     {
  286.         if (is_string($expiry) && !is_numeric($expiry)) {
  287.             $expiry strtotime($expiry);
  288.         }
  289.         $this->expiry $expiry;
  290.         return $this;
  291.     }
  292.     /**
  293.      * @return int|null
  294.      */
  295.     public function getExpiry()
  296.     {
  297.         return $this->expiry;
  298.     }
  299.     /**
  300.      * @return bool|null
  301.      */
  302.     public function getRegex()
  303.     {
  304.         return $this->regex;
  305.     }
  306.     public function isRegex(): bool
  307.     {
  308.         return (bool)$this->regex;
  309.     }
  310.     /**
  311.      * @param bool|null $regex
  312.      *
  313.      * @return $this
  314.      */
  315.     public function setRegex($regex)
  316.     {
  317.         $this->regex $regex ? (bool) $regex null;
  318.         return $this;
  319.     }
  320.     /**
  321.      * @return bool
  322.      */
  323.     public function getActive()
  324.     {
  325.         return $this->active;
  326.     }
  327.     /**
  328.      * @param bool $active
  329.      *
  330.      * @return $this
  331.      */
  332.     public function setActive($active)
  333.     {
  334.         $this->active = (bool) $active;
  335.         return $this;
  336.     }
  337.     /**
  338.      * @param int $sourceSite
  339.      *
  340.      * @return $this
  341.      */
  342.     public function setSourceSite($sourceSite)
  343.     {
  344.         if ($sourceSite) {
  345.             $this->sourceSite = (int) $sourceSite;
  346.         } else {
  347.             $this->sourceSite null;
  348.         }
  349.         return $this;
  350.     }
  351.     /**
  352.      * @return int|null
  353.      */
  354.     public function getSourceSite()
  355.     {
  356.         return $this->sourceSite;
  357.     }
  358.     /**
  359.      * @param int $targetSite
  360.      *
  361.      * @return $this
  362.      */
  363.     public function setTargetSite($targetSite)
  364.     {
  365.         if ($targetSite) {
  366.             $this->targetSite = (int) $targetSite;
  367.         } else {
  368.             $this->targetSite null;
  369.         }
  370.         return $this;
  371.     }
  372.     /**
  373.      * @return int|null
  374.      */
  375.     public function getTargetSite()
  376.     {
  377.         return $this->targetSite;
  378.     }
  379.     /**
  380.      * @param bool $passThroughParameters
  381.      *
  382.      * @return Redirect
  383.      */
  384.     public function setPassThroughParameters($passThroughParameters)
  385.     {
  386.         $this->passThroughParameters = (bool) $passThroughParameters;
  387.         return $this;
  388.     }
  389.     /**
  390.      * @return bool
  391.      */
  392.     public function getPassThroughParameters()
  393.     {
  394.         return $this->passThroughParameters;
  395.     }
  396.     /**
  397.      * @param int $modificationDate
  398.      *
  399.      * @return $this
  400.      */
  401.     public function setModificationDate($modificationDate)
  402.     {
  403.         $this->modificationDate = (int) $modificationDate;
  404.         return $this;
  405.     }
  406.     /**
  407.      * @return int|null
  408.      */
  409.     public function getModificationDate()
  410.     {
  411.         return $this->modificationDate;
  412.     }
  413.     /**
  414.      * @param int $creationDate
  415.      *
  416.      * @return $this
  417.      */
  418.     public function setCreationDate($creationDate)
  419.     {
  420.         $this->creationDate = (int) $creationDate;
  421.         return $this;
  422.     }
  423.     /**
  424.      * @return int|null
  425.      */
  426.     public function getCreationDate()
  427.     {
  428.         return $this->creationDate;
  429.     }
  430.     /**
  431.      * @return int|null
  432.      */
  433.     public function getUserOwner(): ?int
  434.     {
  435.         return $this->userOwner;
  436.     }
  437.     /**
  438.      * @param int|null $userOwner
  439.      */
  440.     public function setUserOwner(?int $userOwner)
  441.     {
  442.         $this->userOwner $userOwner;
  443.     }
  444.     /**
  445.      * @return int|null
  446.      */
  447.     public function getUserModification()
  448.     {
  449.         return $this->userModification;
  450.     }
  451.     /**
  452.      * @param int $userModification
  453.      */
  454.     public function setUserModification($userModification)
  455.     {
  456.         $this->userModification $userModification;
  457.     }
  458.     public function save()
  459.     {
  460.         $this->dispatchEvent(new RedirectEvent($this), RedirectEvents::PRE_SAVE);
  461.         $this->getDao()->save();
  462.         $this->dispatchEvent(new RedirectEvent($this), RedirectEvents::POST_SAVE);
  463.         $this->clearDependentCache();
  464.     }
  465.     public function delete()
  466.     {
  467.         $this->dispatchEvent(new RedirectEvent($this), RedirectEvents::PRE_DELETE);
  468.         $this->getDao()->delete();
  469.         $this->dispatchEvent(new RedirectEvent($this), RedirectEvents::POST_DELETE);
  470.         $this->clearDependentCache();
  471.     }
  472.     /**
  473.      * @return string[]
  474.      */
  475.     public static function getStatusCodes(): array
  476.     {
  477.         return Config::getSystemConfiguration('redirects')['status_codes'];
  478.     }
  479. }