Jacek Kowalski
4 hours ago 6b9278a5d57091fce636dbb640b380cfb990db6c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?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));
    }
}