src/Cms/RedirectBundle/Entity/Redirect.php line 21

Open in your IDE?
  1. <?php
  2. namespace Cms\RedirectBundle\Entity;
  3. use Cms\DomainBundle\Entity\Domain;
  4. use Cms\ImportBundle\Model\Interfaces\Importable\ImportableInterface;
  5. use Cms\ImportBundle\Model\Interfaces\Importable\ImportableTrait;
  6. use Cms\TenantBundle\Entity\TenantedEntity;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Symfony\Component\HttpFoundation\Response;
  9. /**
  10.  * Class Redirect
  11.  * @package Cms\RedirectBundle\Entity
  12.  *
  13.  * @ORM\Entity(
  14.  *  repositoryClass = "Cms\RedirectBundle\Doctrine\RedirectRepository"
  15.  * )
  16.  * @ORM\Table(name = "cms__redirect__redirect")
  17.  */
  18. class Redirect extends TenantedEntity implements ImportableInterface
  19. {
  20.     const CODES__301 Response::HTTP_MOVED_PERMANENTLY;
  21.     const CODES__302 Response::HTTP_FOUND;
  22.     const CODES__303 Response::HTTP_SEE_OTHER;
  23.     const CODES__307 Response::HTTP_TEMPORARY_REDIRECT;
  24.     const CODES__308 Response::HTTP_PERMANENTLY_REDIRECT;
  25.     const CODES = array(
  26.         self::CODES__301,
  27.         self::CODES__302,
  28.         self::CODES__303,
  29.         self::CODES__307,
  30.         self::CODES__308,
  31.     );
  32.     const STATUSES__ACTIVE 1;
  33.     const STATUSES__INACTIVE 0;
  34.     const STATUSES = array(
  35.         self::STATUSES__ACTIVE,
  36.         self::STATUSES__INACTIVE
  37.     );
  38.     use ImportableTrait;
  39.     /**
  40.      * @var Domain
  41.      *
  42.      * @ORM\ManyToOne(
  43.      *  targetEntity = "Cms\DomainBundle\Entity\Domain"
  44.      * )
  45.      * @ORM\JoinColumn(
  46.      *  name = "domain",
  47.      *  referencedColumnName = "id",
  48.      *  onDelete = "CASCADE"
  49.      * )
  50.      */
  51.     protected $domain;
  52.     /**
  53.      * @var int
  54.      *
  55.      * @ORM\Column(
  56.      *  type = "integer",
  57.      *  nullable = false,
  58.      *  options = {
  59.      *      "default" = Redirect::CODES__302
  60.      *  }
  61.      * )
  62.      */
  63.     protected $code self::CODES__302;
  64.     /**
  65.      * @var int
  66.      *
  67.      * @ORM\Column(
  68.      *  type = "integer",
  69.      *  options = {
  70.      *      "default" = Redirect::STATUSES__ACTIVE
  71.      *  }
  72.      * )
  73.      */
  74.     protected $status self::STATUSES__ACTIVE;
  75.     /**
  76.      * @var string
  77.      *
  78.      * @ORM\Column(
  79.      *  type = "text",
  80.      *  nullable = true,
  81.      * )
  82.      */
  83.     protected $description;
  84.     /**
  85.      * @var string
  86.      *
  87.      * @ORM\Column(
  88.      *  type = "string",
  89.      *  nullable = false,
  90.      * )
  91.      */
  92.     protected $source;
  93.     /**
  94.      * @var string
  95.      *
  96.      * @ORM\Column(
  97.      *  type = "string",
  98.      *  nullable = false,
  99.      * )
  100.      */
  101.     protected $destination;
  102.     /**
  103.      * @return Domain
  104.      */
  105.     public function getDomain()
  106.     {
  107.         return $this->domain;
  108.     }
  109.     /**
  110.      * @param Domain $value
  111.      * @return $this
  112.      */
  113.     public function setDomain(Domain $value)
  114.     {
  115.         $this->domain $value;
  116.         return $this;
  117.     }
  118.     /**
  119.      * @return string
  120.      */
  121.     public function getSource()
  122.     {
  123.         return $this->source;
  124.     }
  125.     /**
  126.      * @param string $value
  127.      * @return $this
  128.      */
  129.     public function setSource($value)
  130.     {
  131.         $this->source $value;
  132.         return $this;
  133.     }
  134.     /**
  135.      * @return string
  136.      */
  137.     public function getDestination()
  138.     {
  139.         return $this->destination;
  140.     }
  141.     /**
  142.      * @param string $value
  143.      * @return $this
  144.      */
  145.     public function setDestination($value)
  146.     {
  147.         $this->destination $value;
  148.         return $this;
  149.     }
  150.     /**
  151.      * @return int
  152.      */
  153.     public function getCode()
  154.     {
  155.         return $this->code;
  156.     }
  157.     /**
  158.      * @return int
  159.      */
  160.     public function getStatus()
  161.     {
  162.         return $this->status;
  163.     }
  164.     /**
  165.      * @param int $value
  166.      * @return $this
  167.      * @throws \Exception
  168.      */
  169.     public function setCode($value)
  170.     {
  171.         if ( ! in_array($valueself::CODES)) {
  172.             throw new \Exception();
  173.         }
  174.         $this->code $value;
  175.         return $this;
  176.     }
  177.     /**
  178.      * @param int $value
  179.      * @return $this
  180.      * @throws \Exception
  181.      */
  182.     public function setStatus($value)
  183.     {
  184.         if ( ! in_array($valueself::STATUSES)) {
  185.             throw new \Exception();
  186.         }
  187.         $this->status $value;
  188.         return $this;
  189.     }
  190.     /**
  191.      * @return string
  192.      */
  193.     public function getDescription()
  194.     {
  195.         return $this->description;
  196.     }
  197.     /**
  198.      * @param string $value
  199.      * @return $this
  200.      */
  201.     public function setDescription($value)
  202.     {
  203.         $this->description $value;
  204.         return $this;
  205.     }
  206.     /**
  207.      * @return string
  208.      */
  209.     public function getSourceUrl()
  210.     {
  211.         return sprintf(
  212.             '%s://%s/%s',
  213.             ($this->getDomain()->getHttpsUpgrade() === true) ? 'https' 'http',
  214.             $this->getDomain()->getHost(),
  215.             ltrim($this->getSource(), '/')
  216.         );
  217.     }
  218.     /**
  219.      * @return string
  220.      */
  221.     public function getDestinationUrl()
  222.     {
  223.         if (strpos($this->getDestination(), '/') === 0) {
  224.             return sprintf(
  225.                 '%s://%s/%s',
  226.                 ($this->getDomain()->getHttpsUpgrade() === true) ? 'https' 'http',
  227.                 $this->getDomain()->getHost(),
  228.                 ltrim($this->getDestination(), '/')
  229.             );
  230.         }
  231.         return $this->getDestination();
  232.     }
  233.     /**
  234.      * HACK: used as a first stage to fix the redirection code logic that forces 302's everywhere.
  235.      *
  236.      * @return int
  237.      */
  238.     public function getRealCode(): int
  239.     {
  240.         if ($this->getTenant() && $this->getTenant()->hasRedirectCodes()) {
  241.             return $this->getCode() ?: self::CODES__302;
  242.         }
  243.         return self::CODES__302;
  244.     }
  245. }