<?php
|
declare(strict_types=1);
|
|
namespace App\Entity;
|
|
use App\Enum\LabelType;
|
use App\Repository\BookRepository;
|
use Doctrine\Common\Collections\ArrayCollection;
|
use Doctrine\Common\Collections\Collection;
|
use Doctrine\ORM\Mapping as ORM;
|
|
#[ORM\Entity(repositoryClass: BookRepository::class)]
|
class Book extends AbstractBook {
|
#[ORM\OneToOne(targetEntity: Borrow::class, mappedBy: 'book', cascade: ['persist', 'remove'], orphanRemoval: true)]
|
private ?Borrow $borrow = null;
|
|
/** @var Collection<int, Label> */
|
#[ORM\OneToMany(targetEntity: Label::class, mappedBy: 'book', cascade: ['persist', 'remove'], fetch: 'EAGER', orphanRemoval: true)]
|
#[ORM\OrderBy(['label' => 'ASC'])]
|
private Collection $labels;
|
|
/** @var Collection<int, BorrowHistory> */
|
#[ORM\OneToMany(targetEntity: BorrowHistory::class, mappedBy: 'book', cascade: ['persist', 'remove'], orphanRemoval: true)]
|
#[ORM\OrderBy(['at' => 'DESC'])]
|
private Collection $borrowHistory;
|
|
public function __construct() {
|
$this->labels = new ArrayCollection();
|
$this->borrowHistory = new ArrayCollection();
|
}
|
|
public static function fromOusted(OustedBook $ousted): self {
|
$book = new self();
|
$book->copyDataFrom($ousted);
|
|
return $book;
|
}
|
|
public function getBorrow(): ?Borrow {
|
return $this->borrow;
|
}
|
|
public function isBorrowed(): bool {
|
return $this->borrow !== null;
|
}
|
|
public function lendTo(string $who, \DateTimeImmutable $at): void {
|
if ($this->isBorrowed()) {
|
throw new \LogicException(\sprintf('Book %d is already borrowed.', $this->id));
|
}
|
|
$this->borrow = new Borrow($this, $who, $at);
|
}
|
|
public function giveBack(\DateTimeImmutable $at): void {
|
if (!$this->borrow instanceof Borrow) {
|
throw new \LogicException(\sprintf('Book %d is not borrowed.', $this->id));
|
}
|
|
$this->borrowHistory->add(new BorrowHistory($this, $this->borrow->getWho(), $this->borrow->getAt(), $at));
|
$this->borrow = null;
|
}
|
|
/**
|
* @return Collection<int, Label>
|
*/
|
public function getLabels(): Collection {
|
return $this->labels;
|
}
|
|
protected function createLabel(string $label, LabelType $type): Label {
|
return new Label($this, $label, $type);
|
}
|
|
/**
|
* @return Collection<int, BorrowHistory>
|
*/
|
public function getBorrowHistory(): Collection {
|
return $this->borrowHistory;
|
}
|
|
public function addBorrowHistory(string $who, \DateTimeImmutable $at, \DateTimeImmutable $till): void {
|
$this->borrowHistory->add(new BorrowHistory($this, $who, $at, $till));
|
}
|
}
|