1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace LibraryCatalog\Service; |
6
|
|
|
|
7
|
|
|
use LibraryCatalog\Entity\Author; |
8
|
|
|
use LibraryCatalog\Entity\Book; |
9
|
|
|
use LibraryCatalog\Service\Repository\AuthorRepositoryInterface; |
10
|
|
|
use LibraryCatalog\Service\Repository\BookRepositoryInterface; |
11
|
|
|
use LibraryCatalog\Service\Repository\WarmRepositoryInterface; |
12
|
|
|
|
13
|
|
|
class Catalogue |
14
|
|
|
{ |
15
|
|
|
/** @var AuthorRepositoryInterface */ |
16
|
|
|
protected AuthorRepositoryInterface $authorRepository; |
17
|
|
|
/** @var BookRepositoryInterface */ |
18
|
|
|
protected BookRepositoryInterface $bookRepository; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Catalogue constructor. |
22
|
|
|
* @param AuthorRepositoryInterface $authorRepository |
23
|
|
|
* @param BookRepositoryInterface $bookRepository |
24
|
|
|
*/ |
25
|
1 |
|
public function __construct( |
26
|
|
|
AuthorRepositoryInterface $authorRepository, |
27
|
|
|
BookRepositoryInterface $bookRepository |
28
|
|
|
) { |
29
|
1 |
|
$this->authorRepository = $authorRepository; |
30
|
1 |
|
$this->bookRepository = $bookRepository; |
31
|
1 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param $id |
35
|
|
|
* @param bool $withBooks |
36
|
|
|
* @return Author|null |
37
|
|
|
*/ |
38
|
6 |
|
public function fetchAuthor($id, bool $withBooks = false): ?Author |
39
|
|
|
{ |
40
|
6 |
|
$author = $this->authorRepository->load($id, $withBooks); |
41
|
6 |
|
if ($author && $withBooks && !$author->areBooksLoaded()) { |
42
|
|
|
// Load and set books for Author. |
43
|
3 |
|
$author->setBooks($this->bookRepository->loadByAuthorId($author->id)); |
44
|
3 |
|
if ($this->authorRepository instanceof WarmRepositoryInterface) { |
45
|
|
|
// We can warm cache-repository with books. |
46
|
3 |
|
$this->authorRepository->warm($author); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
} |
49
|
6 |
|
return $author; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param Author $author |
54
|
|
|
*/ |
55
|
4 |
|
public function createAuthor(Author $author): void |
56
|
|
|
{ |
57
|
4 |
|
$this->authorRepository->save($author); |
58
|
4 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param $id |
62
|
|
|
* @param bool $withAuthor |
63
|
|
|
* @return Book|null |
64
|
|
|
*/ |
65
|
4 |
|
public function fetchBook($id, bool $withAuthor = false): ?Book |
66
|
|
|
{ |
67
|
4 |
|
$book = $this->bookRepository->load($id, true); |
68
|
4 |
|
if ($book && $withAuthor && !$book->isAuthorLoaded()) { |
69
|
|
|
// Load and set author for book. |
70
|
2 |
|
$book->setAuthor($this->authorRepository->load($book->authorId)); |
|
|
|
|
71
|
2 |
|
if ($this->bookRepository instanceof WarmRepositoryInterface) { |
72
|
|
|
// We can warm cache-repository with author. |
73
|
2 |
|
$this->bookRepository->warm($book); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
} |
76
|
4 |
|
return $book; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param Book $book |
81
|
|
|
*/ |
82
|
2 |
|
public function createBook(Book $book): void |
83
|
|
|
{ |
84
|
2 |
|
$this->bookRepository->save($book); |
85
|
2 |
|
if ($this->authorRepository instanceof WarmRepositoryInterface) { |
86
|
|
|
// We reset author in warm cache-repository (as he should have to reload books). |
87
|
2 |
|
$this->authorRepository->reset($book->authorId); |
|
|
|
|
88
|
|
|
} |
89
|
2 |
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return AuthorRepositoryInterface |
93
|
|
|
*/ |
94
|
4 |
|
public function getAuthorRepository(): AuthorRepositoryInterface |
95
|
|
|
{ |
96
|
4 |
|
return $this->authorRepository; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|