Invis1ble/assistant

View on GitHub
src/AppBundle/Entity/User.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php
 
namespace AppBundle\Entity;
 
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
use Ramsey\Uuid\Uuid;
 
use AppBundle\Security\UserInterface;
 
use Utils\Entity\CreatedAtTrait;
 
/**
* User
*
* @author Max Invis1ble
* @copyright (c) 2016, Max Invis1ble
* @license http://www.opensource.org/licenses/mit-license.php MIT
*
* @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
*/
class User implements UserInterface
{
use CreatedAtTrait;
 
const ROLE_DEFAULT = 'ROLE_USER';
/**
* @var Uuid
*
* @ORM\Id
* @ORM\Column(type="uuid")
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidGenerator")
*/
protected $id;
 
/**
* @var string
*
* @ORM\Column(type="string", length=255, unique=true)
*/
protected $username;
 
/**
* @var string
*/
protected $plainPassword;
 
/**
* @var string
* @ORM\Column(type="string")
*/
protected $password;
 
/**
* @var string
* @ORM\Column(type="string")
*/
protected $salt;
 
/**
* @var array
*
* @ORM\Column(type="array")
*/
protected $roles;
 
/**
* @var Collection
*
* @ORM\OneToMany(targetEntity="Category", mappedBy="user", cascade={"remove"})
*/
protected $categories;
 
public function __construct()
{
$this->setSalt(base_convert(sha1(uniqid(mt_rand(), true)), 16, 36));
$this->setRoles([]);
$this->categories = new ArrayCollection();
}
 
Avoid variables with short names like $id. Configured minimum length is 3.
public function setId(string $id)
{
$this->id = $id;
}
 
/**
* @return Uuid
*/
public function getId(): Uuid
{
return $this->id;
}
 
/**
* @param Category $category
*
* @return User
*/
public function addCategory(Category $category): User
{
$this->getCategories()->add($category);
 
return $this;
}
 
/**
* @param Category $category
*
* @return User
*/
public function removeCategory(Category $category): User
{
$this->getCategories()->removeElement($category);
 
return $this;
}
 
/**
* @return Collection
*/
public function getCategories(): Collection
{
return $this->categories;
}
 
/**
* @return string|null
*/
public function getUsername()
{
return $this->username;
}
 
/**
* @param string $username
*
* @return User
*/
public function setUsername(string $username): User
{
$this->username = $username;
 
return $this;
}
 
/**
* @return string|null
*/
public function getPlainPassword()
{
return $this->plainPassword;
}
 
/**
* @param string|null $plainPassword
*
* @return User
*/
public function setPlainPassword(string $plainPassword = null): User
{
$this->plainPassword = $plainPassword;
 
return $this;
}
 
/**
* @return string
*/
public function getPassword(): string
{
return $this->password;
}
 
/**
* @param string $password
*
* @return User
*/
public function setPassword(string $password): User
{
$this->password = $password;
 
return $this;
}
 
/**
* @return string
*/
public function getSalt(): string
{
return $this->salt;
}
 
/**
* @param string $salt
*
* @return User
*/
public function setSalt(string $salt): User
{
$this->salt = $salt;
 
return $this;
}
 
/**
* @return string[]
*/
public function getRoles(): array
{
$roles = $this->roles;
 
// we need to make sure to have at least one role
$roles[] = static::ROLE_DEFAULT;
 
return array_unique($roles);
}
 
/**
* @param string[] $roles
*
* @return User
*/
public function setRoles(array $roles): User
{
$this->roles = [];
 
foreach ($roles as $role) {
$this->addRole($role);
}
 
return $this;
}
 
/**
* @param string $role
*
* @return User
*/
public function addRole(string $role): User
{
$role = strtoupper($role);
 
if ($role === static::ROLE_DEFAULT) {
return $this;
}
 
if (!in_array($role, $this->roles, true)) {
$this->roles[] = $role;
}
 
return $this;
}
 
/**
* @param string $role
*
* @return User
*/
Avoid assigning values to variables in if clauses and the like (line '267', column '23').
public function removeRole(string $role): User
{
if (false !== $key = array_search(strtoupper($role), $this->roles, true)) {
unset($this->roles[$key]);
$this->roles = array_values($this->roles);
}
 
return $this;
}
 
/**
* @param string $role
*
* @return bool
*/
public function hasRole(string $role): bool
{
return in_array(strtoupper($role), $this->getRoles(), true);
}
 
/**
* Removes sensitive data from the user.
*
* This is important if, at any given point, sensitive information like
* the plain-text password is stored on this object.
*/
public function eraseCredentials()
{
$this->setPlainPassword(null);
}
}