src/App/Entity/ProfileReview.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Entity\Profile\JoboffererProfile;
  4. use App\Entity\Profile\JobseekerProfile;
  5. use App\Utility\GuidUtility;
  6. use DateTime;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Symfony\Component\Validator\Constraints as Assert;
  9. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  10. /**
  11. * @ORM\Entity
  12. *
  13. * @ORM\Table(
  14. * name="profile_reviews",
  15. * uniqueConstraints={
  16. *
  17. * @ORM\UniqueConstraint(name="jobseeker_profiles_id_jobofferer_profiles_id_reviewer_unique_idx", columns={"jobseeker_profiles_id", "jobofferer_profiles_id", "reviewer"})
  18. * }
  19. * )
  20. *
  21. * The unique constraint ensures only one review from a jobseeker to a jobofferer or vice versa.
  22. */
  23. class ProfileReview
  24. {
  25. public const RATING_POSITIVE = 1;
  26. public const RATING_NEGATIVE = -1;
  27. public const POSSIBLE_RATINGS_AVAILABLE_FOR_SELECTION_WITH_TRANSLATION_MAPPING = [
  28. 'reviews.rating_form_label_positive' => self::RATING_POSITIVE,
  29. 'reviews.rating_form_label_negative' => self::RATING_NEGATIVE
  30. ];
  31. public const REVIEWER_JOBSEEKER = 0;
  32. public const REVIEWER_JOBOFFERER = 1;
  33. /**
  34. * @var string
  35. *
  36. * @ORM\GeneratedValue(strategy="CUSTOM")
  37. *
  38. * @ORM\CustomIdGenerator(class="App\Utility\DatabaseIdGenerator")
  39. *
  40. * @ORM\Column(name="id", type="guid")
  41. *
  42. * @ORM\Id
  43. */
  44. protected $id;
  45. /**
  46. * @var JobseekerProfile
  47. *
  48. * @ORM\ManyToOne(targetEntity="App\Entity\Profile\JobseekerProfile", inversedBy="profileReviews", cascade={"persist"})
  49. *
  50. * @ORM\JoinColumn(name="jobseeker_profiles_id", referencedColumnName="id", nullable=false, onDelete="CASCADE")
  51. */
  52. protected $jobseekerProfile;
  53. /**
  54. * @var JoboffererProfile
  55. *
  56. * @ORM\ManyToOne(targetEntity="App\Entity\Profile\JoboffererProfile", inversedBy="profileReviews", cascade={"persist"})
  57. *
  58. * @ORM\JoinColumn(name="jobofferer_profiles_id", referencedColumnName="id", nullable=false, onDelete="CASCADE")
  59. */
  60. protected $joboffererProfile;
  61. /**
  62. * @var int
  63. *
  64. * @ORM\Column(name="reviewer", type="smallint", nullable=false)
  65. */
  66. protected $reviewer;
  67. /**
  68. * @var int
  69. *
  70. * @ORM\Column(name="rating", type="smallint", nullable=false)
  71. */
  72. protected $rating;
  73. /**
  74. * @var string
  75. *
  76. * @ORM\Column(name="feedback", type="text", length=4096, nullable=true)
  77. */
  78. protected $feedback;
  79. /**
  80. * @var DateTime
  81. *
  82. * @ORM\Column(name="created_at", type="datetime", nullable=false)
  83. */
  84. protected $createdAt;
  85. /**
  86. * @var bool
  87. *
  88. * @ORM\Column(name="is_activated_by_admin", type="boolean", nullable=false)
  89. */
  90. protected $isActivatedByAdmin;
  91. public function __construct()
  92. {
  93. $this->setIsActivatedByAdmin(false);
  94. }
  95. public function isReviewer(Profile $profile)
  96. {
  97. if (($this->reviewer === self::REVIEWER_JOBSEEKER
  98. && $profile instanceof JobseekerProfile
  99. && $profile->getId() === $this->getJobseekerProfile()->getId())
  100. || ($this->reviewer === self::REVIEWER_JOBOFFERER
  101. && $profile instanceof JoboffererProfile
  102. && $profile->getId() === $this->getJoboffererProfile()->getId())
  103. ) {
  104. return true;
  105. } else {
  106. return false;
  107. }
  108. }
  109. public function isReviewee(Profile $profile)
  110. {
  111. if (($this->reviewer === self::REVIEWER_JOBSEEKER
  112. && $profile instanceof JoboffererProfile
  113. && $profile->getId() === $this->getJoboffererProfile()->getId())
  114. || ($this->reviewer === self::REVIEWER_JOBOFFERER
  115. && $profile instanceof JobseekerProfile
  116. && $profile->getId() === $this->getJobseekerProfile()->getId())
  117. ) {
  118. return true;
  119. } else {
  120. return false;
  121. }
  122. }
  123. public function setId(string $id)
  124. {
  125. GuidUtility::validOrThrow($id);
  126. $this->id = $id;
  127. }
  128. public function getId()
  129. {
  130. return $this->id;
  131. }
  132. /**
  133. * @return JobseekerProfile
  134. */
  135. public function getJobseekerProfile()
  136. {
  137. return $this->jobseekerProfile;
  138. }
  139. public function setJobseekerProfile(JobseekerProfile $jobseekerProfile)
  140. {
  141. $this->jobseekerProfile = $jobseekerProfile;
  142. }
  143. /**
  144. * @return JoboffererProfile
  145. */
  146. public function getJoboffererProfile()
  147. {
  148. return $this->joboffererProfile;
  149. }
  150. public function setJoboffererProfile(JoboffererProfile $joboffererProfile)
  151. {
  152. $this->joboffererProfile = $joboffererProfile;
  153. }
  154. /**
  155. * @return int
  156. */
  157. public function getReviewer()
  158. {
  159. return $this->reviewer;
  160. }
  161. public function setReviewer(int $reviewer)
  162. {
  163. $this->reviewer = $reviewer;
  164. }
  165. /**
  166. * @return int
  167. */
  168. public function getRating()
  169. {
  170. return $this->rating;
  171. }
  172. public function setRating(int $rating)
  173. {
  174. $this->rating = $rating;
  175. }
  176. public function getFeedback()
  177. {
  178. return $this->feedback;
  179. }
  180. public function setFeedback(string $feedback)
  181. {
  182. $this->feedback = $feedback;
  183. }
  184. /**
  185. * @return DateTime
  186. */
  187. public function getCreatedAt()
  188. {
  189. return $this->createdAt;
  190. }
  191. public function setCreatedAt(DateTime $createdAt)
  192. {
  193. $this->createdAt = $createdAt;
  194. }
  195. public function isActivatedByAdmin(): bool
  196. {
  197. return $this->isActivatedByAdmin;
  198. }
  199. public function setIsActivatedByAdmin(bool $isActivatedByAdmin)
  200. {
  201. $this->isActivatedByAdmin = $isActivatedByAdmin;
  202. }
  203. public function isActive(): bool
  204. {
  205. return $this->isActivatedByAdmin;
  206. }
  207. public function __toString()
  208. {
  209. return $this->getId();
  210. }
  211. /**
  212. * @Assert\Callback
  213. */
  214. public function validate(ExecutionContextInterface $context, $payload)
  215. {
  216. if ($this->rating === self::RATING_NEGATIVE && (is_null($this->feedback) || trim($this->feedback) === '')) {
  217. $context
  218. ->buildViolation('reviews.new_form_page.feedback_required_error')
  219. ->setTranslationDomain('messages')
  220. ->atPath('feedback')
  221. ->addViolation();
  222. }
  223. }
  224. }