src/Cms/DomainBundle/Entity/Domain.php line 33

Open in your IDE?
  1. <?php
  2. namespace Cms\DomainBundle\Entity;
  3. use Cms\DomainBundle\Entity\Embeddables\DomainRedirectionEmbeddable;
  4. use Cms\DomainBundle\Model\Dns\DomainHostInterface;
  5. use Cms\DomainBundle\Model\Dns\LookupTrait;
  6. use Cms\TenantBundle\Entity\TenantedEntity;
  7. use Doctrine\ORM\Mapping as ORM;
  8. /**
  9.  * Class Domain
  10.  * @package Cms\DomainBundle\Entity
  11.  *
  12.  * @ORM\Entity(
  13.  *     repositoryClass = "Cms\DomainBundle\Doctrine\DomainRepository"
  14.  * )
  15.  *
  16.  * @ORM\Table(
  17.  *     name = "cms__domain__domain",
  18.  *     uniqueConstraints = {
  19.  *         @ORM\UniqueConstraint(
  20.  *             name = "idx_limit_subdomains",
  21.  *             columns = {
  22.  *                 "tenant",
  23.  *                 "apex",
  24.  *                 "name"
  25.  *             }
  26.  *         )
  27.  *     }
  28.  * )
  29.  */
  30. class Domain extends TenantedEntity implements DomainHostInterface
  31. {
  32.     use LookupTrait;
  33.     /**
  34.      * @var Apex
  35.      *
  36.      * @ORM\ManyToOne(
  37.      *     targetEntity = "Apex",
  38.      *     inversedBy = "domains"
  39.      * )
  40.      *
  41.      * @ORM\JoinColumn(
  42.      *     name = "apex",
  43.      *     referencedColumnName = "id",
  44.      *     onDelete = "CASCADE"
  45.      * )
  46.      */
  47.     protected $apex;
  48.     /**
  49.      * @var string
  50.      *
  51.      * @ORM\Column(
  52.      *     type = "string",
  53.      *     nullable = false,
  54.      *     unique = false
  55.      * )
  56.      */
  57.     protected $name;
  58.     /**
  59.      * @var DomainRedirectionEmbeddable
  60.      *
  61.      * @ORM\Embedded(
  62.      *     class = "Cms\DomainBundle\Entity\Embeddables\DomainRedirectionEmbeddable",
  63.      *     columnPrefix = "redirection_"
  64.      * )
  65.      */
  66.     protected $redirection;
  67.     /**
  68.      * @var SslCertificate
  69.      *
  70.      * @ORM\OneToOne(
  71.      *     targetEntity = "SslCertificate",
  72.      *     cascade = {"remove"}
  73.      * )
  74.      *
  75.      * @ORM\JoinColumn(
  76.      *     name = "certificate",
  77.      *     referencedColumnName = "id",
  78.      *     onDelete = "SET NULL"
  79.      * )
  80.      */
  81.     protected $certificate;
  82.     /**
  83.      * The operating mode of SSL for the domain.
  84.      * This determines whether or not SSL is "forced".
  85.      *
  86.      * @var int
  87.      *
  88.      * @ORM\Column(
  89.      *     type = "boolean",
  90.      *     nullable = false,
  91.      *     options = {
  92.      *         "default" = false
  93.      *     }
  94.      * )
  95.      */
  96.     protected $httpsUpgrade false;
  97.     /**
  98.      * {@inheritdoc}
  99.      */
  100.     public function __construct()
  101.     {
  102.         $this->redirection = new DomainRedirectionEmbeddable();
  103.     }
  104.     /**
  105.      * {@inheritdoc}
  106.      */
  107.     public function getHost(): string
  108.     {
  109.         // if there is no apex, just return the name
  110.         // this would be used if like a domain way made on the fly for like intranet use
  111.         if (empty($this->getApex())) {
  112.             if (empty($this->getName())) {
  113.                 throw new \Exception();
  114.             }
  115.             return $this->getName();
  116.         }
  117.         if (empty($this->getName())) {
  118.             return $this->getApex()->getHost();
  119.         }
  120.         return sprintf(
  121.             '%s.%s',
  122.             $this->getName(),
  123.             $this->getApex()->getHost()
  124.         );
  125.     }
  126.     /**
  127.      * @return Apex
  128.      */
  129.     public function getApex()
  130.     {
  131.         return $this->apex;
  132.     }
  133.     /**
  134.      * @param Apex $value
  135.      * @return $this
  136.      */
  137.     public function setApex(Apex $value)
  138.     {
  139.         $this->apex $value;
  140.         return $this;
  141.     }
  142.     /**
  143.      * @return string
  144.      */
  145.     public function getName()
  146.     {
  147.         return $this->name;
  148.     }
  149.     /**
  150.      * @param string $value
  151.      * @return $this
  152.      */
  153.     public function setName($value)
  154.     {
  155.         $this->name $value;
  156.         return $this;
  157.     }
  158.     /**
  159.      * @return DomainRedirectionEmbeddable
  160.      */
  161.     public function getRedirection()
  162.     {
  163.         return $this->redirection;
  164.     }
  165.     /**
  166.      * @return bool
  167.      */
  168.     public function isRedirecting()
  169.     {
  170.         return $this->getRedirection()->isEnabled();
  171.     }
  172.     /**
  173.      * @return SslCertificate|null
  174.      */
  175.     public function getCertificate()
  176.     {
  177.         return $this->certificate;
  178.     }
  179.     /**
  180.      * @param SslCertificate|null $value
  181.      * @return $this
  182.      */
  183.     public function setCertificate(SslCertificate $value null)
  184.     {
  185.         $this->certificate $value;
  186.         return $this;
  187.     }
  188.     /**
  189.      * @return bool
  190.      */
  191.     public function isHttpsUpgrade()
  192.     {
  193.         return ($this->getHttpsUpgrade() === true);
  194.     }
  195.     /**
  196.      * @return bool
  197.      */
  198.     public function getHttpsUpgrade()
  199.     {
  200.         return ($this->httpsUpgrade === true);
  201.     }
  202.     /**
  203.      * @param bool $value
  204.      * @return $this
  205.      */
  206.     public function setHttpsUpgrade($value)
  207.     {
  208.         $this->httpsUpgrade = ($value === true);
  209.         return $this;
  210.     }
  211.     /**
  212.      * HACK: used as a first stage to fix the redirection code logic that forces 302's everywhere.
  213.      *
  214.      * @return int
  215.      */
  216.     public function getRealCode(): int
  217.     {
  218.         if ($this->getTenant() && $this->getTenant()->hasRedirectCodes() && $this->getRedirection()) {
  219.             return $this->getRedirection()->getCode() ?: DomainRedirectionEmbeddable::TYPES__302;
  220.         }
  221.         return DomainRedirectionEmbeddable::TYPES__302;
  222.     }
  223. }