src/Entity/ChannelUserData/Cursus/Internship.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\Entity\ChannelUserData\Cursus;
  3. use Nellapp\Bundle\SDKBundle\TrackData\Attribute\TrackEntity;
  4. use Nellapp\Bundle\SDKBundle\TrackData\Attribute\TrackField;
  5. use App\Entity\Channel\Company;
  6. use App\Entity\ChannelUserData\AdminChannelUserData;
  7. use App\Entity\Common\Address;
  8. use App\Enum\ChannelUserData\Activity\CursusActivityTagEnum;
  9. use App\Repository\ChannelUserData\Cursus\InternshipRepository;
  10. use DateTimeImmutable;
  11. use DateTimeInterface;
  12. use Doctrine\ORM\Mapping as ORM;
  13. use DrosalysWeb\ObjectExtensions\Blame\Model\BlameInterface;
  14. use DrosalysWeb\ObjectExtensions\Blame\Model\BlameTrait;
  15. use DrosalysWeb\ObjectExtensions\Timestamp\Model\TimestampInterface;
  16. use DrosalysWeb\ObjectExtensions\Timestamp\Model\TimestampTrait;
  17. use Nellapp\Bundle\SDKBundle\Utils\NellappFormatService;
  18. use Symfony\Component\Validator\Constraints as Assert;
  19. #[
  20. ORM\Entity(repositoryClass: InternshipRepository::class),
  21. ORM\Table(name: 'channel_user_data_cursus_internship'),
  22. TrackEntity(
  23. name: 'internship.entity.track_data.entity.name',
  24. fieldGroups: [
  25. 'internship' => [
  26. 'tags' => [ CursusActivityTagEnum::INTERNSHIP ]
  27. ],
  28. ],
  29. ),
  30. ]
  31. class Internship implements TimestampInterface, BlameInterface
  32. {
  33. use TimestampTrait;
  34. use BlameTrait;
  35. public const START_DELAY = '-1 weeks';
  36. public const END_DELAY = '+2 weeks';
  37. #[ORM\Id, ORM\GeneratedValue]
  38. #[ORM\Column(type: 'integer')]
  39. private ?int $id = null;
  40. #[ORM\ManyToOne(targetEntity: AbstractCursus::class, inversedBy: 'internships')]
  41. #[ORM\JoinColumn(nullable: false, onDelete: "CASCADE")]
  42. private ?AbstractCursus $cursus = null;
  43. #[ORM\Column(type: 'date')]
  44. #[
  45. Assert\NotBlank,
  46. ]
  47. #[TrackField(name: 'internship.entity.startAt.label', fieldKey: 'internship', callable: 'format', callableParams: ['d/m/Y'])]
  48. private ?DateTimeInterface $startAt = null;
  49. #[ORM\Column(type: 'date')]
  50. #[
  51. Assert\NotBlank,
  52. ]
  53. #[TrackField(name: 'internship.entity.endAt.label', fieldKey: 'internship', callable: 'format', callableParams: ['d/m/Y'])]
  54. private ?DateTimeInterface $endAt = null;
  55. #[ORM\Column(type: 'integer', nullable: true)]
  56. #[TrackField(name: 'internship.entity.duration.label', fieldKey: 'internship', callable: 'formatDuration', callableClass: NellappFormatService::class)]
  57. #[Assert\GreaterThanOrEqual(value: 0)]
  58. private ?int $duration = null;
  59. #[ORM\Embedded(class: Address::class)]
  60. #[Assert\Valid()]
  61. #[TrackField(name: 'internship.entity.address.label', fieldKey: 'internship', embeddedFields: [
  62. 'address.street' => 'internship.entity.addressStreet.label',
  63. 'address.postCode' => 'internship.entity.addressPostCode.label',
  64. 'address.city' => 'internship.entity.addressCity.label',
  65. 'address.country' => 'internship.entity.addressCountry.label',
  66. ])]
  67. private ?Address $address = null;
  68. #[ORM\ManyToOne(targetEntity: Company::class, cascade: ['persist'], inversedBy: 'internships')]
  69. #[ORM\JoinColumn(nullable: true, onDelete: 'SET NULL')]
  70. #[TrackField(name: 'internship.entity.company.label', fieldKey: 'internship')]
  71. private ?Company $company = null;
  72. #[ORM\ManyToOne(targetEntity: AdminChannelUserData::class, fetch: 'EAGER', inversedBy: 'internships')]
  73. #[ORM\JoinColumn(nullable: true, onDelete: 'SET NULL')]
  74. #[TrackField(name: 'internship.entity.tutor.label', fieldKey: 'internship')]
  75. private ?AdminChannelUserData $tutor = null;
  76. public function getId(): ?int
  77. {
  78. return $this->id;
  79. }
  80. public function getCursus(): ?AbstractCursus
  81. {
  82. return $this->cursus;
  83. }
  84. public function setCursus(?AbstractCursus $cursus): static
  85. {
  86. $this->cursus = $cursus;
  87. return $this;
  88. }
  89. public function getStartAt(): ?DateTimeInterface
  90. {
  91. return $this->startAt;
  92. }
  93. public function setStartAt(?DateTimeInterface $startAt): static
  94. {
  95. $this->startAt = $startAt;
  96. return $this;
  97. }
  98. public function getEndAt(): ?DateTimeInterface
  99. {
  100. return $this->endAt;
  101. }
  102. public function setEndAt(?DateTimeInterface $endAt): static
  103. {
  104. $this->endAt = $endAt;
  105. return $this;
  106. }
  107. /**
  108. * Date from which the tutor gets access to the cursus (START_DELAY before the internship starts)
  109. */
  110. public function getTutorAccessStartAt(): ?\DateTime
  111. {
  112. return $this->startAt ? \DateTime::createFromInterface($this->startAt)->modify(self::START_DELAY) : null;
  113. }
  114. /**
  115. * Date until which the tutor keeps access to the cursus (END_DELAY after the internship ends)
  116. */
  117. public function getTutorAccessEndAt(): ?\DateTime
  118. {
  119. return $this->endAt ? \DateTime::createFromInterface($this->endAt)->modify(self::END_DELAY) : null;
  120. }
  121. public function isActiveWithDelays(): bool
  122. {
  123. $now = new DateTimeImmutable();
  124. $start = $this->getTutorAccessStartAt();
  125. $end = $this->getTutorAccessEndAt();
  126. if ($start && $now < $start) {
  127. return false;
  128. }
  129. if ($end && $now > $end) {
  130. return false;
  131. }
  132. return true;
  133. }
  134. public function isEnded(): bool
  135. {
  136. return $this->endAt !==null && $this->endAt < new DateTimeImmutable();
  137. }
  138. public function isActive(): bool
  139. {
  140. return !$this->isEnded();
  141. }
  142. public function getDuration(): ?int
  143. {
  144. return $this->duration;
  145. }
  146. public function setDuration(?int $duration): static
  147. {
  148. $this->duration = $duration;
  149. return $this;
  150. }
  151. public function getAddress(): ?Address
  152. {
  153. return $this->address;
  154. }
  155. public function setAddress(?Address $address): static
  156. {
  157. $this->address = $address;
  158. return $this;
  159. }
  160. public function getCompany(): ?Company
  161. {
  162. return $this->company;
  163. }
  164. public function setCompany(?Company $company): static
  165. {
  166. $this->company = $company;
  167. return $this;
  168. }
  169. public function getTutor(): ?AdminChannelUserData
  170. {
  171. return $this->tutor;
  172. }
  173. public function setTutor(?AdminChannelUserData $tutor): static
  174. {
  175. $this->tutor = $tutor;
  176. return $this;
  177. }
  178. }