<?php
namespace Cms\RedirectBundle\Entity;
use Cms\DomainBundle\Entity\Domain;
use Cms\ImportBundle\Model\Interfaces\Importable\ImportableInterface;
use Cms\ImportBundle\Model\Interfaces\Importable\ImportableTrait;
use Cms\TenantBundle\Entity\TenantedEntity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\Response;
/**
* Class Redirect
* @package Cms\RedirectBundle\Entity
*
* @ORM\Entity(
* repositoryClass = "Cms\RedirectBundle\Doctrine\RedirectRepository"
* )
* @ORM\Table(name = "cms__redirect__redirect")
*/
class Redirect extends TenantedEntity implements ImportableInterface
{
const CODES__301 = Response::HTTP_MOVED_PERMANENTLY;
const CODES__302 = Response::HTTP_FOUND;
const CODES__303 = Response::HTTP_SEE_OTHER;
const CODES__307 = Response::HTTP_TEMPORARY_REDIRECT;
const CODES__308 = Response::HTTP_PERMANENTLY_REDIRECT;
const CODES = array(
self::CODES__301,
self::CODES__302,
self::CODES__303,
self::CODES__307,
self::CODES__308,
);
const STATUSES__ACTIVE = 1;
const STATUSES__INACTIVE = 0;
const STATUSES = array(
self::STATUSES__ACTIVE,
self::STATUSES__INACTIVE
);
use ImportableTrait;
/**
* @var Domain
*
* @ORM\ManyToOne(
* targetEntity = "Cms\DomainBundle\Entity\Domain"
* )
* @ORM\JoinColumn(
* name = "domain",
* referencedColumnName = "id",
* onDelete = "CASCADE"
* )
*/
protected $domain;
/**
* @var int
*
* @ORM\Column(
* type = "integer",
* nullable = false,
* options = {
* "default" = Redirect::CODES__302
* }
* )
*/
protected $code = self::CODES__302;
/**
* @var int
*
* @ORM\Column(
* type = "integer",
* options = {
* "default" = Redirect::STATUSES__ACTIVE
* }
* )
*/
protected $status = self::STATUSES__ACTIVE;
/**
* @var string
*
* @ORM\Column(
* type = "text",
* nullable = true,
* )
*/
protected $description;
/**
* @var string
*
* @ORM\Column(
* type = "string",
* nullable = false,
* )
*/
protected $source;
/**
* @var string
*
* @ORM\Column(
* type = "string",
* nullable = false,
* )
*/
protected $destination;
/**
* @return Domain
*/
public function getDomain()
{
return $this->domain;
}
/**
* @param Domain $value
* @return $this
*/
public function setDomain(Domain $value)
{
$this->domain = $value;
return $this;
}
/**
* @return string
*/
public function getSource()
{
return $this->source;
}
/**
* @param string $value
* @return $this
*/
public function setSource($value)
{
$this->source = $value;
return $this;
}
/**
* @return string
*/
public function getDestination()
{
return $this->destination;
}
/**
* @param string $value
* @return $this
*/
public function setDestination($value)
{
$this->destination = $value;
return $this;
}
/**
* @return int
*/
public function getCode()
{
return $this->code;
}
/**
* @return int
*/
public function getStatus()
{
return $this->status;
}
/**
* @param int $value
* @return $this
* @throws \Exception
*/
public function setCode($value)
{
if ( ! in_array($value, self::CODES)) {
throw new \Exception();
}
$this->code = $value;
return $this;
}
/**
* @param int $value
* @return $this
* @throws \Exception
*/
public function setStatus($value)
{
if ( ! in_array($value, self::STATUSES)) {
throw new \Exception();
}
$this->status = $value;
return $this;
}
/**
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* @param string $value
* @return $this
*/
public function setDescription($value)
{
$this->description = $value;
return $this;
}
/**
* @return string
*/
public function getSourceUrl()
{
return sprintf(
'%s://%s/%s',
($this->getDomain()->getHttpsUpgrade() === true) ? 'https' : 'http',
$this->getDomain()->getHost(),
ltrim($this->getSource(), '/')
);
}
/**
* @return string
*/
public function getDestinationUrl()
{
if (strpos($this->getDestination(), '/') === 0) {
return sprintf(
'%s://%s/%s',
($this->getDomain()->getHttpsUpgrade() === true) ? 'https' : 'http',
$this->getDomain()->getHost(),
ltrim($this->getDestination(), '/')
);
}
return $this->getDestination();
}
/**
* HACK: used as a first stage to fix the redirection code logic that forces 302's everywhere.
*
* @return int
*/
public function getRealCode(): int
{
if ($this->getTenant() && $this->getTenant()->hasRedirectCodes()) {
return $this->getCode() ?: self::CODES__302;
}
return self::CODES__302;
}
}