<?php
declare(strict_types=1);
/**
* This file is part of the Symfony users-bundle package.
* (C) 2019 AVT-Korporacja Sp. z o.o.
*/
namespace App\Entity\User;
use App\Entity\Address\Address;
use App\Entity\Shop\ShopOrder;
use Doctrine\Common\Collections\Collection;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use App\Entity\AbstractEntity;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use DateTime;
use Serializable;
use function strval;
use function json_decode;
use function json_encode;
use function is_null;
use function is_array;
use function array_key_exists;
use function serialize;
use function unserialize;
/**
* @author Andrzej Dunowski <andrzej.dunowski@avt.pl>
* @author Przemysław Chrupek <przemyslaw.chrupek@avt.pl>
*/
/**
* @ORM\Entity(repositoryClass="App\Repository\User\UserRepository")
* @UniqueEntity(
* fields={"email"},
* message="Adres email już istnieje w naszej bazie"
* )
*/
class User extends AbstractEntity implements UserInterface, Serializable, PasswordAuthenticatedUserInterface
{
const ROLE_USER = 'ROLE_USER';
const ROLE_ADMIN = 'ROLE_ADMIN';
const ROLE_SUPER_ADMIN = 'ROLE_SUPER_ADMIN';
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(name="id", type="integer", options={"unsigned": "true"})
*/
private $id;
/**
* @ORM\Column(type="json")
*/
private $roles = [];
/**
* @ORM\Column(name="active", type="boolean", options={"default": 1})
*/
private $active = false;
/**
* @ORM\Column(name="date", type="datetime", options={"default": "CURRENT_TIMESTAMP"})
*/
private $date;
/**
* @var User
*
* @ORM\ManyToOne(targetEntity="App\Entity\User\User")
* @ORM\JoinColumn(name="user", referencedColumnName="id")
*/
private $user;
/**
* @ORM\Column(name="name", length=182, nullable=true)
*/
private $name;
/**
* @ORM\Column(name="lastname", length=182, nullable=true)
*/
private $lastname;
/**
* @ORM\Column(name="email", type="string", length=128, unique=true, options={"default": "NULL"})
*/
private $email;
/**
* @ORM\Column(name="password", type="string", length=128, options={"default": "NULL"})
*/
private $password;
/**
* @ORM\Column(name="code", type="string", length=128, nullable=true, options={"default": "NULL"})
*/
private $code;
/**
* @ORM\Column(name="api_key", type="string", length=32, nullable=true, unique=true, options={"default": "NULL"})
*/
private $apiKey;
/**
* @ORM\Column(name="api_ips", type="text", nullable=true, options={"default": "NULL"})
*/
private $apiIps;
/**
* @ORM\Column(name="params", nullable=true, type="text", options={"default": "NULL"})
*/
private $params;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Shop\Cart", mappedBy="user")
*/
private $carts;
/**
* @ORM\Column(name="date_last_login", type="datetime", nullable=true)
*
* @var DateTime|null
*/
private $dateLastLogin;
/**
*
* @ORM\Column(name="date_last_modified", type="datetime", nullable=true)
*
* @var DateTime|null
*/
private $dateLastModified;
/**
* @ORM\OneToMany(targetEntity=ShopOrder::class, mappedBy="user")
*/
private $shopOrders;
/**
* @ORM\OneToMany(targetEntity=Address::class, mappedBy="user")
*/
private $addresses;
public function __construct()
{
$this->date = new DateTime;
$this->shopOrders = new ArrayCollection();
$this->addresses = new ArrayCollection();
}
public function __toString(): string
{
return strval($this->name);
}
public function getId(): ?int
{
return $this->id;
}
public function setId(?int $id): void
{
$this->id = $id;
}
public function getActive(): ?bool
{
return $this->active;
}
public function setActive(?bool $active): void
{
$this->active = $active;
}
public function getDate(): ?DateTime
{
return $this->date;
}
public function setDate(?DateTime $date): void
{
$this->date = $date;
}
public function getRoles(): array
{
$roles = $this->roles;
return array_unique($roles);
}
public function setRoles(array $roles): void
{
$this->roles = $roles;
}
public function addRole(string $role): void
{
$this->roles[] = $role;
}
public function removeRole(string $role): void
{
$index = array_search($role, $this->roles);
if($index !== FALSE) {
unset($this->roles[$index]);
}
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(User $user): void
{
$this->user = $user;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(?string $name): void
{
$this->name = $name;
}
public function getLastname(): ?string
{
return $this->lastname;
}
public function setLastname(?string $lastname): void
{
$this->lastname = $lastname;
}
public function getName2(): ?string
{
$name = $this->name;
return $name;
}
public function getFullName(): string
{
$name = '';
if (!empty($this->name)) {
$name .= $this->name;
}
if (!empty($this->lastname)) {
if (!empty($this->name)) {
$name .= ' ';
}
$name .= $this->lastname;
}
return $name;
}
public function getUsername(): ?string
{
return $this->email;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(?string $email): void
{
$this->email = $email;
}
public function getSalt()
{
return null;
}
public function getPassword(): ?string
{
return $this->password;
}
public function setPassword(?string $password): void
{
$this->password = $password;
}
public function getCode(): ?string
{
return $this->code;
}
public function setCode(?string $code): void
{
$this->code = $code;
}
public function getApiKey(): ?string
{
return $this->apiKey;
}
public function setApiKey(?string $apiKey): void
{
$this->apiKey = $apiKey;
}
public function getApiIps(): array
{
if (is_null($this->apiIps))
{
return [];
}
$apiIps = json_decode($this->apiIps, true);
return is_array($apiIps) ? $apiIps : [];
}
public function setApiIps(array $apiIps): void
{
$this->apiIps = count($apiIps) === 0 ? null : json_encode($apiIps);
}
public function getParams(): array
{
if (is_null($this->params))
{
return [];
}
$params = json_decode($this->params, true);
return is_array($params) ? $params : [];
}
public function setParams(array $params): void
{
$this->params = count($params) === 0 ? null : json_encode($params);
}
public function setParam(string $name, $value): void
{
$params = $this->getParams();
$params[$name] = $value;
$this->setParams($params);
}
public function getParam(string $param, $default = null)
{
$params = $this->getParams();
if (!is_array($params))
{
return $default;
}
if (array_key_exists($param, $params))
{
return $params[$param];
}
return $default;
}
public function getDateLastLogin(): ?DateTime
{
return $this->dateLastLogin;
}
public function setDateLastLogin(?DateTime $dateLastLogin): void
{
$this->dateLastLogin = $dateLastLogin;
}
public function getDateLastModified(): ?DateTime
{
return $this->dateLastModified;
}
public function setDateLastModified(?DateTime $dateLastModified): void
{
$this->dateLastModified = $dateLastModified;
}
public function eraseCredentials()
{
}
public function serialize(): string
{
return serialize([
$this->id,
$this->email,
$this->password
]);
}
public function unserialize($serialized): void
{
list (
$this->id,
$this->email,
$this->password,
) = unserialize($serialized);
}
public function getDataForApi(): array
{
return [
'id' => $this->id,
'name' => $this->name
];
}
/**
* @return Collection|ShopOrder[]
*/
public function getShopOrders(): Collection
{
return $this->shopOrders;
}
public function addShopOrder(ShopOrder $shopOrder): self
{
if (!$this->shopOrders->contains($shopOrder)) {
$this->shopOrders[] = $shopOrder;
$shopOrder->setUser($this);
}
return $this;
}
public function removeShopOrder(ShopOrder $shopOrder): self
{
if ($this->shopOrders->removeElement($shopOrder)) {
// set the owning side to null (unless already changed)
if ($shopOrder->getUser() === $this) {
$shopOrder->setUser(null);
}
}
return $this;
}
/**
* @return Collection|Address[]
*/
public function getAddresses(): Collection
{
return $this->addresses;
}
public function addAddress(Address $address): self
{
if (!$this->addresses->contains($address)) {
$this->addresses[] = $address;
$address->setUser($this);
}
return $this;
}
public function removeAddress(Address $address): self
{
if ($this->addresses->removeElement($address)) {
// set the owning side to null (unless already changed)
if ($address->getUser() === $this) {
$address->setUser(null);
}
}
return $this;
}
public function getUserIdentifier(): string
{
return $this->email;
}
}