<?php
namespace App\Entity;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use FOS\UserBundle\Model\User as BaseUser;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\Validator\Constraints as Assert;
/**
* User
*
* @ORM\Table(name="user")
* @ORM\Entity
*
* @UniqueEntity("email", message="W systemie już istnieje użytkownik o takim adresie e-mail")
* @UniqueEntity("username", message="W systemie już istnieje użytkownik o takiej nazwie.")
*/
class User extends BaseUser
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @Assert\NotBlank()
*/
protected $username;
/**
* @Assert\NotBlank()
* @Assert\Email()
*/
protected $email;
/**
* @var Company
*
* @Assert\NotNull()
*
* @ORM\ManyToOne(targetEntity="Company", inversedBy="users")
* @ORM\JoinColumn(name="company_id", referencedColumnName="id", nullable=true, onDelete="CASCADE")
*/
private $company;
/**
* @var bool
*
* @ORM\Column(name="help", type="boolean", nullable=true)
*/
private $help = true;
/**
* @var bool
*
* @ORM\Column(name="reload_map", type="boolean", nullable=true)
*/
private $reloadMap = false;
/**
* @var ArrayCollection
*
* @ORM\OneToMany(targetEntity="CableConnectionAction", mappedBy="user")
*/
private $actions;
/**
* @var ArrayCollection
*
* * @ORM\OneToMany(targetEntity="Payment", mappedBy="user")
*/
protected $payments;
/**
* @var \DateTime
*
* @ORM\Column(name="created_at", type="datetime")
*/
private $createdAt;
/**
* @var \DateTime
*
* @Gedmo\Timestampable(on="update")
*
* @ORM\Column(name="updated_at", type="datetime")
*/
private $updatedAt;
public function __construct()
{
parent::__construct();
$this->createdAt = new \DateTime();
$this->actions = new ArrayCollection();
$this->payments = new ArrayCollection();
}
/**
* @Assert\Callback
*/
public function validate(ExecutionContextInterface $context): void
{
if ($this->plainPassword) {
$chars = [];
foreach (str_split($this->plainPassword) as $ch) {
if (!in_array($ch, $chars)) {
$chars[] = $ch;
}
}
if (count($chars) < 10) {
$context->buildViolation('Hasło musi zawierać conajmniej 10 różnych znaków')
->atPath('plainPassword')
->addViolation();
}
}
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set createdAt
*
* @param \DateTime $createdAt
* @return User
*/
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
return $this;
}
/**
* Get createdAt
*
* @return \DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Set updatedAt
*
* @param \DateTime $updatedAt
* @return User
*/
public function setUpdatedAt($updatedAt)
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* Get updatedAt
*
* @return \DateTime
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
/**
* Set company
*
* @param Company $company
* @return User
*/
public function setCompany(Company $company = null)
{
$this->company = $company;
return $this;
}
/**
* Get company
*
* @return Company
*/
public function getCompany()
{
return $this->company;
}
/**
* Add actions
*
* @param CableConnectionAction $actions
* @return User
*/
public function addAction(CableConnectionAction $actions)
{
$this->actions[] = $actions;
return $this;
}
/**
* Remove actions
*
* @param CableConnectionAction $actions
*/
public function removeAction(CableConnectionAction $actions): void
{
$this->actions->removeElement($actions);
}
/**
* Get actions
*
* @return Collection
*/
public function getActions()
{
return $this->actions;
}
/**
* Set reloadMap
*
* @param boolean $reloadMap
* @return User
*/
public function setReloadMap($reloadMap)
{
$this->reloadMap = $reloadMap;
return $this;
}
/**
* Get reloadMap
*
* @return boolean
*/
public function getReloadMap()
{
return $this->reloadMap;
}
/**
* {@inheritdoc}
*
* @return static
*/
public function setPlainPassword($password)
{
$this->updatedAt = new \DateTime();
return parent::setPlainPassword($password);
}
/**
* Set help
*
* @param boolean $help
* @return User
*/
public function setHelp($help)
{
$this->help = $help;
return $this;
}
/**
* Get help
*
* @return boolean
*/
public function getHelp()
{
return $this->help;
}
public function getRoles(): array
{
$roles = parent::getRoles();
if (!$company = $this->getCompany()) {
return $roles;
}
foreach ($company->getGroups() as $group) {
$roles = array_merge($roles, $group->getRoles());
}
return array_unique($roles);
}
public function getLocale(): string
{
return $this->getCompany()->getLanguage();
}
/**
* Add payment.
*
* @param Payment $payment
*
* @return User
*/
public function addPayment(Payment $payment)
{
$this->payments[] = $payment;
return $this;
}
/**
* Remove payment.
*
* @param Payment $payment
*
* @return boolean TRUE if this collection contained the specified element, FALSE otherwise.
*/
public function removePayment(Payment $payment)
{
return $this->payments->removeElement($payment);
}
/**
* Get payments.
*
* @return Collection
*/
public function getPayments()
{
return $this->payments;
}
}