<?php
namespace Cms\TenantBundle\Entity;
use App\Entity\Shared\UlidTransitionalInterface;
use App\Entity\Shared\UlidTransitionalTrait;
use App\Util\Locales;
use Cms\CoreBundle\Entity\LocaleSettings;
use Cms\CoreBundle\Model\CustomDataTrait;
use Cms\CoreBundle\Model\Interfaces\Identifiable\IdentifiableInterface;
use Cms\CoreBundle\Model\Interfaces\Identifiable\IdentifiableTrait;
use Cms\CoreBundle\Model\Interfaces\Timestampable\TimestampableInterface;
use Cms\CoreBundle\Model\Interfaces\Timestampable\TimestampableTrait;
use Cms\TenantBundle\Model\ProductsBitwise;
use Cms\TenantBundle\Model\SimpleTenantableInterface;
use Doctrine\ORM\Mapping as ORM;
use DateTime;
use Products\AdaBundle\Entity\AdaSettingsEmbeddable;
use Reinder83\BinaryFlags\Bits;
/**
* Class Tenant
* @package Cms\TenantBundle\Entity
*
* @ORM\Entity(repositoryClass = "Cms\TenantBundle\Doctrine\TenantRepository")
* @ORM\Table(name = "cms__tenant__tenant")
*/
class Tenant
implements
IdentifiableInterface,
TimestampableInterface,
SimpleTenantableInterface,
UlidTransitionalInterface
{
use IdentifiableTrait;
use TimestampableTrait;
use CustomDataTrait;
use TenantAuthenticationTrait;
use UlidTransitionalTrait;
const STATUS__OK = 'OK';
const STATUS__SUSPENDED = 'suspended';
const STATUS__INACTIVE = 'inactive';
const STAGE__PROVISIONED = 0;
const STAGE__BUILD = 1;
const STAGE__LIVE = 2;
const STAGE__DEFUNCT = 3;
const EMAILS__SHOW = Bits::BIT_1;
const EMAILS__OBFUSCATE = Bits::BIT_2;
const EMAILS__HIDE = Bits::BIT_3;
/**
* @var string
*
* @ORM\Column(type = "string", nullable = false, unique = true)
*/
protected $name;
/**
* @var string
*
* @ORM\Column(type = "string", nullable = false, unique = true)
*/
protected $slug;
/**
* @var string
*
* @ORM\Column(type = "string", nullable = false)
*/
protected $status;
/**
* @var int
*
* @ORM\Column(
* type = "integer",
* nullable = false,
* options = {
* "default" = Tenant::EMAILS__SHOW
* }
* )
*/
protected $emailsDisplay = self::EMAILS__SHOW;
/**
* @var LocaleSettings
*
* @ORM\Embedded(class = "Cms\CoreBundle\Entity\LocaleSettings", columnPrefix = "locale_")
*/
protected $locale;
/**
* @var DateTime
* @ORM\Column(type = "datetime", nullable = true)
*/
protected $policy;
/**
* @var string
*
* @ORM\Column(type = "string", nullable = true)
*/
protected $disqusId;
/**
* @var string
*
* @ORM\Column(
* type = "string",
* nullable = true
* )
*/
protected $googleToken;
/**
* @var string
*
* @ORM\Column(type = "integer", length=1, nullable = false, options={"default" = Tenant::STAGE__PROVISIONED})
*/
protected $stage = self::STAGE__PROVISIONED;
/**
* @var integer
*
* @ORM\Column(type = "integer", nullable = true)
*/
protected $teamworkTasklist;
/**
* @var string
*
* @ORM\Column(type = "string", nullable = true)
*/
protected $latitude;
/**
* @var string
*
* @ORM\Column(type = "string", nullable = true)
*/
protected $longitude;
/**
* @var bool
*
* @ORM\Column(
* type = "boolean",
* nullable = false,
* options = {
* "default" = false
* }
* )
*/
protected $redirects = false;
/**
* @var bool
*
* @ORM\Column(
* type = "boolean",
* nullable = false,
* options = {
* "default" = false,
* },
* )
*/
protected bool $redirectCodes = false;
/**
* @var bool
*
* @ORM\Column(
* type = "boolean",
* nullable = false,
* options = {
* "default" = false
* }
* )
*/
protected $debugging = false;
/**
* @var ProductsBitwise
*
* @ORM\Column(
* type = "bitwise_products",
* nullable = false,
* options = {
* "default" = 0
* }
* )
*/
protected $products;
/**
* @var AdaSettingsEmbeddable
*
* @ORM\Embedded(
* class = "Products\AdaBundle\Entity\AdaSettingsEmbeddable",
* columnPrefix = "ada_"
* )
*/
protected $adaSettings;
/**
* @var TenantTypeEmbeddable
*
* @ORM\Embedded(
* class = "Cms\TenantBundle\Entity\TenantTypeEmbeddable",
* columnPrefix = "type_"
* )
*/
protected $type;
/**
* @var string|null
*
* @ORM\Column(
* type = "string",
* nullable = true,
* unique = true
* )
*/
protected $hubspotId;
/**
* @var string|null
*
* @ORM\Column(
* type = "string",
* nullable = true,
* )
*/
protected ?string $weglotId = null;
/**
* @var array|string[]
*/
protected array $locales = [];
/**
* Tenant constructor.
*/
public function __construct()
{
$this->locale = new LocaleSettings();
$this->adaSettings = new AdaSettingsEmbeddable();
$this->type = new TenantTypeEmbeddable();
}
/**
* @return bool
*/
public function hasRedirectCodes(): bool
{
return $this->redirectCodes;
}
/**
* @param bool $redirectCodes
* @return $this
*/
public function setRedirectCodes(bool $redirectCodes): self
{
$this->redirectCodes = $redirectCodes;
return $this;
}
/**
* @return string|null
*/
public function getWeglotId(): ?string
{
return $this->weglotId;
}
/**
* @param string|null $weglotId
* @return $this
*/
public function setWeglotId(?string $weglotId): self
{
$this->weglotId = $weglotId;
return $this;
}
/**
* @return int
*/
public function getEmailsDisplay(): int
{
return $this->emailsDisplay;
}
/**
* @param int $value
* @return $this
*/
public function setEmailsDisplay(int $value): self
{
$this->emailsDisplay = $value;
return $this;
}
/**
* @param string|int $value
* @return bool
*/
public function isEmailsDisplay($value): bool
{
if (is_string($value)) {
$value = constant(self::class.'::'.$value);
}
if ( ! is_int($value)) {
throw new \Exception();
}
return ($this->getEmailsDisplay() === $value);
}
/**
* @return TenantTypeEmbeddable
*/
public function getType()
{
return $this->type;
}
/**
* @param TenantTypeEmbeddable $value
* @return $this
*/
public function setType(TenantTypeEmbeddable $value)
{
$this->type = $value;
return $this;
}
/**
* @return ProductsBitwise
*/
public function getProducts()
{
return $this->products;
}
/**
* @param ProductsBitwise $value
* @return $this
*/
public function setProducts(ProductsBitwise $value)
{
if ( ! $this->products instanceof ProductsBitwise) {
$this->products = new ProductsBitwise();
}
$this->products = $this->products->orm($value);
return $this;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @return string
*/
public function getSlug()
{
return $this->slug;
}
/**
* @return string
*/
public function getStatus()
{
return $this->status;
}
/**
* @param string $value
* @return $this
*/
public function setName($value)
{
$this->name = $value;
return $this;
}
/**
* @param string $value
* @return $this
*/
public function setSlug($value)
{
$this->slug = $value;
return $this;
}
/**
* @param string $value
* @return $this
*/
public function setStatus($value)
{
$this->status = $value;
return $this;
}
/**
* @return LocaleSettings
*/
public function getLocale()
{
return $this->locale;
}
/**
* @param LocaleSettings $locale
* @return $this
*/
public function setLocale(LocaleSettings $locale)
{
$this->locale = $locale;
return $this;
}
/**
* @return DateTime
*/
public function getPolicy()
{
return $this->policy;
}
/**
* @param DateTime $policy
* @return $this
*/
public function setPolicy(DateTime $policy)
{
$this->policy = $policy;
return $this;
}
/**
* @return string
*/
public function getDisqusId()
{
return $this->disqusId;
}
/**
* @param string $disqusId
* @return $this
*/
public function setDisqusId($disqusId)
{
$this->disqusId = $disqusId;
return $this;
}
/**
* @return string
*/
public function getGoogleToken()
{
return $this->googleToken;
}
/**
* @param string $value
* @return $this
*/
public function setGoogleToken($value = null)
{
$this->googleToken = $value;
return $this;
}
/**
* @return string
*/
public function getStage()
{
return $this->stage;
}
/**
* @param string $stage
* @return $this
*/
public function setStage($stage)
{
$this->stage = $stage;
return $this;
}
/**
* @return int
*/
public function getTeamworkTasklist()
{
return $this->teamworkTasklist;
}
/**
* @param int $teamworkTasklist
* @return $this
*/
public function setTeamworkTasklist($teamworkTasklist)
{
$this->teamworkTasklist = $teamworkTasklist;
return $this;
}
/**
* @return string
*/
public function getLatitude()
{
return $this->latitude;
}
/**
* @param string $value
* @return $this
*/
public function setLatitude($value)
{
$this->latitude = $value;
return $this;
}
/**
* @return string
*/
public function getLongitude()
{
return $this->longitude;
}
/**
* @param string $value
* @return $this
*/
public function setLongitude($value)
{
$this->longitude = $value;
return $this;
}
/**
* @return bool
*/
public function getRedirects()
{
return ($this->redirects === true);
}
/**
* @param bool $value
* @return $this
*/
public function setRedirects($value)
{
$this->redirects = ($value === true);
return $this;
}
/**
* @return bool
*/
public function getDebugging()
{
return ($this->debugging === true);
}
/**
* @param bool $value
* @return $this
*/
public function setDebugging($value)
{
$this->debugging = ($value === true);
return $this;
}
/**
* @return AdaSettingsEmbeddable
*/
public function getAdaSettings()
{
return $this->adaSettings;
}
/**
* @return string|null
*/
public function getHubspotId(): ?string
{
return $this->hubspotId;
}
/**
* @param string|null $hubspotId
* @return $this
*/
public function setHubspotId(?string $hubspotId): self
{
$this->hubspotId = $hubspotId;
return $this;
}
/**
* {@inheritDoc}
*/
public function getTenant(): ?Tenant
{
return $this;
}
/**
* @return bool
*/
public function isSchoolNow(): bool
{
return $this->getProducts()->hasFlag(ProductsBitwise::SCHOOLNOW__MIGRATED);
}
public function getLocales(): array
{
return $this->locales;
}
public function setLocales(array $locales): self
{
$locales = array_values(array_filter(array_unique(
$locales
)));
foreach ($locales as $locale) {
if ( ! Locales::isLocale($locale)) {
throw new \Exception();
}
}
$primary = null;
if ($locales) {
$primary = $locales[0];
}
usort($locales, function (string $a, string $b) use ($primary) {
if ($a === $b) {
return 0;
}
if ($a === $primary) {
return 1;
}
if ($b === $primary) {
return -1;
}
return $a <=> $b;
});
$this->locales = $locales;
return $this;
}
public function isPrimaryLocale(string $locale): bool
{
return $this->getPrimaryLocale() === $locale;
}
public function getPrimaryLocale(): string
{
return $this->locales ? $this->locales[0] : Locales::RFC4646_DEFAULT;
}
public function setPrimaryLocale(string $locale): self
{
if ( ! Locales::isLocale($locale)) {
throw new \Exception();
}
$locales = $this->getLocales();
$index = array_search($locale, $locales);
if ($index !== false) {
$locales[$index] = null;
}
array_unshift($this->locales, $locale);
return $this->setLocales($locales);
}
public function isSecondaryLocale(string $locale): bool
{
return in_array($locale, $this->getSecondaryLocales());
}
public function getSecondaryLocales(): array
{
return array_slice($this->locales, 1);
}
/**
* @return string
*/
public function getTimezone(): string
{
if (($locale = $this->getLocale()) && ($timezone = $locale->getTimezone())) {
return $timezone;
}
return 'UTC';
}
}