src/Entity/Subscription.php line 16

  1. <?php
  2. namespace App\Entity;
  3. use App\Kimia;
  4. use App\Repository\SubscriptionRepository;
  5. use App\Trait\ArrayableTrait;
  6. use DateTime;
  7. use DateTimeInterface;
  8. use Doctrine\Common\Collections\ArrayCollection;
  9. use Doctrine\Common\Collections\Collection;
  10. use Doctrine\DBAL\Types\Types;
  11. use Doctrine\ORM\Mapping as ORM;
  12. #[ORM\Entity(repositoryClass: SubscriptionRepository::class)]
  13. class Subscription
  14. {
  15.     use ArrayableTrait;
  16.     #[ORM\Id]
  17.     #[ORM\GeneratedValue]
  18.     #[ORM\Column]
  19.     private ?int $id = null;
  20.     #[ORM\Column(type: Types::SMALLINT)]
  21.     private ?int $state = Kimia::ACTIVE;
  22.     #[ORM\Column(type: Types::DATETIME_MUTABLE)]
  23.     private ?DateTimeInterface $nextPaymentAt = null;
  24.     #[ORM\ManyToOne]
  25.     #[ORM\JoinColumn(nullable: false)]
  26.     private ?Plan $plan = null;
  27.     #[ORM\ManyToMany(targetEntity: Option::class)]
  28.     private Collection $extraOptions;
  29.     #[ORM\ManyToOne]
  30.     #[ORM\JoinColumn(nullable: false)]
  31.     private ?User $user = null;
  32.     #[ORM\Column(type: Types::DATETIME_MUTABLE)]
  33.     private ?DateTimeInterface $createdAt;
  34.     #[ORM\Column]
  35.     private ?float $amount = null;
  36.     #[ORM\ManyToOne]
  37.     #[ORM\JoinColumn(nullable: false)]
  38.     private ?Currency $currency = null;
  39.     #[ORM\ManyToOne(inversedBy: 'subscriptions')]
  40.     #[ORM\JoinColumn(nullable: false)]
  41.     private ?Establishment $establishment = null;
  42.     public function __construct()
  43.     {
  44.         $this->createdAt = new DateTime();
  45.         $this->extraOptions = new ArrayCollection();
  46.     }
  47.     public function getId(): ?int
  48.     {
  49.         return $this->id;
  50.     }
  51.     public function getState(): ?int
  52.     {
  53.         return $this->state;
  54.     }
  55.     public function setState(int $state): self
  56.     {
  57.         $this->state = $state;
  58.         return $this;
  59.     }
  60.     public function getNextPaymentAt(): ?DateTimeInterface
  61.     {
  62.         return $this->nextPaymentAt;
  63.     }
  64.     public function getExpirationDays(): bool|int
  65.     {
  66.         $dateTime = new DateTime();
  67.         return $this->nextPaymentAt > $dateTime ? $dateTime->diff($this->nextPaymentAt, true)->days : 0;
  68.     }
  69.     public function setNextPaymentAt(DateTimeInterface $nextPaymentAt): self
  70.     {
  71.         $this->nextPaymentAt = $nextPaymentAt;
  72.         return $this;
  73.     }
  74.     /**
  75.      * @throws \Exception
  76.      */
  77.     public function addDuration(int $duration): self
  78.     {
  79.         $date = $this->nextPaymentAt ?? new \DateTime();
  80.         $this->nextPaymentAt = $date->modify("+$duration days");
  81.         return $this;
  82.     }
  83.     public function getPlan(): ?Plan
  84.     {
  85.         return $this->plan;
  86.     }
  87.     public function setPlan(?Plan $plan): self
  88.     {
  89.         $this->plan = $plan;
  90.         return $this;
  91.     }
  92.     public function getOptions(): ArrayCollection|Collection
  93.     {
  94.         $options = $this->extraOptions;
  95.         foreach ($this->plan->getOptions() as $option) {
  96.             $options->add($option);
  97.         }
  98.         return $options;
  99.     }
  100.     /**
  101.      * @return Collection<int, Option>
  102.      */
  103.     public function getExtraOptions(): Collection
  104.     {
  105.         return $this->extraOptions;
  106.     }
  107.     public function addExtraOption(Option $extraOption): self
  108.     {
  109.         if (!$this->extraOptions->contains($extraOption)) {
  110.             $this->extraOptions->add($extraOption);
  111.         }
  112.         return $this;
  113.     }
  114.     public function removeExtraOption(Option $extraOption): self
  115.     {
  116.         $this->extraOptions->removeElement($extraOption);
  117.         return $this;
  118.     }
  119.     public function getUser(): ?User
  120.     {
  121.         return $this->user;
  122.     }
  123.     public function setUser(?User $user): self
  124.     {
  125.         $this->user = $user;
  126.         return $this;
  127.     }
  128.     public function getCreatedAt(): ?DateTimeInterface
  129.     {
  130.         return $this->createdAt;
  131.     }
  132.     public function setCreatedAt(DateTimeInterface $createdAt): self
  133.     {
  134.         $this->createdAt = $createdAt;
  135.         return $this;
  136.     }
  137.     public function isActive(): bool
  138.     {
  139.         return $this->getState() === Kimia::ACTIVE;
  140.     }
  141.     public function isPremium(): bool
  142.     {
  143.         return $this->getPlan()?->getName() === Kimia::PLAN_PREMIUM;
  144.     }
  145.     public function getAmount(): ?float
  146.     {
  147.         return $this->amount;
  148.     }
  149.     public function setAmount(float $amount): self
  150.     {
  151.         $this->amount = $amount;
  152.         return $this;
  153.     }
  154.     public function getCurrency(): ?Currency
  155.     {
  156.         return $this->currency;
  157.     }
  158.     public function setCurrency(?Currency $currency): self
  159.     {
  160.         $this->currency = $currency;
  161.         return $this;
  162.     }
  163.     public function getEstablishment(): ?Establishment
  164.     {
  165.         return $this->establishment;
  166.     }
  167.     public function setEstablishment(?Establishment $establishment): self
  168.     {
  169.         $this->establishment = $establishment;
  170.         return $this;
  171.     }
  172. }