src/App/Entity/RecurrentJobProfilePhoto.php line 28

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use DateTime;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use InvalidArgumentException;
  6. use Vich\UploaderBundle\Entity\File;
  7. use Vich\UploaderBundle\Mapping\Annotation as Vich;
  8. /**
  9. * @ORM\Entity()
  10. *
  11. * @ORM\Table(
  12. * name="recurrent_job_profile_photos",
  13. * uniqueConstraints={
  14. *
  15. * @ORM\UniqueConstraint(name="file_name_unique_idx", columns={"file_name"})
  16. * },
  17. * indexes={
  18. *
  19. * @ORM\Index(name="batch_id_idx", columns={"batch_id"}),
  20. * }
  21. * )
  22. *
  23. * @Vich\Uploadable
  24. */
  25. class RecurrentJobProfilePhoto extends UserUploadedFile
  26. {
  27. /**
  28. * @var string
  29. *
  30. * @ORM\GeneratedValue(strategy="CUSTOM")
  31. *
  32. * @ORM\CustomIdGenerator(class="App\Utility\DatabaseIdGenerator")
  33. *
  34. * @ORM\Column(name="id", type="guid")
  35. *
  36. * @ORM\Id
  37. */
  38. protected $id;
  39. /**
  40. * @ORM\ManyToOne(targetEntity="App\Entity\User", cascade={"persist"})
  41. *
  42. * @ORM\JoinColumn(name="users_id", referencedColumnName="id", nullable=false, onDelete="CASCADE")
  43. */
  44. protected User $user;
  45. /**
  46. * @var File|null
  47. *
  48. * @Vich\UploadableField(mapping="recurrent_job_profile_photo", fileNameProperty="fileName")
  49. */
  50. protected $file;
  51. /**
  52. * @var string
  53. *
  54. * @ORM\Column(name="file_name", type="string", length=255, nullable=false)
  55. */
  56. protected $fileName;
  57. /**
  58. * @var string
  59. *
  60. * @ORM\Column(name="original_file_name", type="string", length=255, nullable=false)
  61. */
  62. protected $originalFileName;
  63. /**
  64. * @var string
  65. *
  66. * @ORM\Column(name="mime_type", type="string", length=255, nullable=false)
  67. */
  68. protected $mimeType;
  69. /**
  70. * @var string
  71. *
  72. * @ORM\Column(name="batch_id", type="string", length=64, nullable=false)
  73. */
  74. protected $batchId;
  75. /**
  76. * @var DateTime
  77. *
  78. * @ORM\Column(name="updated_at", type="datetime", nullable=false)
  79. */
  80. protected $updatedAt;
  81. /**
  82. * At the beginning of a recurrentJobProfilePhoto lifecycle, it is not necessarily attached to any
  83. * recurrent job yet. That's because at least in some cases, the file is uploaded by the user while the user
  84. * creates a new recurrent job, which is only created once the recurrent job form is submitted.
  85. *
  86. * @ORM\OneToOne(targetEntity="App\Entity\RecurrentJob", inversedBy="profilePhoto", cascade={"persist"})
  87. *
  88. * @ORM\JoinColumn(name="recurrent_jobs_id", referencedColumnName="id", nullable=true, onDelete="CASCADE")
  89. */
  90. protected ?RecurrentJob $recurrentJob;
  91. public function setRecurrentJob(?RecurrentJob $recurrentJob): void
  92. {
  93. if (!is_null($this->user) && !is_null($recurrentJob)) {
  94. if ($this->user->getId() !== $recurrentJob->getJoboffererProfile()->getUser()->getId()) {
  95. throw new InvalidArgumentException("Recurrent job belongs to user '{$recurrentJob->getJoboffererProfile()->getUser()->getId()}', but file belongs to user '{$this->getUser()->getId()}'.");
  96. }
  97. }
  98. $this->recurrentJob = $recurrentJob;
  99. }
  100. public function getRecurrentJob(): ?RecurrentJob
  101. {
  102. return $this->recurrentJob;
  103. }
  104. }