<?php
namespace App\Entity\User;
use App\Entity\Company;
use App\Entity\Offer\Offer;
use App\Entity\SharedOffer;
use App\Entity\Traits\IdTrait;
use App\Exception\UserAccountBlockedException;
use App\Exception\UserAccountNotActiveException;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Doctrine\ORM\Mapping as ORM;
use App\Repository\User\UserRepository;
/**
* @ORM\Entity(repositoryClass=UserRepository::class)
*/
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
use IdTrait;
/**
* @ORM\Column(type="string", length=180, unique=true)
*/
private $email;
/**
* @ORM\Column(type="json")
*/
private $roles = [];
/**
* @var string The hashed password
* @ORM\Column(type="string")
*/
private $password;
/**
* @ORM\Column(type="datetime")
*/
private $createdAt;
/**
* @ORM\Column(type="datetime")
*/
private $updatedAt;
/**
* @ORM\Column(type="boolean")
*/
private $activated;
/**
* @ORM\Column(type="boolean")
*/
private $blocked;
/**
* @ORM\OneToMany(targetEntity=UserPasswordResetToken::class, mappedBy="user",cascade={"persist", "remove"})
*/
private $userPasswordResetTokens;
/**
* @ORM\OneToMany(targetEntity=UserActivationToken::class, mappedBy="user",cascade={"persist", "remove"})
*/
private $userActivationTokens;
/**
* @ORM\OneToMany(targetEntity=Company::class, mappedBy="user",cascade={"persist", "remove"})
*/
private $company;
/**
* @ORM\OneToMany(targetEntity=SharedOffer::class, mappedBy="user")
*/
private $sharedOffers;
public function __construct()
{
$this->userPasswordResetTokens = new ArrayCollection();
$this->userActivationTokens = new ArrayCollection();
$this->company = new ArrayCollection();
$this->sharedOffers = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUserIdentifier(): string
{
return (string) $this->email;
}
/**
* @see UserInterface
*/
public function getRoles(): array
{
$roles = $this->roles;
return array_unique($roles);
}
public function setRoles(array $roles): self
{
$this->roles = $roles;
return $this;
}
/**
* @see PasswordAuthenticatedUserInterface
*/
public function getPassword(): string
{
return $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
/**
* @see UserInterface
*/
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public function setUpdatedAt(\DateTimeInterface $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
public function getActivated(): ?bool
{
return $this->activated;
}
public function setActivated(bool $activated): self
{
$this->activated = $activated;
return $this;
}
public function getBlocked(): ?bool
{
return $this->blocked;
}
public function setBlocked(bool $blocked): self
{
$this->blocked = $blocked;
return $this;
}
/**
* @return Collection|UserPasswordResetToken[]
*/
public function getUserPasswordResetTokens(): Collection
{
return $this->userPasswordResetTokens;
}
public function addUserPasswordResetToken(UserPasswordResetToken $userPasswordResetToken): self
{
if (!$this->userPasswordResetTokens->contains($userPasswordResetToken)) {
$this->userPasswordResetTokens[] = $userPasswordResetToken;
$userPasswordResetToken->setUser($this);
}
return $this;
}
public function removeUserPasswordResetToken(UserPasswordResetToken $userPasswordResetToken): self
{
if ($this->userPasswordResetTokens->removeElement($userPasswordResetToken)) {
// set the owning side to null (unless already changed)
if ($userPasswordResetToken->getUser() === $this) {
$userPasswordResetToken->setUser(null);
}
}
return $this;
}
/**
* @return Collection|UserActivationToken[]
*/
public function getUserActivationTokens(): Collection
{
return $this->userActivationTokens;
}
public function addUserActivationToken(UserActivationToken $userActivationToken): self
{
if (!$this->userActivationTokens->contains($userActivationToken)) {
$this->userActivationTokens[] = $userActivationToken;
$userActivationToken->setUser($this);
}
return $this;
}
public function removeUserActivationToken(UserActivationToken $userActivationToken): self
{
if ($this->userActivationTokens->removeElement($userActivationToken)) {
// set the owning side to null (unless already changed)
if ($userActivationToken->getUser() === $this) {
$userActivationToken->setUser(null);
}
}
return $this;
}
/**
* @return Collection|Company[]
*/
public function getCompany():Collection
{
return $this->company;
}
public function addCompany(?Company &$company):self
{
if(!$this->company->contains($company))
{
$this->company[] = $company;
$company->setUser($this);
}
return $this;
}
public function removeCompany(?Company &$company):self
{
if($this->company->removeElement($company))
{
if($company->getUser() === $this)
{
$company->setUser(null);
}
}
return $this;
}
public function checkIfActive(): void
{
$checkIfActive = false === $this->activated ? throw new UserAccountNotActiveException() : '';
}
public function checkIfBlocked(): void
{
$checkIfBlocked = true === $this->blocked ? throw new UserAccountBlockedException() : '';
}
public static function createUser(array $data): User
{
$user = new User();
$user->setEmail($data['email']);
$user->setRoles(["ROLE_USER"]);
$user->setPassword($data['hashedPassword']);
$user->setCreatedAt(new DateTime());
$user->setUpdatedAt(new DateTime());
$user->setActivated(0);
$user->setBlocked(0);
return $user;
}
/**
* @return Collection<int, SharedOffer>
*/
public function getSharedOffers(): Collection
{
return $this->sharedOffers;
}
public function addSharedOffer(SharedOffer $sharedOffer): self
{
if (!$this->sharedOffers->contains($sharedOffer)) {
$this->sharedOffers[] = $sharedOffer;
$sharedOffer->setUserId($this);
}
return $this;
}
public function checkPermissionToEditOfferByOfferAndUser(?Offer $offer, ?User $user): bool
{
if (is_null($offer) || is_null($user)) {
return false;
}
foreach ($user->getSharedOffers() as $sharedOffer){
if ($sharedOffer->getUser() === $user && $sharedOffer->getOffer() === $offer) {
$editPermission = (bool)($sharedOffer->getPrivileges()['EDITING_OFFER'] ?? false);
if ($editPermission === true) {
return true;
}
return false;
}
}
return false;
}
public function removeSharedOffer(SharedOffer $sharedOffer): self
{
if ($this->sharedOffers->removeElement($sharedOffer)) {
if ($sharedOffer->getUserId() === $this) {
$sharedOffer->setUserId(null);
}
}
return $this;
}
public function getCompanyCountry(): ?string
{
$company = $this->company[0] ?? null;
if (is_null($company)) {
return null;
}
$invoiceData = $company->getInvoiceData();
if ($company->getIsExtraInvoiceData()) {
$invoiceData = $company->getExtraInvoiceData();
}
if (is_null($invoiceData)) {
return null;
}
$address = $invoiceData->getAddress();
if (is_null($address)) {
return null;
}
$country = $address->getCountry();
return is_null($country) === false ? strtolower($country) : null;
}
public function haveCompany(): bool
{
$company = $this->company[0] ?? null;
if (is_null($company)) {
return false;
}
$invoiceData = $company->getInvoiceData();
if (empty($invoiceData->getVatId()) || empty($invoiceData->getName())) {
return false;
}
return true;
}
}