<?php
declare(strict_types=1);
/**
* @author Maciej Kaczmarek <maciej.kaczmarek@autentika.pl>
*/
namespace Hitso\Bundle\CommonBundle\Entity;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\PersistentCollection;
use FOS\UserBundle\Model\User as BaseUser;
use Hitso\Bundle\CommonBundle\Annotation\Loggable;
use Hitso\Bundle\CommonBundle\Doctrine\Behaviours\Identifiable;
use Hitso\Bundle\CommonBundle\Interfaces\Timestampable;
use Hitso\Bundle\CommonBundle\Traits\TimestampableTrait;
use Hitso\Bundle\FileManagerBundle\Entity\File;
use Hitso\Extra\CommonBundle\Entity\UserExtraTrait;
use Hitso\Extra\CommonBundle\Entity\UserExtraInterface;
use Symfony\Component\Serializer\Annotation as Serializer;
/**
* Class User
*
* @package Hitso\Bundle\CommonBundle\Entity
*
* @Loggable(
* createMsg={"Utworzono konto użytkownika", "%user% utworzył(|a) nowego użytkownika %label%"},
* updateMsg={"Aktualizacja konta użytkownika", "%user% zaktualizował(|a) konto użytkownika %label%"},
* removeMsg={"Usunięcie konta użytkownika", "%user% usun(ął|ęła) konto użytkownika %label%"},
* routeName="hitso_admin_users_edit",
* sites={},
* labelSource="name",
* ignoredFields={"lastLogin","updatedAt"},
* saveChangeSet=true
* )
*
* @codeCoverageIgnore
*/
class User extends BaseUser implements UserExtraInterface, EntityInterface, Timestampable
{
const GENDER_MALE = 'm';
const GENDER_FEMALE = 'f';
use Identifiable;
use UserExtraTrait;
use TimestampableTrait;
/**
* @var string
* @Serializer\Groups({"WS"})
*/
protected $firstName;
/**
* @var string
*/
protected $lastName;
/**
* @var string
*/
protected $slug;
/**
* @var string
*/
protected $description;
/**
* @var string
*/
protected $biography;
/**
* @var DateTime
*/
protected $deletedAt;
/**
* @var string[]
*/
protected $allowedSites;
/**
* @var ArrayCollection|UserNote[]
*/
protected $notes;
/**
* @var string
*/
protected $fbLink;
/**
* @var string
*/
protected $gender;
/**
* One Signal User Id (notificaton push service)
*
* @var string
*/
protected $oneSignalId;
/**
* Not mapped.
*
* @var string
* @Serializer\Groups({"WS"})
*/
protected $name;
/**
* Not mapped.
*
* @var string
*/
protected $currentPassword;
/**
* Not mapped.
*
* @var string
*/
protected $fullName;
/**
* @var string
*/
protected $lastLoginDevice;
/**
* @var File
*/
protected $avatar;
/**
* @var string
*/
protected $defaultLocale;
/**
* @var string
*/
protected $apiKey = '';
/**
* User constructor.
*
* @param null $id
*/
public function __construct($id = null)
{
parent::__construct();
$this->id = $id;
$this->roles = ['ROLE_USER'];
$this->allowedSites = [];
$this->notes = new ArrayCollection();
}
/**
* @param int $id
*
* @return User
*/
public function setId($id)
{
$this->id = $id;
return $this;
}
/**
* Get date of deletion
*
* @return DateTime
*/
public function getDeletedAt()
{
return $this->deletedAt;
}
/**
* Set date of deletion
*
* @param DateTime $deletedAt
*/
public function setDeletedAt(DateTime $deletedAt)
{
$this->deletedAt = $deletedAt;
}
/**
* Get slug
*
* @return string
*/
public function getSlug()
{
return $this->slug;
}
/**
* Set slug
*
* @param string $slug
*/
public function setSlug($slug)
{
$this->slug = $slug;
}
/**
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* @param string $description
*
* @return User
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* @return string
*/
public function getBiography()
{
return $this->biography;
}
/**
* @param string $biography
*
* @return User
*/
public function setBiography($biography)
{
$this->biography = $biography;
return $this;
}
/**
* Get role.
*
* @return mixed|null
*/
public function getRole()
{
$roles = $this->getRoles();
if (count($roles) == 1) {
return array_shift($roles);
}
return null;
}
/**
* @inheritdoc
*/
public function getRoles()
{
$roles = $this->roles;
foreach ($this->getGroups() as $group) {
$roles = array_merge($roles, $group->getRoles());
}
if (!$roles) {
$roles[] = static::ROLE_DEFAULT;
}
return array_unique($roles);
}
/**
* Set role.
*
* @param string $role
*
* @return $this
*/
public function setRole($role)
{
$this->setRoles([$role]);
return $this;
}
/**
* Get allowed sites names.
*
* @return string[]|null
*/
public function getAllowedSites()
{
return $this->allowedSites;
}
/**
* Set allowed sites names.
*
* @param string[]|null $allowedSites
*
* @return User
*/
public function setAllowedSites($allowedSites)
{
$this->allowedSites = $allowedSites;
return $this;
}
/**
* @param UserNote $note
*
* @return User
*/
public function addNote(UserNote $note)
{
if (!$this->notes->contains($note)) {
$note->setUser($this);
$this->notes->add($note);
}
return $this;
}
/**
* @param UserNote $note
*
* @return User
*/
public function removeNote(UserNote $note)
{
$this->notes->removeElement($note);
return $this;
}
/**
* @return string
*/
public function getFbLink()
{
return $this->fbLink;
}
/**
* @param string $fbLink
*
* @return User
*/
public function setFbLink($fbLink)
{
$this->fbLink = $fbLink;
return $this;
}
/**
* @param bool $gues
*
* @return string
*/
public function getGender($gues = false)
{
if (!$this->gender && $gues) {
return preg_match('@[aei]$@', $this->firstName) ? self::GENDER_FEMALE : self::GENDER_MALE;
}
return $this->gender;
}
/**
* @param string $gender
*
* @return User
*/
public function setGender($gender)
{
$this->gender = $gender;
return $this;
}
/**
* @param string $salt
*
* @return User
*/
public function setSalt($salt)
{
$this->salt = $salt;
return $this;
}
/**
* @return string
*/
public function getCurrentPassword()
{
return $this->currentPassword;
}
/**
* @param string $currentPassword
*
* @return User
*/
public function setCurrentPassword($currentPassword)
{
$this->currentPassword = $currentPassword;
return $this;
}
/**
* @return mixed
*/
public function getLastLoginDevice()
{
return $this->lastLoginDevice;
}
/**
* @param mixed $lastLoginDevice
*
* @return User
*/
public function setLastLoginDevice($lastLoginDevice)
{
$this->lastLoginDevice = $lastLoginDevice;
return $this;
}
/**
* @return string
*/
public function getOneSignalId()
{
return $this->oneSignalId;
}
/**
* @param $oneSignalId
*
* @return $this
*/
public function setOneSignalId($oneSignalId)
{
$this->oneSignalId = $oneSignalId;
return $this;
}
public function getAvatar(): ?File
{
return $this->avatar;
}
public function setAvatar(?File $avatar)
{
$this->avatar = $avatar;
return $this;
}
/**
* @return string|null
*/
public function getDefaultLocale()
{
return $this->defaultLocale;
}
/**
* @param string $defaultLocale
*/
public function setDefaultLocale(string $defaultLocale)
{
$this->defaultLocale = $defaultLocale;
}
/**
* Post update method
*
* @param LifecycleEventArgs $args
*
* @throws \Doctrine\ORM\OptimisticLockException
*/
public function processRemovedNotes(LifecycleEventArgs $args)
{
$notes = $this->getNotes();
if ($notes instanceof PersistentCollection) {
$em = $args->getEntityManager();
foreach ($notes->getDeleteDiff() as $removedNote) {
$em->remove($removedNote);
}
if (!empty($removedNote)) {
$em->flush();
}
}
}
/**
* @return ArrayCollection|UserNote[]
*/
public function getNotes()
{
return $this->notes;
}
/**
* Pre remove method
*
* @param LifecycleEventArgs $args
*
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/
public function onPreRemove(LifecycleEventArgs $args)
{
$this->name = $this->getName();
$this->setFirstName(null);
$this->setLastName(null);
$this->setSlug(null);
$this->setEmail(null);
$this->setPassword('');
$this->setEnabled(false);
//$this->setLocked(true);
$em = $args->getEntityManager();
$em->persist($this);
$userConnections = $em->getRepository(UserConnection::class);
$connections = $userConnections->findBy(['user' => $this]);
foreach ($connections as $connection) {
$em->remove($connection);
$em->flush($connections);
}
$em->flush($this);
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name ?: trim($this->getFirstName() . ' ' . $this->getLastName());
}
/**
* Get name
*
* @return string
*/
public function getFullName()
{
$return = $this->name ?: trim($this->getFirstName() . ' ' . $this->getLastName());
if (!empty($this->email)) {
$return .= ' (' . $this->email . ')';
}
return $return;
}
/**
* Get firstName
*
* @return string
*/
public function getFirstName()
{
return $this->firstName;
}
/**
* Set first name
*
* @param string $firstName
*
* @return User
*/
public function setFirstName($firstName)
{
$this->firstName = $firstName;
return $this;
}
/**
* Get lastName
*
* @return string
*/
public function getLastName()
{
return $this->lastName;
}
/**
* Set last name
*
* @param string $lastName
*
* @return User
*/
public function setLastName($lastName)
{
$this->lastName = $lastName;
return $this;
}
public function getApiKey()
{
return $this->apiKey;
}
public function setApiKey($apiKey)
{
$this->apiKey = $apiKey;
}
/**
* @return string
* @magic
*/
public function __toString()
{
return $this->getName();
}
}