<?php
namespace Cms\DomainBundle\Entity;
use Cms\DomainBundle\Entity\Embeddables\DomainRedirectionEmbeddable;
use Cms\DomainBundle\Model\Dns\DomainHostInterface;
use Cms\DomainBundle\Model\Dns\LookupTrait;
use Cms\TenantBundle\Entity\TenantedEntity;
use Doctrine\ORM\Mapping as ORM;
/**
* Class Domain
* @package Cms\DomainBundle\Entity
*
* @ORM\Entity(
* repositoryClass = "Cms\DomainBundle\Doctrine\DomainRepository"
* )
*
* @ORM\Table(
* name = "cms__domain__domain",
* uniqueConstraints = {
* @ORM\UniqueConstraint(
* name = "idx_limit_subdomains",
* columns = {
* "tenant",
* "apex",
* "name"
* }
* )
* }
* )
*/
class Domain extends TenantedEntity implements DomainHostInterface
{
use LookupTrait;
/**
* @var Apex
*
* @ORM\ManyToOne(
* targetEntity = "Apex",
* inversedBy = "domains"
* )
*
* @ORM\JoinColumn(
* name = "apex",
* referencedColumnName = "id",
* onDelete = "CASCADE"
* )
*/
protected $apex;
/**
* @var string
*
* @ORM\Column(
* type = "string",
* nullable = false,
* unique = false
* )
*/
protected $name;
/**
* @var DomainRedirectionEmbeddable
*
* @ORM\Embedded(
* class = "Cms\DomainBundle\Entity\Embeddables\DomainRedirectionEmbeddable",
* columnPrefix = "redirection_"
* )
*/
protected $redirection;
/**
* @var SslCertificate
*
* @ORM\OneToOne(
* targetEntity = "SslCertificate",
* cascade = {"remove"}
* )
*
* @ORM\JoinColumn(
* name = "certificate",
* referencedColumnName = "id",
* onDelete = "SET NULL"
* )
*/
protected $certificate;
/**
* The operating mode of SSL for the domain.
* This determines whether or not SSL is "forced".
*
* @var int
*
* @ORM\Column(
* type = "boolean",
* nullable = false,
* options = {
* "default" = false
* }
* )
*/
protected $httpsUpgrade = false;
/**
* {@inheritdoc}
*/
public function __construct()
{
$this->redirection = new DomainRedirectionEmbeddable();
}
/**
* {@inheritdoc}
*/
public function getHost(): string
{
// if there is no apex, just return the name
// this would be used if like a domain way made on the fly for like intranet use
if (empty($this->getApex())) {
if (empty($this->getName())) {
throw new \Exception();
}
return $this->getName();
}
if (empty($this->getName())) {
return $this->getApex()->getHost();
}
return sprintf(
'%s.%s',
$this->getName(),
$this->getApex()->getHost()
);
}
/**
* @return Apex
*/
public function getApex()
{
return $this->apex;
}
/**
* @param Apex $value
* @return $this
*/
public function setApex(Apex $value)
{
$this->apex = $value;
return $this;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @param string $value
* @return $this
*/
public function setName($value)
{
$this->name = $value;
return $this;
}
/**
* @return DomainRedirectionEmbeddable
*/
public function getRedirection()
{
return $this->redirection;
}
/**
* @return bool
*/
public function isRedirecting()
{
return $this->getRedirection()->isEnabled();
}
/**
* @return SslCertificate|null
*/
public function getCertificate()
{
return $this->certificate;
}
/**
* @param SslCertificate|null $value
* @return $this
*/
public function setCertificate(SslCertificate $value = null)
{
$this->certificate = $value;
return $this;
}
/**
* @return bool
*/
public function isHttpsUpgrade()
{
return ($this->getHttpsUpgrade() === true);
}
/**
* @return bool
*/
public function getHttpsUpgrade()
{
return ($this->httpsUpgrade === true);
}
/**
* @param bool $value
* @return $this
*/
public function setHttpsUpgrade($value)
{
$this->httpsUpgrade = ($value === true);
return $this;
}
/**
* 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() && $this->getRedirection()) {
return $this->getRedirection()->getCode() ?: DomainRedirectionEmbeddable::TYPES__302;
}
return DomainRedirectionEmbeddable::TYPES__302;
}
}