src/Hitso/Bundle/CommonBundle/Entity/User.php line 41

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * @author Maciej Kaczmarek <maciej.kaczmarek@autentika.pl>
  5.  */
  6. namespace Hitso\Bundle\CommonBundle\Entity;
  7. use DateTime;
  8. use Doctrine\Common\Collections\ArrayCollection;
  9. use Doctrine\ORM\Event\LifecycleEventArgs;
  10. use Doctrine\ORM\PersistentCollection;
  11. use FOS\UserBundle\Model\User as BaseUser;
  12. use Hitso\Bundle\CommonBundle\Annotation\Loggable;
  13. use Hitso\Bundle\CommonBundle\Doctrine\Behaviours\Identifiable;
  14. use Hitso\Bundle\CommonBundle\Interfaces\Timestampable;
  15. use Hitso\Bundle\CommonBundle\Traits\TimestampableTrait;
  16. use Hitso\Bundle\FileManagerBundle\Entity\File;
  17. use Hitso\Extra\CommonBundle\Entity\UserExtraTrait;
  18. use Hitso\Extra\CommonBundle\Entity\UserExtraInterface;
  19. use Symfony\Component\Serializer\Annotation as Serializer;
  20. /**
  21.  * Class User
  22.  *
  23.  * @package Hitso\Bundle\CommonBundle\Entity
  24.  *
  25.  * @Loggable(
  26.  *     createMsg={"Utworzono konto użytkownika", "%user% utworzył(|a) nowego użytkownika %label%"},
  27.  *     updateMsg={"Aktualizacja konta użytkownika", "%user% zaktualizował(|a) konto użytkownika %label%"},
  28.  *     removeMsg={"Usunięcie konta użytkownika", "%user% usun(ął|ęła) konto użytkownika %label%"},
  29.  *     routeName="hitso_admin_users_edit",
  30.  *     sites={},
  31.  *     labelSource="name",
  32.  *     ignoredFields={"lastLogin","updatedAt"},
  33.  *     saveChangeSet=true
  34.  * )
  35.  *
  36.  * @codeCoverageIgnore
  37.  */
  38. class User extends BaseUser implements UserExtraInterfaceEntityInterfaceTimestampable
  39. {
  40.     const GENDER_MALE   'm';
  41.     const GENDER_FEMALE 'f';
  42.     use Identifiable;
  43.     use UserExtraTrait;
  44.     use TimestampableTrait;
  45.     /**
  46.      * @var string
  47.      * @Serializer\Groups({"WS"})
  48.      */
  49.     protected $firstName;
  50.     /**
  51.      * @var string
  52.      */
  53.     protected $lastName;
  54.     /**
  55.      * @var string
  56.      */
  57.     protected $slug;
  58.     /**
  59.      * @var string
  60.      */
  61.     protected $description;
  62.     /**
  63.      * @var string
  64.      */
  65.     protected $biography;
  66.     /**
  67.      * @var DateTime
  68.      */
  69.     protected $deletedAt;
  70.     /**
  71.      * @var string[]
  72.      */
  73.     protected $allowedSites;
  74.     /**
  75.      * @var ArrayCollection|UserNote[]
  76.      */
  77.     protected $notes;
  78.     /**
  79.      * @var string
  80.      */
  81.     protected $fbLink;
  82.     /**
  83.      * @var string
  84.      */
  85.     protected $gender;
  86.     /**
  87.      * One Signal User Id (notificaton push service)
  88.      *
  89.      * @var string
  90.      */
  91.     protected $oneSignalId;
  92.     /**
  93.      * Not mapped.
  94.      *
  95.      * @var string
  96.      * @Serializer\Groups({"WS"})
  97.      */
  98.     protected $name;
  99.     /**
  100.      * Not mapped.
  101.      *
  102.      * @var string
  103.      */
  104.     protected $currentPassword;
  105.     /**
  106.      * Not mapped.
  107.      *
  108.      * @var string
  109.      */
  110.     protected $fullName;
  111.     /**
  112.      * @var string
  113.      */
  114.     protected $lastLoginDevice;
  115.     /**
  116.      * @var File
  117.      */
  118.     protected $avatar;
  119.     /**
  120.      * @var string
  121.      */
  122.     protected $defaultLocale;
  123.     /**
  124.      * @var string
  125.      */
  126.     protected $apiKey '';
  127.     /**
  128.      * User constructor.
  129.      *
  130.      * @param null $id
  131.      */
  132.     public function __construct($id null)
  133.     {
  134.         parent::__construct();
  135.         $this->id           $id;
  136.         $this->roles        = ['ROLE_USER'];
  137.         $this->allowedSites = [];
  138.         $this->notes        = new ArrayCollection();
  139.     }
  140.     /**
  141.      * @param int $id
  142.      *
  143.      * @return User
  144.      */
  145.     public function setId($id)
  146.     {
  147.         $this->id $id;
  148.         return $this;
  149.     }
  150.     /**
  151.      * Get date of deletion
  152.      *
  153.      * @return DateTime
  154.      */
  155.     public function getDeletedAt()
  156.     {
  157.         return $this->deletedAt;
  158.     }
  159.     /**
  160.      * Set date of deletion
  161.      *
  162.      * @param DateTime $deletedAt
  163.      */
  164.     public function setDeletedAt(DateTime $deletedAt)
  165.     {
  166.         $this->deletedAt $deletedAt;
  167.     }
  168.     /**
  169.      * Get slug
  170.      *
  171.      * @return string
  172.      */
  173.     public function getSlug()
  174.     {
  175.         return $this->slug;
  176.     }
  177.     /**
  178.      * Set slug
  179.      *
  180.      * @param string $slug
  181.      */
  182.     public function setSlug($slug)
  183.     {
  184.         $this->slug $slug;
  185.     }
  186.     /**
  187.      * @return string
  188.      */
  189.     public function getDescription()
  190.     {
  191.         return $this->description;
  192.     }
  193.     /**
  194.      * @param string $description
  195.      *
  196.      * @return User
  197.      */
  198.     public function setDescription($description)
  199.     {
  200.         $this->description $description;
  201.         return $this;
  202.     }
  203.     /**
  204.      * @return string
  205.      */
  206.     public function getBiography()
  207.     {
  208.         return $this->biography;
  209.     }
  210.     /**
  211.      * @param string $biography
  212.      *
  213.      * @return User
  214.      */
  215.     public function setBiography($biography)
  216.     {
  217.         $this->biography $biography;
  218.         return $this;
  219.     }
  220.     /**
  221.      * Get role.
  222.      *
  223.      * @return mixed|null
  224.      */
  225.     public function getRole()
  226.     {
  227.         $roles $this->getRoles();
  228.         if (count($roles) == 1) {
  229.             return array_shift($roles);
  230.         }
  231.         return null;
  232.     }
  233.     /**
  234.      * @inheritdoc
  235.      */
  236.     public function getRoles()
  237.     {
  238.         $roles $this->roles;
  239.         foreach ($this->getGroups() as $group) {
  240.             $roles array_merge($roles$group->getRoles());
  241.         }
  242.         if (!$roles) {
  243.             $roles[] = static::ROLE_DEFAULT;
  244.         }
  245.         return array_unique($roles);
  246.     }
  247.     /**
  248.      * Set role.
  249.      *
  250.      * @param string $role
  251.      *
  252.      * @return $this
  253.      */
  254.     public function setRole($role)
  255.     {
  256.         $this->setRoles([$role]);
  257.         return $this;
  258.     }
  259.     /**
  260.      * Get allowed sites names.
  261.      *
  262.      * @return string[]|null
  263.      */
  264.     public function getAllowedSites()
  265.     {
  266.         return $this->allowedSites;
  267.     }
  268.     /**
  269.      * Set allowed sites names.
  270.      *
  271.      * @param string[]|null $allowedSites
  272.      *
  273.      * @return User
  274.      */
  275.     public function setAllowedSites($allowedSites)
  276.     {
  277.         $this->allowedSites $allowedSites;
  278.         return $this;
  279.     }
  280.     /**
  281.      * @param UserNote $note
  282.      *
  283.      * @return User
  284.      */
  285.     public function addNote(UserNote $note)
  286.     {
  287.         if (!$this->notes->contains($note)) {
  288.             $note->setUser($this);
  289.             $this->notes->add($note);
  290.         }
  291.         return $this;
  292.     }
  293.     /**
  294.      * @param UserNote $note
  295.      *
  296.      * @return User
  297.      */
  298.     public function removeNote(UserNote $note)
  299.     {
  300.         $this->notes->removeElement($note);
  301.         return $this;
  302.     }
  303.     /**
  304.      * @return string
  305.      */
  306.     public function getFbLink()
  307.     {
  308.         return $this->fbLink;
  309.     }
  310.     /**
  311.      * @param string $fbLink
  312.      *
  313.      * @return User
  314.      */
  315.     public function setFbLink($fbLink)
  316.     {
  317.         $this->fbLink $fbLink;
  318.         return $this;
  319.     }
  320.     /**
  321.      * @param bool $gues
  322.      *
  323.      * @return string
  324.      */
  325.     public function getGender($gues false)
  326.     {
  327.         if (!$this->gender && $gues) {
  328.             return preg_match('@[aei]$@'$this->firstName) ? self::GENDER_FEMALE self::GENDER_MALE;
  329.         }
  330.         return $this->gender;
  331.     }
  332.     /**
  333.      * @param string $gender
  334.      *
  335.      * @return User
  336.      */
  337.     public function setGender($gender)
  338.     {
  339.         $this->gender $gender;
  340.         return $this;
  341.     }
  342.     /**
  343.      * @param string $salt
  344.      *
  345.      * @return User
  346.      */
  347.     public function setSalt($salt)
  348.     {
  349.         $this->salt $salt;
  350.         return $this;
  351.     }
  352.     /**
  353.      * @return string
  354.      */
  355.     public function getCurrentPassword()
  356.     {
  357.         return $this->currentPassword;
  358.     }
  359.     /**
  360.      * @param string $currentPassword
  361.      *
  362.      * @return User
  363.      */
  364.     public function setCurrentPassword($currentPassword)
  365.     {
  366.         $this->currentPassword $currentPassword;
  367.         return $this;
  368.     }
  369.     /**
  370.      * @return mixed
  371.      */
  372.     public function getLastLoginDevice()
  373.     {
  374.         return $this->lastLoginDevice;
  375.     }
  376.     /**
  377.      * @param mixed $lastLoginDevice
  378.      *
  379.      * @return User
  380.      */
  381.     public function setLastLoginDevice($lastLoginDevice)
  382.     {
  383.         $this->lastLoginDevice $lastLoginDevice;
  384.         return $this;
  385.     }
  386.     /**
  387.      * @return string
  388.      */
  389.     public function getOneSignalId()
  390.     {
  391.         return $this->oneSignalId;
  392.     }
  393.     /**
  394.      * @param $oneSignalId
  395.      *
  396.      * @return $this
  397.      */
  398.     public function setOneSignalId($oneSignalId)
  399.     {
  400.         $this->oneSignalId $oneSignalId;
  401.         return $this;
  402.     }
  403.     public function getAvatar(): ?File
  404.     {
  405.         return $this->avatar;
  406.     }
  407.     public function setAvatar(?File $avatar)
  408.     {
  409.         $this->avatar $avatar;
  410.         return $this;
  411.     }
  412.     /**
  413.      * @return string|null
  414.      */
  415.     public function getDefaultLocale()
  416.     {
  417.         return $this->defaultLocale;
  418.     }
  419.     /**
  420.      * @param string $defaultLocale
  421.      */
  422.     public function setDefaultLocale(string $defaultLocale)
  423.     {
  424.         $this->defaultLocale $defaultLocale;
  425.     }
  426.     /**
  427.      * Post update method
  428.      *
  429.      * @param LifecycleEventArgs $args
  430.      *
  431.      * @throws \Doctrine\ORM\OptimisticLockException
  432.      */
  433.     public function processRemovedNotes(LifecycleEventArgs $args)
  434.     {
  435.         $notes $this->getNotes();
  436.         if ($notes instanceof PersistentCollection) {
  437.             $em $args->getEntityManager();
  438.             foreach ($notes->getDeleteDiff() as $removedNote) {
  439.                 $em->remove($removedNote);
  440.             }
  441.             if (!empty($removedNote)) {
  442.                 $em->flush();
  443.             }
  444.         }
  445.     }
  446.     /**
  447.      * @return ArrayCollection|UserNote[]
  448.      */
  449.     public function getNotes()
  450.     {
  451.         return $this->notes;
  452.     }
  453.     /**
  454.      * Pre remove method
  455.      *
  456.      * @param LifecycleEventArgs $args
  457.      *
  458.      * @throws \Doctrine\ORM\ORMException
  459.      * @throws \Doctrine\ORM\OptimisticLockException
  460.      */
  461.     public function onPreRemove(LifecycleEventArgs $args)
  462.     {
  463.         $this->name $this->getName();
  464.         $this->setFirstName(null);
  465.         $this->setLastName(null);
  466.         $this->setSlug(null);
  467.         $this->setEmail(null);
  468.         $this->setPassword('');
  469.         $this->setEnabled(false);
  470.         //$this->setLocked(true);
  471.         $em $args->getEntityManager();
  472.         $em->persist($this);
  473.         $userConnections $em->getRepository(UserConnection::class);
  474.         $connections     $userConnections->findBy(['user' => $this]);
  475.         foreach ($connections as $connection) {
  476.             $em->remove($connection);
  477.             $em->flush($connections);
  478.         }
  479.         $em->flush($this);
  480.     }
  481.     /**
  482.      * Get name
  483.      *
  484.      * @return string
  485.      */
  486.     public function getName()
  487.     {
  488.         return $this->name ?: trim($this->getFirstName() . ' ' $this->getLastName());
  489.     }
  490.     /**
  491.      * Get name
  492.      *
  493.      * @return string
  494.      */
  495.     public function getFullName()
  496.     {
  497.         $return $this->name ?: trim($this->getFirstName() . ' ' $this->getLastName());
  498.         if (!empty($this->email)) {
  499.             $return .= ' (' $this->email ')';
  500.         }
  501.         return $return;
  502.     }
  503.     /**
  504.      * Get firstName
  505.      *
  506.      * @return string
  507.      */
  508.     public function getFirstName()
  509.     {
  510.         return $this->firstName;
  511.     }
  512.     /**
  513.      * Set first name
  514.      *
  515.      * @param string $firstName
  516.      *
  517.      * @return User
  518.      */
  519.     public function setFirstName($firstName)
  520.     {
  521.         $this->firstName $firstName;
  522.         return $this;
  523.     }
  524.     /**
  525.      * Get lastName
  526.      *
  527.      * @return string
  528.      */
  529.     public function getLastName()
  530.     {
  531.         return $this->lastName;
  532.     }
  533.     /**
  534.      * Set last name
  535.      *
  536.      * @param string $lastName
  537.      *
  538.      * @return User
  539.      */
  540.     public function setLastName($lastName)
  541.     {
  542.         $this->lastName $lastName;
  543.         return $this;
  544.     }
  545.     public function getApiKey()
  546.     {
  547.         return $this->apiKey;
  548.     }
  549.     public function setApiKey($apiKey)
  550.     {
  551.         $this->apiKey $apiKey;
  552.     }
  553.     /**
  554.      * @return string
  555.      * @magic
  556.      */
  557.     public function __toString()
  558.     {
  559.         return $this->getName();
  560.     }
  561. }