Catalogue::createAuthor()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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);
0 ignored issues
show
Bug introduced by
The method warm() does not exist on LibraryCatalog\Service\R...thorRepositoryInterface. It seems like you code against a sub-type of LibraryCatalog\Service\R...thorRepositoryInterface such as LibraryCatalog\Infrastru...e\AuthorRepositoryRedis. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

46
                $this->authorRepository->/** @scrutinizer ignore-call */ 
47
                                         warm($author);
Loading history...
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));
0 ignored issues
show
Bug introduced by
It seems like $this->authorRepository->load($book->authorId) can also be of type null; however, parameter $author of LibraryCatalog\Entity\Book::setAuthor() does only seem to accept LibraryCatalog\Entity\Author, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
            $book->setAuthor(/** @scrutinizer ignore-type */ $this->authorRepository->load($book->authorId));
Loading history...
71 2
            if ($this->bookRepository instanceof WarmRepositoryInterface) {
72
                // We can warm cache-repository with author.
73 2
                $this->bookRepository->warm($book);
0 ignored issues
show
Bug introduced by
The method warm() does not exist on LibraryCatalog\Service\R...BookRepositoryInterface. It seems like you code against a sub-type of LibraryCatalog\Service\R...BookRepositoryInterface such as LibraryCatalog\Infrastru...nce\BookRepositoryRedis. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
                $this->bookRepository->/** @scrutinizer ignore-call */ 
74
                                       warm($book);
Loading history...
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);
0 ignored issues
show
Bug introduced by
The method reset() does not exist on LibraryCatalog\Service\R...thorRepositoryInterface. It seems like you code against a sub-type of LibraryCatalog\Service\R...thorRepositoryInterface such as LibraryCatalog\Infrastru...e\AuthorRepositoryRedis. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

87
            $this->authorRepository->/** @scrutinizer ignore-call */ 
88
                                     reset($book->authorId);
Loading history...
88
        }
89 2
    }
90
91
    /**
92
     * @return AuthorRepositoryInterface
93
     */
94 4
    public function getAuthorRepository(): AuthorRepositoryInterface
95
    {
96 4
        return $this->authorRepository;
97
    }
98
}
99