<?php
namespace App\Entity\Config;
use App\Entity\Addon\Installation;
use App\Entity\BaseEntity;
use App\Repository\Config\SmartAlternativeConfigRepository;
use DateTimeInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: SmartAlternativeConfigRepository::class)]
class SmartAlternativeConfig extends BaseEntity
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column()]
private ?int $id = null;
#[ORM\Column(options: ["default" => true])]
private bool $isActive = true;
#[ORM\OneToMany(mappedBy: 'config', targetEntity: SmartAlternativeConfigRule::class, cascade: ['persist', 'remove'], orphanRemoval: true)]
private Collection $rules;
#[ORM\OneToOne(inversedBy: 'config', cascade: ['persist', 'remove'])]
#[ORM\JoinColumn(nullable: false)]
private ?Installation $installation = null;
#[ORM\OneToMany(mappedBy: 'config', targetEntity: SmartAlternativeConfigProduct::class, orphanRemoval: true)]
private Collection $smartAlternativeConfigProducts;
#[ORM\Column(options: ["default" => false])]
private bool $isProductsRetrieved = false;
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true, options: ["default" => null])]
private ?\DateTimeInterface $productsExpiry = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $customHtmlSelector = null;
#[ORM\Column(options: ["default" => false])]
private bool $deactivateDiscount = false;
public function __construct()
{
$this->rules = new ArrayCollection();
$this->addRule($this->defaultRule());
$this->smartAlternativeConfigProducts = new ArrayCollection();
}
private function defaultRule(): SmartAlternativeConfigRule
{
$rule = new SmartAlternativeConfigRule();
$rule->setConfig($this);
$rule->setIsGlobal(true);
return $rule;
}
public function getId(): ?int
{
return $this->id;
}
public function isIsActive(): ?bool
{
return $this->isActive;
}
public function setIsActive(bool $isActive): self
{
$this->isActive = $isActive;
return $this;
}
/**
* @return Collection<int, SmartAlternativeConfigRule>
*/
public function getRules(): Collection
{
return $this->rules;
}
public function addRule(SmartAlternativeConfigRule $rule): self
{
if (!$this->rules->contains($rule)) {
$this->rules[] = $rule;
$rule->setConfig($this);
}
return $this;
}
public function removeRule(SmartAlternativeConfigRule $rule): self
{
if ($this->rules->removeElement($rule)) {
// set the owning side to null (unless already changed)
if ($rule->getConfig() === $this) {
$rule->setConfig(null);
}
}
return $this;
}
public function getInstallation(): ?Installation
{
return $this->installation;
}
public function setInstallation(Installation $installation): self
{
$this->installation = $installation;
return $this;
}
/**
* @return Collection<int, SmartAlternativeConfigProduct>
*/
public function getSmartAlternativeConfigProducts(): Collection
{
return $this->smartAlternativeConfigProducts;
}
public function addSmartAlternativeConfigProduct(SmartAlternativeConfigProduct $smartAlternativeConfigProduct): self
{
if (!$this->smartAlternativeConfigProducts->contains($smartAlternativeConfigProduct)) {
$this->smartAlternativeConfigProducts[] = $smartAlternativeConfigProduct;
$smartAlternativeConfigProduct->setConfig($this);
}
return $this;
}
public function removeSmartAlternativeConfigProduct(SmartAlternativeConfigProduct $smartAlternativeConfigProduct): self
{
if ($this->smartAlternativeConfigProducts->removeElement($smartAlternativeConfigProduct)) {
// set the owning side to null (unless already changed)
if ($smartAlternativeConfigProduct->getConfig() === $this) {
$smartAlternativeConfigProduct->setConfig(null);
}
}
return $this;
}
public function isIsProductsRetrieved(): ?bool
{
return $this->isProductsRetrieved;
}
public function setIsProductsRetrieved(bool $isProductsRetrieved): self
{
$this->isProductsRetrieved = $isProductsRetrieved;
return $this;
}
public function getProductsExpiry(): ?DateTimeInterface
{
return $this->productsExpiry;
}
public function setProductsExpiry(?DateTimeInterface $productsExpiry): self
{
$this->productsExpiry = $productsExpiry;
return $this;
}
public function willProductsExpire($timeQueryFromNow = "+ 1 hour"): bool
{
$now = new \DateTime();
$expiry = $this->productsExpiry;
if (!$expiry) return false;
$now->modify($timeQueryFromNow);
return $now >= $expiry;
}
public function getCustomHtmlSelector(): ?string
{
return $this->customHtmlSelector;
}
public function setCustomHtmlSelector(?string $customHtmlSelector): self
{
$this->customHtmlSelector = $customHtmlSelector;
return $this;
}
public function setDeactivateDiscount(bool $deactivateDiscount): self
{
$this->deactivateDiscount = $deactivateDiscount;
return $this;
}
public function isDeactivateDiscount(): bool
{
return $this->deactivateDiscount;
}
}