<?php
namespace App\Entity;
use App\Utils\File\FileServiceTrait;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Validator\Exception\ValidatorException;
/**
* @ORM\Entity(repositoryClass="App\Repository\JobApplyRepository")
* @ORM\Table(name="job_apply",uniqueConstraints={@ORM\UniqueConstraint(name="indx", columns={"job_position_id", "candidate_id"})})
*/
class JobApply implements \JsonSerializable {
use FileServiceTrait;
const FILE_OTHER = "other";
const FILE_CV = "cv";
const FILES = [
self::FILE_OTHER => [
'types' => ['doc', 'docx', 'odt', 'pdf', 'rtf', 'txt', ],
'dir' => '/job-apply',
'size' => 5*1024*1024,
],
self::FILE_CV => [
'types' => ['doc', 'docx', 'odt', 'pdf', 'rtf', 'txt'],
'dir' => '/job-apply',
'size' => 5*1024*1024,
],
];
// const TYPE_PROFILE = 1;
// const TYPE_PROFILE_CV = 2;
// const TYPE_CV = 3;
// const TYPES = [
// self::TYPE_PROFILE => "with profile",
// self::TYPE_PROFILE_CV => "with profile CV",
// self::TYPE_CV => "with uploading CV"
// ];
const TYPE_PROFILE_AND_CV = 1;
const TYPE_CV = 2;
const TYPES = [
self::TYPE_PROFILE_AND_CV => "with profile and/or with profile CV",
self::TYPE_CV => "with uploading CV"
];
const STATUS_NEW = 1;
const STATUS_APPROVED = 2;
const STATUS_NOT_APPROVED = 3;
const STATUS_HIRED = 4;
const STATUSES = [
self::STATUS_NEW => "New",
self::STATUS_APPROVED => "Approved",
self::STATUS_NOT_APPROVED => "Not Approved",
self::STATUS_HIRED => "Hired"
];
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
public $id;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\JobPosition", inversedBy="applicants")
*/
private $jobPosition;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Candidate", inversedBy="applicants")
*/
private $candidate;
/**
* @ORM\Column(type="integer")
*/
private $type;
/**
* @ORM\Column(type="datetime", options={"default": "CURRENT_TIMESTAMP"})
*/
public $createdAt;
/**
* @ORM\Column(type="integer")
*/
private $status;
public function __construct() {
$this->setCreatedAt(new \DateTime());
$this->setStatus(self::STATUS_NEW);
}
/**
* @return int
*/
public function getId(): int {
return $this->id;
}
/**
* @param int $id
*
* @return JobApply
*/
public function setId(int $id): self {
$this->id = $id;
return $this;
}
/**
* @return JobPosition
*/
public function getJobPosition(): ?JobPosition {
return $this->jobPosition;
}
/**
* @param JobPosition $jobPosition
*
* @return JobApply
*/
public function setJobPosition(JobPosition $jobPosition): self {
$this->jobPosition = $jobPosition;
return $this;
}
/**
* @return Candidate
*/
public function getCandidate(): ?Candidate {
return $this->candidate;
}
/**
* @param Candidate $candidate
*
* @return JobApply
*/
public function setCandidate(Candidate $candidate): self {
$this->candidate = $candidate;
return $this;
}
/**
* @return \DateTime
*/
public function getCreatedAt(): \DateTime {
return $this->createdAt;
}
/**
* @param \DateTime $createdAt
*
* @return JobApply
*/
public function setCreatedAt(\DateTime $createdAt): self {
$this->createdAt = $createdAt;
return $this;
}
/**
* @return int|null
*/
public function getType(): ?int
{
return $this->type;
}
/**
* @param int $type
*
* @return JobApply
*/
public function setType(int $type): self
{
if(!array_key_exists($type, self::TYPES)){
throw new ValidatorException("Job Apply type is not valid");
}
$this->type = $type;
return $this;
}
/**
* @return int|null
*/
public function getStatus(): ?int
{
return $this->status;
}
/**
* @param int $status
*
* @return JobApply
*/
public function setStatus(int $status): self
{
if(!array_key_exists($status, self::STATUSES)){
throw new ValidatorException("Job Apply status is not valid");
}
$this->status = $status;
return $this;
}
/**
* @return File|null
*/
public function getCV(){
return $this->getFileService()->getFile($this, self::FILE_CV);
}
/**
* @param UploadedFile|null $file
*
* @return JobApply
*/
public function setCV(?UploadedFile $file): self{
if($file) {
$this->getFileService()->upload($file, $this, self::FILE_CV);
}
return $this;
}
/**
* @return JobApply
*/
public function removeCV(): self{
$file = $this->getFileService()->getFile($this, self::FILE_CV);
if($file) {
$this->getFileService()->delete($file);
}
return $this;
}
/**
* @return File[]|null
*/
public function getOtherFiles(){
return $this->getFileService()->getFiles($this, self::FILE_OTHER);
}
/**
* @param UploadedFile|null $file
*
* @return JobApply
*/
public function addOtherFile(?UploadedFile $file): self{
if($file) {
$this->getFileService()->upload($file, $this, self::FILE_OTHER);
}
return $this;
}
/**
* @return JobApply
*/
public function removeOtherFiles(): self{
$files = $this->getFileService()->getFiles($this, self::FILE_OTHER);
foreach ($files as $file) {
$this->getFileService()->delete($file);
}
return $this;
}
/**
* @param array $files
*
* @return JobApply
*/
public function setOtherFiles(array $files): self{
if($files) {
foreach ($files as $file){
$this->getFileService()->upload($file, $this, self::FILE_OTHER);
}
}
return $this;
}
public function __toString() {
return (string)$this->getJobPosition() . ' - ' . (string)$this->getCandidate();
}
/**
* Specify data which should be serialized to JSON
*
* @link https://php.net/manual/en/jsonserializable.jsonserialize.php
* @return mixed data which can be serialized by <b>json_encode</b>,
* which is a value of any type other than a resource.
* @since 5.4.0
*/
public function jsonSerialize() {
return [
'id' => $this->getId(),
'jobPositionId' => $this->getJobPosition(),
'candidate' => $this->getCandidate(),
'type' => $this->getType(),
'cv' => $this->getCV(),
'otherFiles' => $this->getOtherFiles(),
'createdAt' => $this->getCreatedAt(),
'status' => $this->getStatus()
];
}
}