src/Entity/Referral.php line 21

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Entity;
  4. use App\Repository\ReferralRepository;
  5. use DateTime;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\ORM\Mapping\Column;
  8. use Doctrine\ORM\Mapping\Entity;
  9. use Doctrine\ORM\Mapping\Id;
  10. use Doctrine\ORM\Mapping\Index;
  11. use Doctrine\ORM\Mapping\JoinColumn;
  12. use Doctrine\ORM\Mapping\ManyToOne;
  13. use Doctrine\ORM\Mapping\OneToMany;
  14. use Doctrine\ORM\Mapping\OneToOne;
  15. #[Entity(ReferralRepository::class)]
  16. #[Index(fields: ['code'], name'idx_referral_code')]
  17. class Referral
  18. {
  19.     public const DEFAULT_REFERRAL_ACTIVITY_COMMISSION_PERCENT 10;
  20.     public const DEFAULT_REFERRAL_NEW_CAMPAIGN_COMMISSION_PERCENT 10;
  21.     public const DEFAULT_DURATION_DAYS_PER_REFERRED_USER 90;
  22.     public const DURATION_365_DAYS_PER_REFERRED_USER 365;
  23.     #[Id]
  24.     #[OneToOne(targetEntityUser::class)]
  25.     #[JoinColumn(name'id'uniquetruenullablefalseonDelete'CASCADE')]
  26.     private User $user;
  27.     #[Column(length10nullabletrue)]
  28.     private ?string $code null;
  29.     /**
  30.      * @var User|null The User who referred this user
  31.      */
  32.     #[ManyToOne(targetEntityUser::class)]
  33.     #[JoinColumn(name'referred_by_id'referencedColumnName'id'nullabletrue)]
  34.     private ?User $referredBy null;
  35.     /**
  36.      * @var Collection|User[] The Users referred by this user
  37.      */
  38.     #[OneToMany(mappedBy'referredBy'targetEntityUser::class)]
  39.     private Collection|array $referrals = [];
  40.     #[Column(options: ['default' => 1])]
  41.     private bool $isEnabled true;
  42.     #[Column(name'duration_days_per_referred_user'type'integer'options: ['unsigned' => true])]
  43.     private int $durationDaysPerReferredUser self::DEFAULT_DURATION_DAYS_PER_REFERRED_USER;
  44.     #[Column('referred_by_ended_at''datetime'nullabletrue)]
  45.     private ?DateTime $referredByEndedAt null;
  46.     #[Column(name'referral_activity_commission_percent'type'integer'options: ['unsigned' => true])]
  47.     private int $referralActivityCommissionPercent self::DEFAULT_REFERRAL_ACTIVITY_COMMISSION_PERCENT;
  48.     #[Column(name'referral_new_campaign_commission_percent'type'integer'options: ['unsigned' => true])]
  49.     private int $referralNewCampaignCommissionPercent self::DEFAULT_REFERRAL_NEW_CAMPAIGN_COMMISSION_PERCENT;
  50.     /**
  51.      * The total monetary balance earned through referrals.
  52.      * This column stores the cumulative earnings from successful referral actions.
  53.      * Currently, it is used exclusively for sponsors.
  54.      */
  55.     #[Column(type'float'nullablefalse)]
  56.     private float $balance 0;
  57.     public function __construct(User $userstring $code)
  58.     {
  59.         $this->user $user;
  60.         $this->code $code;
  61.     }
  62.     public function getUser(): User
  63.     {
  64.         return $this->user;
  65.     }
  66.     public function getCode(): ?string
  67.     {
  68.         return $this->code;
  69.     }
  70.     public function setCode(string $code): static
  71.     {
  72.         $this->code $code;
  73.         return $this;
  74.     }
  75.     public function getReferredBy(): ?User
  76.     {
  77.         return $this->referredBy;
  78.     }
  79.     public function setReferredBy(User $referredBy): static
  80.     {
  81.         $this->referredBy $referredBy;
  82.         return $this;
  83.     }
  84.     public function hasReferredBy(): bool
  85.     {
  86.         return $this->referredBy !== null;
  87.     }
  88.     /**
  89.      * @return Collection<int, User>
  90.      */
  91.     public function getReferrals(): Collection
  92.     {
  93.         return $this->referrals;
  94.     }
  95.     public function isEnabled(): bool
  96.     {
  97.         return $this->isEnabled;
  98.     }
  99.     public function isDisabled(): bool
  100.     {
  101.         return $this->isEnabled === false;
  102.     }
  103.     public function setIsReferralEnabled(bool $isEnabled): static
  104.     {
  105.         $this->isEnabled $isEnabled;
  106.         return $this;
  107.     }
  108.     public function getDurationDaysPerReferredUser(): int
  109.     {
  110.         return $this->durationDaysPerReferredUser;
  111.     }
  112.     public function setDurationDaysPerReferredUser(int $durationDaysPerReferredUser): self
  113.     {
  114.         $this->durationDaysPerReferredUser $durationDaysPerReferredUser;
  115.         return $this;
  116.     }
  117.     public function getReferredByEndedAt(): ?DateTime
  118.     {
  119.         return $this->referredByEndedAt;
  120.     }
  121.     public function setReferredByEndedAt(DateTime $referredByEndedAt): self
  122.     {
  123.         $this->referredByEndedAt $referredByEndedAt;
  124.         return $this;
  125.     }
  126.     public function isReferredByExpired(): bool
  127.     {
  128.         if ($this->referredByEndedAt === null) {
  129.             return false;
  130.         }
  131.         return $this->referredByEndedAt < (new DateTime())->setTime(235959);
  132.     }
  133.     public function getReferralActivityCommissionPercent(): int
  134.     {
  135.         return $this->referralActivityCommissionPercent;
  136.     }
  137.     public function setReferralActivityCommissionPercent(int $referralActivityCommissionPercent): self
  138.     {
  139.         $this->referralActivityCommissionPercent $referralActivityCommissionPercent;
  140.         return $this;
  141.     }
  142.     public function getReferralNewCampaignCommissionPercent(): int
  143.     {
  144.         return $this->referralNewCampaignCommissionPercent;
  145.     }
  146.     public function setReferralNewCampaignCommissionPercent(int $referralNewCampaignCommissionPercent): self
  147.     {
  148.         $this->referralNewCampaignCommissionPercent $referralNewCampaignCommissionPercent;
  149.         return $this;
  150.     }
  151.     public function getBalance(): float
  152.     {
  153.         return $this->balance;
  154.     }
  155.     public function setBalance(float $balance): self
  156.     {
  157.         $this->balance $balance;
  158.         return $this;
  159.     }
  160.     public function __toString(): string
  161.     {
  162.         return sprintf(
  163.             '#%s %s [%s]',
  164.             $this->user->getId(),
  165.             $this->user->getEmail(),
  166.             basename(str_replace('\\''/'get_class($this->user)))
  167.         );
  168.     }
  169. }