src/Entity/Employee.php line 19

Open in your IDE?
  1. <?php
  2. /**
  3.  * Copyright (c) 2019, MND Next GmbH - www.mndnext.de
  4.  */
  5. namespace App\Entity;
  6. use App\Entity\User as BaseUser;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  9. use Symfony\Component\Validator\Constraints as Assert;
  10. use Symfony\Component\HttpFoundation\File\UploadedFile;
  11. /**
  12.  * @ORM\Entity
  13.  * @ORM\HasLifecycleCallbacks()
  14.  * @ORM\Table(name="employees")
  15.  */
  16. class Employee extends BaseUser
  17. {
  18.     public const API_OBJECT_NAME 'contact';
  19.     public const ROLE_EMPLOYEE 'ROLE_EMPLOYEE';
  20.     public const ROLE_NEW_EMPLOYEE 'ROLE_NEW_EMPLOYEE';
  21.     public const ROLE_CONTACT 'ROLE_CONTACT';
  22.     public const ROLE_NEW_CONTACT 'ROLE_NEW_CONTACT';
  23.     public const MANDATORY_PROPS = ['firstname''lastname''email''password'];
  24.     /**
  25.      * @ORM\Id
  26.      * @ORM\Column(type="integer")
  27.      * @ORM\GeneratedValue(strategy="AUTO")
  28.      */
  29.     protected $id;
  30.     /**
  31.      * @var string
  32.      * @ORM\Column(name="firstname", type="string", length=255, nullable=true)
  33.      */
  34.     private $firstname;
  35.     /**
  36.      * @var string
  37.      * @ORM\Column(name="lastname", type="string", length=255, nullable=true)
  38.      */
  39.     private $lastname;
  40.     /**
  41.      * @var string
  42.      * @ORM\Column(name="salutation", type="string", length=1, nullable=true)
  43.      */
  44.     private $salutation;
  45.     /**
  46.      * @var string
  47.      * @ORM\Column(name="function", type="string", length=255, nullable=true)
  48.      */
  49.     private $function;
  50.     /**
  51.      * @var string
  52.      * @ORM\Column(name="department", type="string", length=255, nullable=true)
  53.      */
  54.     private $department;
  55.     /**
  56.      * @var string
  57.      * @ORM\Column(name="phone", type="string", length=255, nullable=true)
  58.      */
  59.     private $phone;
  60.     /**
  61.      * @ORM\ManyToOne(targetEntity="Company", inversedBy="employees", cascade={"persist"})
  62.      */
  63.     private $company;
  64.     /**
  65.      * @var bool
  66.      * @ORM\Column(name="contractor", type="boolean", nullable=false, options={"default":false})
  67.      */
  68.     private $contractor false;
  69.     /**
  70.      * @Assert\Regex(
  71.      *  pattern="/^(?=.*?[a-zA-Z])(?=.*?[0-9])(?=.*?[!$%&=?ยง#+*,;-_\/\\]).{8,}$/",
  72.      *  message="Das Passwort muss mindestens 8 Zeichen lang sein, Zahlen, Buchstaben und und mindestens ein Sonderzeichen enthalten."
  73.      * )
  74.      */
  75.     protected $plainPassword;
  76.     public function __construct()
  77.     {
  78.         parent::__construct();
  79.         $this->registrations = new \Doctrine\Common\Collections\ArrayCollection();
  80.     }
  81.     public function setFirstname($firstname)
  82.     {
  83.         $this->firstname $firstname;
  84.         return $this;
  85.     }
  86.     public function getFirstname()
  87.     {
  88.         return $this->firstname;
  89.     }
  90.     public function setLastname($lastname)
  91.     {
  92.         $this->lastname $lastname;
  93.         return $this;
  94.     }
  95.     public function getLastname()
  96.     {
  97.         return $this->lastname;
  98.     }
  99.     public function setSalutation($salutation)
  100.     {
  101.         $this->salutation $salutation;
  102.         return $this;
  103.     }
  104.     public function getSalutation()
  105.     {
  106.         return $this->salutation;
  107.     }
  108.     public function setFunction($function)
  109.     {
  110.         $this->function $function;
  111.         return $this;
  112.     }
  113.     public function getFunction()
  114.     {
  115.         return $this->function;
  116.     }
  117.     public function setDepartment($department)
  118.     {
  119.         $this->department $department;
  120.         return $this;
  121.     }
  122.     public function getDepartment()
  123.     {
  124.         return $this->department;
  125.     }
  126.     public function setPhone($phone)
  127.     {
  128.         $this->phone $phone;
  129.         return $this;
  130.     }
  131.     public function getPhone()
  132.     {
  133.         return $this->phone;
  134.     }
  135.     public function setEmail($email)
  136.     {
  137.         parent::setEmail($email);
  138.         $this->setUsername($email);
  139.     }
  140.     public function getEmail()
  141.     {
  142.         return parent::getEmail();
  143.     }
  144.     public function setCompany(Company $company null)
  145.     {
  146.         $this->company $company;
  147.         return $this;
  148.     }
  149.     public function isContractor()
  150.     {
  151.         $state false;
  152.         if ($this->hasRole(self::ROLE_CONTACT) || $this->hasRole(self::ROLE_NEW_CONTACT)) {
  153.             $state true;
  154.         }
  155.         if ($this->contractor != $state) {
  156.             $this->setContractor($state);
  157.         }
  158.         return $state;
  159.     }
  160.     public function setContractor($value)
  161.     {
  162.         if ($value) {
  163.             if ($this->hasRole(Employee::ROLE_NEW_EMPLOYEE)) {
  164.                 $this->removeRole(Employee::ROLE_NEW_EMPLOYEE);
  165.                 $this->addRole(Employee::ROLE_NEW_CONTACT);
  166.             } elseif ($this->hasRole(Employee::ROLE_EMPLOYEE)) {
  167.                 $this->removeRole(Employee::ROLE_EMPLOYEE);
  168.                 $this->addRole(Employee::ROLE_CONTACT);
  169.             } elseif (!$this->hasRole(Employee::ROLE_NEW_CONTACT) && !$this->hasRole(Employee::ROLE_CONTACT)) {
  170.                 $this->addRole(Employee::ROLE_NEW_CONTACT);
  171.             }
  172.         } else {
  173.             if ($this->hasRole(Employee::ROLE_NEW_CONTACT)) {
  174.                 $this->removeRole(Employee::ROLE_NEW_CONTACT);
  175.                 $this->addRole(Employee::ROLE_NEW_EMPLOYEE);
  176.             } elseif ($this->hasRole(Employee::ROLE_CONTACT)) {
  177.                 $this->removeRole(Employee::ROLE_CONTACT);
  178.                 $this->addRole(Employee::ROLE_EMPLOYEE);
  179.             } elseif (!$this->hasRole(Employee::ROLE_NEW_EMPLOYEE) && !$this->hasRole(Employee::ROLE_EMPLOYEE)) {
  180.                 $this->addRole(Employee::ROLE_NEW_EMPLOYEE);
  181.             }
  182.         }
  183.         $this->contractor $value;
  184.     }
  185.     /**
  186.      * Get company
  187.      *
  188.      * @return Company|null
  189.      */
  190.     public function getCompany()
  191.     {
  192.         return $this->company;
  193.     }
  194.     public function getName()
  195.     {
  196.         return $this->getFirstname() . ' ' $this->getLastname();
  197.     }
  198.     public function __toString()
  199.     {
  200.         return $this->getFirstname() . ' ' $this->getLastname();
  201.     }
  202.     private static function getOptProp($data$prop)
  203.     {
  204.         if (array_key_exists($prop$data)) {
  205.             if (!is_string($data[$prop])) {
  206.                 throw new BadRequestHttpException(sprintf('The key "%s" must be must be a string in "' self::API_OBJECT_NAME .'".'$prop));
  207.             }
  208.             return $data[$prop];
  209.         }
  210.     }
  211.     public static function createFromData($data)
  212.     {
  213.         foreach (self::MANDATORY_PROPS as $prop) {
  214.             if (!array_key_exists($prop$data)) {
  215.                 throw new BadRequestHttpException(sprintf('The key "%s" must be provided in "' self::API_OBJECT_NAME '".'$prop));
  216.             }
  217.         }
  218.         $object = new Employee();
  219.         $object->setFirstname(self::getOptProp($data'firstname'));
  220.         $object->setLastname(self::getOptProp($data'lastname'));
  221.         $object->setSalutation(self::getOptProp($data'salutation'));
  222.         $object->setFunction(self::getOptProp($data'function'));
  223.         $object->setDepartment(self::getOptProp($data'department'));
  224.         $object->setPhone(self::getOptProp($data'phone'));
  225.         $object->setEmail(self::getOptProp($data'email'));
  226.         $object->setPassword(self::getOptProp($data'password'));
  227.         return $object;
  228.     }
  229. }