<?php
/**
* Copyright (c) 2019, MND Next GmbH - www.mndnext.de
*/
namespace App\Entity;
use App\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* @ORM\Entity
* @ORM\HasLifecycleCallbacks()
* @ORM\Table(name="employees")
*/
class Employee extends BaseUser
{
public const API_OBJECT_NAME = 'contact';
public const ROLE_EMPLOYEE = 'ROLE_EMPLOYEE';
public const ROLE_NEW_EMPLOYEE = 'ROLE_NEW_EMPLOYEE';
public const ROLE_CONTACT = 'ROLE_CONTACT';
public const ROLE_NEW_CONTACT = 'ROLE_NEW_CONTACT';
public const MANDATORY_PROPS = ['firstname', 'lastname', 'email', 'password'];
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string
* @ORM\Column(name="firstname", type="string", length=255, nullable=true)
*/
private $firstname;
/**
* @var string
* @ORM\Column(name="lastname", type="string", length=255, nullable=true)
*/
private $lastname;
/**
* @var string
* @ORM\Column(name="salutation", type="string", length=1, nullable=true)
*/
private $salutation;
/**
* @var string
* @ORM\Column(name="function", type="string", length=255, nullable=true)
*/
private $function;
/**
* @var string
* @ORM\Column(name="department", type="string", length=255, nullable=true)
*/
private $department;
/**
* @var string
* @ORM\Column(name="phone", type="string", length=255, nullable=true)
*/
private $phone;
/**
* @ORM\ManyToOne(targetEntity="Company", inversedBy="employees", cascade={"persist"})
*/
private $company;
/**
* @var bool
* @ORM\Column(name="contractor", type="boolean", nullable=false, options={"default":false})
*/
private $contractor = false;
/**
* @Assert\Regex(
* pattern="/^(?=.*?[a-zA-Z])(?=.*?[0-9])(?=.*?[!$%&=?ยง#+*,;-_\/\\]).{8,}$/",
* message="Das Passwort muss mindestens 8 Zeichen lang sein, Zahlen, Buchstaben und und mindestens ein Sonderzeichen enthalten."
* )
*/
protected $plainPassword;
public function __construct()
{
parent::__construct();
$this->registrations = new \Doctrine\Common\Collections\ArrayCollection();
}
public function setFirstname($firstname)
{
$this->firstname = $firstname;
return $this;
}
public function getFirstname()
{
return $this->firstname;
}
public function setLastname($lastname)
{
$this->lastname = $lastname;
return $this;
}
public function getLastname()
{
return $this->lastname;
}
public function setSalutation($salutation)
{
$this->salutation = $salutation;
return $this;
}
public function getSalutation()
{
return $this->salutation;
}
public function setFunction($function)
{
$this->function = $function;
return $this;
}
public function getFunction()
{
return $this->function;
}
public function setDepartment($department)
{
$this->department = $department;
return $this;
}
public function getDepartment()
{
return $this->department;
}
public function setPhone($phone)
{
$this->phone = $phone;
return $this;
}
public function getPhone()
{
return $this->phone;
}
public function setEmail($email)
{
parent::setEmail($email);
$this->setUsername($email);
}
public function getEmail()
{
return parent::getEmail();
}
public function setCompany(Company $company = null)
{
$this->company = $company;
return $this;
}
public function isContractor()
{
$state = false;
if ($this->hasRole(self::ROLE_CONTACT) || $this->hasRole(self::ROLE_NEW_CONTACT)) {
$state = true;
}
if ($this->contractor != $state) {
$this->setContractor($state);
}
return $state;
}
public function setContractor($value)
{
if ($value) {
if ($this->hasRole(Employee::ROLE_NEW_EMPLOYEE)) {
$this->removeRole(Employee::ROLE_NEW_EMPLOYEE);
$this->addRole(Employee::ROLE_NEW_CONTACT);
} elseif ($this->hasRole(Employee::ROLE_EMPLOYEE)) {
$this->removeRole(Employee::ROLE_EMPLOYEE);
$this->addRole(Employee::ROLE_CONTACT);
} elseif (!$this->hasRole(Employee::ROLE_NEW_CONTACT) && !$this->hasRole(Employee::ROLE_CONTACT)) {
$this->addRole(Employee::ROLE_NEW_CONTACT);
}
} else {
if ($this->hasRole(Employee::ROLE_NEW_CONTACT)) {
$this->removeRole(Employee::ROLE_NEW_CONTACT);
$this->addRole(Employee::ROLE_NEW_EMPLOYEE);
} elseif ($this->hasRole(Employee::ROLE_CONTACT)) {
$this->removeRole(Employee::ROLE_CONTACT);
$this->addRole(Employee::ROLE_EMPLOYEE);
} elseif (!$this->hasRole(Employee::ROLE_NEW_EMPLOYEE) && !$this->hasRole(Employee::ROLE_EMPLOYEE)) {
$this->addRole(Employee::ROLE_NEW_EMPLOYEE);
}
}
$this->contractor = $value;
}
/**
* Get company
*
* @return Company|null
*/
public function getCompany()
{
return $this->company;
}
public function getName()
{
return $this->getFirstname() . ' ' . $this->getLastname();
}
public function __toString()
{
return $this->getFirstname() . ' ' . $this->getLastname();
}
private static function getOptProp($data, $prop)
{
if (array_key_exists($prop, $data)) {
if (!is_string($data[$prop])) {
throw new BadRequestHttpException(sprintf('The key "%s" must be must be a string in "' . self::API_OBJECT_NAME .'".', $prop));
}
return $data[$prop];
}
}
public static function createFromData($data)
{
foreach (self::MANDATORY_PROPS as $prop) {
if (!array_key_exists($prop, $data)) {
throw new BadRequestHttpException(sprintf('The key "%s" must be provided in "' . self::API_OBJECT_NAME . '".', $prop));
}
}
$object = new Employee();
$object->setFirstname(self::getOptProp($data, 'firstname'));
$object->setLastname(self::getOptProp($data, 'lastname'));
$object->setSalutation(self::getOptProp($data, 'salutation'));
$object->setFunction(self::getOptProp($data, 'function'));
$object->setDepartment(self::getOptProp($data, 'department'));
$object->setPhone(self::getOptProp($data, 'phone'));
$object->setEmail(self::getOptProp($data, 'email'));
$object->setPassword(self::getOptProp($data, 'password'));
return $object;
}
}