= startDate (can be the same day) */ #[ORM\Column(type: Types::DATETIME_IMMUTABLE)] protected \DateTimeImmutable $endAt; /** * Virtual field for the request service creation. This is the optional message * sent by the borrower to the lender a the request service creation. It is stored * in the conversation thread of the request service not the request service itself. */ protected ?string $message = null; /** * @var Collection $messages */ #[ORM\OneToMany(mappedBy: 'serviceRequest', targetEntity: Message::class, cascade: ['persist', 'remove', 'detach'])] #[ORM\OrderBy(['createdAt' => 'ASC'])] private Collection $messages; public function __construct() { $this->messages = new ArrayCollection(); } public function __toString() { return (string) $this->id; } public function getId(): Uuid { return $this->id; } public function setId(Uuid $id): ServiceRequest { $this->id = $id; return $this; } public function getOwner(): User { return $this->owner; } public function isOwner(?User $user): bool { return $this->owner === $user; } public function setOwner(User $owner): ServiceRequest { $this->owner = $owner; return $this; } public function getProduct(): Product { return $this->product; } public function setProduct(Product $product): ServiceRequest { $this->product = $product; return $this; } public function getRecipient(): User { return $this->recipient; } public function isRecipient(User $user): bool { return $this->recipient === $user; } public function setRecipient(User $recipient): ServiceRequest { $this->recipient = $recipient; return $this; } public function getStatus(): ServiceRequestStatus { return $this->status; } /** * This function should never be called manually, except in tests. * * @internal */ public function setStatus(ServiceRequestStatus $status): ServiceRequest { $this->status = $status; return $this; } /** * This is needed for the workflow that works with strings, not enum. * * @internal */ public function getStatusRaw(): string { return $this->status->value; } /** * This function should never be called manually. * * @internal */ public function setStatusRaw(string $status): ServiceRequest { $this->status = ServiceRequestStatus::from($status); return $this; } public function getStartAt(): \DateTimeImmutable { return $this->startAt; } public function setStartAt(\DateTimeImmutable $startAt): ServiceRequest { $this->startAt = $startAt; return $this; } public function getEndAt(): \DateTimeImmutable { return $this->endAt; } public function setEndAt(\DateTimeImmutable $endAt): ServiceRequest { $this->endAt = $endAt; return $this; } public function getMessage(): ?string { return $this->message; } public function setMessage(?string $message): ServiceRequest { $this->message = $message; return $this; } /** * @return Collection */ public function getMessages(): Collection { return $this->messages; } /** * @param Collection $messages */ public function setMessages(Collection $messages): self { $this->messages = $messages; return $this; } public function addMessage(Message $message): self { if (!$this->messages->contains($message)) { $this->messages->add($message); $message->setServiceRequest($this); } return $this; } public function removeMessage(Message $item): self { $this->messages->removeElement($item); return $this; } public function messagesCount(): int { return $this->messages->count(); } public function isLoan(): bool { return $this->product->getType()->isObject(); } public function isService(): bool { return $this->product->getType()->isService(); } // end of basic 'etters ———————————————————————————————————————————————————— public function isOwnerOrRecipient(User $user): bool { return $this->owner === $user || $this->recipient === $user; } public function hasUnreadMessages(User $user): bool { $messages = $this->messages->filter( fn (Message $message) => $this->isOwner($user) ? !$message->isOwnerRead() : !$message->isRecipientRead()); return !$messages->isEmpty(); } /** * The the other user involved in the service request. */ public function getOtherUser(?User $actor): User { return $this->isOwner($actor) ? $this->getRecipient() : $this->getOwner(); } /** * Get the finalized date from the end date. We consider a service request is * finished the day just after the end date to avoid overlap problems. */ public function getFinalizedAt(): \DateTimeImmutable { return $this->getEndAt()->modify('tomorrow midnight'); } }