<?php
namespace App\Database\Domain\Entity\User;
use App\Database\Domain\Entity\AbstractEntity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity()
*/
class UserMembership extends AbstractEntity
{
public const SUBSCRIPTION_NONE = 'none';
public const SUBSCRIPTION_STRIPE_YEARLY_PROMO = 'stripe_yearly_promo';
public const SUBSCRIPTION_STRIPE_MONTHLY_STUDENTS = 'stripe_monthly_students';
public const SUBSCRIPTION_STRIPE_DISCOUNT = 'stripe_monthly_discount';
public const SUBSCRIPTION_STRIPE_TOAST_LIGHT_MONTHLY = 'toast_light_monthly';
public const SUBSCRIPTION_STRIPE_TOAST_PRO_MONTHLY = 'toast_pro_monthly';
public const SUBSCRIPTION_STRIPE_TOAST_PRO_PLUS_MONTHLY = 'toast_pro_plus_monthly';
public const SUBSCRIPTION_STRIPE_TOAST_LIGHT_YEARLY = 'toast_light_yearly';
public const SUBSCRIPTION_STRIPE_TOAST_PRO_YEARLY = 'toast_pro_yearly';
public const SUBSCRIPTION_STRIPE_TOAST_PRO_PLUS_YEARLY = 'toast_pro_plus_yearly';
public const SUBSCRIPTION_MIGRATION_TRIAL = 'migration_trial'; // Was given to old users before update
public const SUBSCRIPTION_APPLE = 'apple'; // Was given by ios subscription
public const SUBSCRIPTION_GIFT = 'premium_gift'; // Premium gift
public const SUBSCRIPTION_REFERRAL = 'referral';
/**
* @ORM\Column(type="datetime_immutable", nullable=true)
*/
private ?\DateTimeInterface $premiumExpires;
/**
* @ORM\Column(type="string", nullable=false)
*
* @Assert\Choice(choices={
* UserMembership::SUBSCRIPTION_NONE,
* UserMembership::SUBSCRIPTION_STRIPE_TOAST_LIGHT_MONTHLY,
* UserMembership::SUBSCRIPTION_STRIPE_TOAST_PRO_MONTHLY,
* UserMembership::SUBSCRIPTION_STRIPE_TOAST_PRO_PLUS_MONTHLY,
* UserMembership::SUBSCRIPTION_STRIPE_TOAST_LIGHT_YEARLY,
* UserMembership::SUBSCRIPTION_STRIPE_TOAST_PRO_YEARLY,
* UserMembership::SUBSCRIPTION_STRIPE_TOAST_PRO_PLUS_YEARLY,
* UserMembership::SUBSCRIPTION_MIGRATION_TRIAL,
* UserMembership::SUBSCRIPTION_APPLE,
* UserMembership::SUBSCRIPTION_GIFT,
* }, message="Invalid subscription type")
*/
private string $subscriptionType;
/**
* @ORM\Column(type="boolean")
*/
private bool $allowedTrial;
/**
* @ORM\Column(type="boolean")
*/
private bool $cancelledSubscription;
public function __construct(
string $id,
string $subscriptionType = self::SUBSCRIPTION_NONE,
?\DateTimeInterface $premiumExpires = null,
bool $allowedTrial = true,
bool $cancelledSubscription = false
) {
$this->id = $id;
$this->premiumExpires = $premiumExpires;
$this->allowedTrial = $allowedTrial;
$this->cancelledSubscription = $cancelledSubscription;
$this->subscriptionType = $subscriptionType;
}
public function getPremiumExpires(): ?\DateTimeInterface
{
return $this->premiumExpires;
}
public function setPremiumExpires(?\DateTimeInterface $premiumExpires): self
{
$this->premiumExpires = $premiumExpires;
return $this;
}
public function getSubscriptionType(): string
{
return $this->subscriptionType;
}
public function setSubscriptionType(string $subscriptionType): self
{
$this->subscriptionType = $subscriptionType;
return $this;
}
public function isAllowedTrial(): bool
{
return $this->allowedTrial;
}
public function setAllowedTrial(bool $allowedTrial): self
{
$this->allowedTrial = $allowedTrial;
return $this;
}
public function isCancelledSubscription(): bool
{
return $this->cancelledSubscription;
}
public function setCancelledSubscription(bool $cancelledSubscription): self
{
$this->cancelledSubscription = $cancelledSubscription;
return $this;
}
public function isPremium(): bool
{
if (null === $this->getPremiumExpires()) {
return false;
}
return $this->getPremiumExpires()->getTimestamp() > (new \DateTimeImmutable('now'))->getTimestamp();
}
public function isMigrationTrial(): bool
{
return $this->subscriptionType === self::SUBSCRIPTION_MIGRATION_TRIAL;
}
public function isApple(): bool
{
return $this->subscriptionType === self::SUBSCRIPTION_APPLE;
}
public function isStripeYearly(): bool
{
return in_array($this->subscriptionType, [self::SUBSCRIPTION_STRIPE_TOAST_PRO_YEARLY, self::SUBSCRIPTION_STRIPE_TOAST_PRO_PLUS_YEARLY, self::SUBSCRIPTION_STRIPE_TOAST_LIGHT_YEARLY]);
}
public function isStripeMonthly(): bool
{
return in_array($this->subscriptionType, [self::SUBSCRIPTION_STRIPE_TOAST_PRO_MONTHLY, self::SUBSCRIPTION_STRIPE_TOAST_PRO_PLUS_MONTHLY, self::SUBSCRIPTION_STRIPE_TOAST_LIGHT_MONTHLY]);
}
public function isGift()
{
return $this->subscriptionType === self::SUBSCRIPTION_GIFT;
}
public static function getNonFreeMembershipTypes()
{
return [
self::SUBSCRIPTION_APPLE,
self::SUBSCRIPTION_STRIPE_MONTHLY_STUDENTS,
self::SUBSCRIPTION_STRIPE_TOAST_PRO_YEARLY,
self::SUBSCRIPTION_STRIPE_TOAST_PRO_PLUS_YEARLY,
self::SUBSCRIPTION_STRIPE_TOAST_LIGHT_YEARLY,
self::SUBSCRIPTION_STRIPE_TOAST_PRO_MONTHLY,
self::SUBSCRIPTION_STRIPE_TOAST_PRO_PLUS_MONTHLY,
self::SUBSCRIPTION_STRIPE_TOAST_LIGHT_MONTHLY,
self::SUBSCRIPTION_STRIPE_YEARLY_PROMO,
];
}
public function isFreeSubscription(): bool
{
return !in_array($this->subscriptionType, static::getNonFreeMembershipTypes());
}
}