src/Entity/User.php line 31

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Entity\Account\Avatar;
  4. use App\Entity\Location\City;
  5. use App\Entity\Sales\AccountCharge;
  6. use App\Entity\Sales\AccountEnrollment;
  7. use App\Entity\Sales\AccountTransaction;
  8. use App\PaymentProcessing\Exception\CurrencyMismatchException;
  9. use App\PaymentProcessing\Exception\NotEnoughMoneyException;
  10. use App\Repository\UserRepository;
  11. use App\Service\CountryCurrencyResolver;
  12. use Doctrine\Common\Collections\ArrayCollection;
  13. use Doctrine\Common\Collections\Collection;
  14. use Doctrine\ORM\Mapping as ORM;
  15. use Money\Currency;
  16. use Money\Money;
  17. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  18. use Symfony\Component\Validator\Constraints as Assert;
  19. //use Vich\UploaderBundle\Mapping\Annotation as Vich;
  20. use Symfony\Component\Security\Core\User\UserInterface;
  21. #[ORM\Entity(repositoryClassUserRepository::class)]
  22. #[UniqueEntity(fields: ['email'], groups: ['Registration'])]
  23. #[UniqueEntity(fields: ['nickName'], groups: ['Registration'])] // //Vich\Uploadable
  24. #[ORM\HasLifecycleCallbacks]
  25. #[ORM\InheritanceType('SINGLE_TABLE')]
  26. #[ORM\DiscriminatorColumn(name'type'type'string'length12)]
  27. #[ORM\DiscriminatorMap(['advertiser' => Account\Advertiser::class, 'customer' => Account\Customer::class])]
  28. abstract class User implements UserInterface\Serializable
  29. {
  30.     public const ROLE_USER 'ROLE_USER';
  31.     #[ORM\Id]
  32.     #[ORM\GeneratedValue]
  33.     #[ORM\Column(type'integer')]
  34.     private int $id;
  35.     
  36.     #[ORM\Column(type'string'length180uniquetrue)]
  37.     #[Assert\NotBlank(groups: ['Registration'])]
  38.     #[Assert\Email(groups: ['Registration'])]
  39.     private ?string $email null;
  40.     #[ORM\Column(type'string'length100)]
  41.     #[Assert\NotBlank(groups: ['Registration'])]
  42.     #[Assert\Length(max64groups: ['Registration'])]
  43.     private ?string $nickName null;
  44.     #[ORM\Column(type'text'nullabletrue)]
  45.     private ?string $notes null;
  46.     #[ORM\Column(name'country_code'type'string'length2)]
  47.     private string $country;
  48.     #[ORM\JoinColumn(name'city_id'referencedColumnName'id')]
  49.     #[ORM\ManyToOne(targetEntityCity::class)]
  50.     private ?City $city null;
  51.     #[ORM\OneToOne(targetEntityAvatar::class, mappedBy'user'cascade: ['all'], orphanRemovaltrue)]
  52.     protected ?Avatar $avatar;
  53.     #[ORM\Column(type'json')]
  54.     private array $roles = [];
  55.     /** The hashed password*/
  56.     #[ORM\Column(type'string')]
  57.     private string $password;
  58.     #[Assert\NotBlank(groups: ['Registration'])]
  59.     private ?string $plainPassword null;
  60.     #[ORM\Column(type'string'length255nullabletrue)]
  61.     private ?string $confirmationCode;
  62.     #[ORM\Column(type'string'length255nullabletrue)]
  63.     private ?string $smscCode;
  64.     #[ORM\Column(type'boolean')]
  65.     private bool $enabled;
  66.     #[ORM\Column(type'boolean')]
  67.     private bool $trusted false;
  68.     #[ORM\Column(name'credits'type'integer')]
  69.     private int $credits 0;
  70.     /**
  71.      * Валюта для финансовых операций аккаунта.
  72.      * Устанавливается при регистрации в зависимости от выбранной страны, и не может быть изменена через кабинет.
  73.      */
  74.     #[ORM\Column(name'currency_code'type'string'length3)]
  75.     private string $currencyCode;
  76.     #[ORM\Column(type'boolean'options: ['default' => 1])]
  77.     private bool $fullRegistered true;
  78.     #[ORM\Column(type'integer'options: ['default' => 0])]
  79.     private int $postRegistrationStep 0;
  80.     /** @var AccountEnrollment[] */
  81.     #[ORM\OneToMany(targetEntityAccountEnrollment::class, mappedBy'account')]
  82.     private Collection $enrollments;
  83.     /** @var AccountCharge[] */
  84.     #[ORM\OneToMany(targetEntityAccountCharge::class, mappedBy'account')]
  85.     private Collection $charges;
  86.     /** @var AccountTransaction[] */
  87.     #[ORM\OneToMany(targetEntityAccountTransaction::class, mappedBy'account')]
  88.     private Collection $transactions;
  89.     #[ORM\Column(name'created'type'datetime')]
  90.     private \DateTimeInterface $created;
  91.     #[ORM\Column(name'updated'type'datetime'nullabletrue)]
  92.     private ?\DateTimeInterface $updated;
  93.     #[ORM\Column(name'balance_low_notified_at'type'datetime'nullabletrue)]
  94.     private ?\DateTimeInterface $lowBalanceNotifiedAt;
  95.     #[ORM\Column(name'ban'type'string'length5nullabletrue)]
  96.     private ?string $ban null;
  97.     #[ORM\OneToOne(targetEntityOfferBarHidden::class, cascade: ['all'], mappedBy'account')]
  98.     private ?OfferBarHidden $offerBarHidden;
  99.     public function __construct()
  100.     {
  101.         $this->roles = [self::ROLE_USER];
  102.         $this->enabled false;
  103.         $this->enrollments = new ArrayCollection();
  104.         $this->charges = new ArrayCollection();
  105.         $this->transactions = new ArrayCollection();
  106.         //TODO temp
  107.         $this->created = new \DateTime();
  108.         $this->updated = new \DateTime();
  109.     }
  110.     public function getId(): ?int
  111.     {
  112.         return $this->id;
  113.     }
  114.     public function getEmail(): ?string
  115.     {
  116.         return $this->email;
  117.     }
  118.     public function setEmail(string $email): void
  119.     {
  120.         $this->email $email;
  121.     }
  122.     public function getNickName(): ?string
  123.     {
  124.         return $this->nickName;
  125.     }
  126.     public function setNickName(string $nickName): void
  127.     {
  128.         $this->nickName $nickName;
  129.     }
  130.     public function getCountry(): ?string
  131.     {
  132.         return $this->country;
  133.     }
  134.     public function setCountry($country): void
  135.     {
  136.         $this->country $country;
  137.     }
  138.     public function getCity(): ?City
  139.     {
  140.         return $this->city;
  141.     }
  142.     public function setCity(City $city): void
  143.     {
  144.         $this->city $city;
  145.         $this->country $city->getCountryCode();
  146.     }
  147.     /**
  148.      * A visual identifier that represents this user.
  149.      *
  150.      * @see UserInterface
  151.      */
  152.     public function getUsername(): string
  153.     {
  154.         return (string)$this->email;
  155.     }
  156.     /**
  157.      * @see UserInterface
  158.      */
  159.     public function getRoles(): array
  160.     {
  161.         $roles $this->roles;
  162.         // guarantee every user at least has ROLE_USER
  163.         $roles[] = 'ROLE_USER';
  164.         return array_unique($roles);
  165.     }
  166.     public function setRoles(array $roles): self
  167.     {
  168.         $this->roles $roles;
  169.         return $this;
  170.     }
  171.     /**
  172.      * @see UserInterface
  173.      */
  174.     public function getPassword(): string
  175.     {
  176.         return (string)$this->password;
  177.     }
  178.     public function setPassword(string $password): void
  179.     {
  180.         $this->password $password;
  181.     }
  182.     public function getPlainPassword(): ?string
  183.     {
  184.         return $this->plainPassword;
  185.     }
  186.     public function setPlainPassword(string $plainPassword): void
  187.     {
  188.         $this->plainPassword $plainPassword;
  189.     }
  190.     public function getConfirmationCode(): string
  191.     {
  192.         return $this->confirmationCode;
  193.     }
  194.     public function setConfirmationCode(string $confirmationCode): void
  195.     {
  196.         $this->confirmationCode $confirmationCode;
  197.     }
  198.     public function getSmscCode(): string
  199.     {
  200.         return $this->smscCode;
  201.     }
  202.     public function setSmscCode(string $smscCode): void
  203.     {
  204.         $this->smscCode $smscCode;
  205.     }
  206.     public function isEnabled(): bool
  207.     {
  208.         return $this->enabled;
  209.     }
  210.     public function setEnabled(bool $enabled): void
  211.     {
  212.         $this->enabled $enabled;
  213.     }
  214.     public function isTrusted(): bool
  215.     {
  216.         return $this->trusted;
  217.     }
  218.     public function setTrusted(bool $trusted): void
  219.     {
  220.         $this->trusted $trusted;
  221.     }
  222.     public function isFullRegistered(): bool
  223.     {
  224.         return $this->fullRegistered;
  225.     }
  226.     public function setFullRegistered(bool $fullRegistered): void
  227.     {
  228.         $this->fullRegistered $fullRegistered;
  229.     }
  230.     /**
  231.      * Зачисляет деньги на счет аккаунта
  232.      *
  233.      * @param Money $toEnroll
  234.      *
  235.      * @throws \DomainException Если указана отрицательная или нулевая сумма
  236.      * @throws CurrencyMismatchException Если валюты баланса и суммы зачисления не совпадают
  237.      */
  238.     public function enroll(Money $toEnroll): void
  239.     {
  240.         if ($toEnroll->isNegative() || $toEnroll->isZero()) {
  241.             throw new \DomainException('Can not enroll negative or zero amount.');
  242.         }
  243.         $currentBalance $this->getCurrentBalance();
  244.         if (!$currentBalance->isSameCurrency($toEnroll)) {
  245.             throw new CurrencyMismatchException();
  246.         }
  247.         $newBalance $currentBalance->add($toEnroll);
  248.         $this->credits $newBalance->getAmount();
  249.     }
  250.     /**
  251.      * Списывает деньги со счета аккаунта
  252.      *
  253.      * @param Money $toCharge
  254.      * @param bool  $withOverdraft Возможность делать отрицательный баланс для ручных списаний
  255.      *
  256.      * @throws \DomainException Если указана отрицательная или нулевая сумма
  257.      * @throws CurrencyMismatchException Если валюты баланса и суммы списания не совпадают
  258.      * @throws NotEnoughMoneyException Если на счету недостаточно средств
  259.      */
  260.     public function charge(Money $toChargebool $withOverdraft false): void
  261.     {
  262.         if ($toCharge->isNegative() || $toCharge->isZero()) {
  263.             throw new \DomainException('Can not charge negative or zero amount.');
  264.         }
  265.         $currentBalance $this->getCurrentBalance();
  266.         if (!$currentBalance->isSameCurrency($toCharge)) {
  267.             throw new CurrencyMismatchException();
  268.         }
  269.         if ($currentBalance->lessThan($toCharge) && !$withOverdraft) {
  270.             throw new NotEnoughMoneyException();
  271.         }
  272.         $newBalance $currentBalance->subtract($toCharge);
  273.         $this->credits $newBalance->getAmount();
  274.     }
  275.     public function getCurrentBalance(): Money
  276.     {
  277.         return new Money($this->credits, new Currency($this->currencyCode));
  278.     }
  279.     public function getCurrencyCode(): string
  280.     {
  281.         return $this->currencyCode;
  282.     }
  283.     public function resolveCurrency(CountryCurrencyResolver $currencyResolver): void
  284.     {
  285.         $this->currencyCode $currencyResolver->getCurrencyFor($this->country);
  286.     }
  287.     /**
  288.      * @return AccountEnrollment[]
  289.      */
  290.     public function getEnrollments(): Collection
  291.     {
  292.         return $this->enrollments;
  293.     }
  294.     /**
  295.      * @return AccountCharge[]
  296.      */
  297.     public function getCharges(): Collection
  298.     {
  299.         return $this->charges;
  300.     }
  301.     /**
  302.      * @return AccountTransaction[]
  303.      */
  304.     public function getTransactions(): Collection
  305.     {
  306.         return $this->transactions;
  307.     }
  308.     /**
  309.      * @see UserInterface
  310.      */
  311.     public function getSalt(): void
  312.     {
  313.         // not needed when using the "bcrypt" algorithm in security.yaml
  314.     }
  315.     /**
  316.      * @see UserInterface
  317.      */
  318.     public function eraseCredentials(): void
  319.     {
  320.         // If you store any temporary, sensitive data on the user, clear it here
  321.         $this->plainPassword null;
  322.     }
  323.     public function getCreated(): \DateTimeInterface
  324.     {
  325.         return $this->created;
  326.     }
  327.     /**
  328.      * @inheritDoc
  329.      */
  330.     public function serialize()
  331.     {
  332.         return \serialize([
  333.             $this->id,
  334.             $this->email,
  335.             $this->password,
  336.             $this->roles,
  337.             $this->enabled,
  338.         ]);
  339.     }
  340.     /**
  341.      * @inheritDoc
  342.      */
  343.     public function unserialize($serialized): void
  344.     {
  345.         list(
  346.             $this->id,
  347.             $this->email,
  348.             $this->password,
  349.             $this->roles,
  350.             $this->enabled
  351.             ) = \unserialize($serialized, ['allowed_classes' => false]);
  352.     }
  353.     public function getNotes(): ?string
  354.     {
  355.         return $this->notes;
  356.     }
  357.     public function setNotes(?string $notes): void
  358.     {
  359.         $this->notes $notes;
  360.     }
  361.     public function isBanned(): bool
  362.     {
  363.         return null != $this->ban;
  364.     }
  365.     public function getBan(): ?string
  366.     {
  367.         return $this->ban;
  368.     }
  369.     public function setBan(?string $ban): void
  370.     {
  371.         $this->ban $ban;
  372.     }
  373.     public function isLowBalanceNotified(): bool
  374.     {
  375.         return $this->lowBalanceNotifiedAt != null;
  376.     }
  377.     public function setLowBalanceNotified(?\DateTimeInterface $dateTime): void
  378.     {
  379.         $this->lowBalanceNotifiedAt $dateTime;
  380.     }
  381.     public function getAvatar(): ?Avatar
  382.     {
  383.         return $this->avatar;
  384.     }
  385.     public function setAvatar(string $path): void
  386.     {
  387.         $this->avatar = new Avatar($this$path);
  388.     }
  389.     public function getPostRegistrationStep(): int
  390.     {
  391.         return $this->postRegistrationStep;
  392.     }
  393.     public function setPostRegistrationStep(int $postRegistrationStep): void
  394.     {
  395.         $this->postRegistrationStep $postRegistrationStep;
  396.     }
  397.     public function offerBarHidden(): ?OfferBarHidden
  398.     {
  399.         return $this->offerBarHidden;
  400.     }
  401.     public function setOfferBarHidden(): void
  402.     {
  403.         if (null !== $this->offerBarHidden) {
  404.             return;
  405.         }
  406.         $this->offerBarHidden = new OfferBarHidden($this);
  407.     }
  408. }