BookRepository::returnBook()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
nc 1
nop 4
dl 0
loc 10
rs 10
c 1
b 0
f 0
ccs 7
cts 7
cp 1
crap 1
1
<?php
2
3
namespace App\Repository;
4
5
use App\Entity\Book;
6
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
7
use Doctrine\Persistence\ManagerRegistry;
8
9
/**
10
 * @extends ServiceEntityRepository<Book>
11
 */
12
class BookRepository extends ServiceEntityRepository
13
{
14
    private ManagerRegistry $doctrine;
15
16
    /**
17
     * __construct.
18
     *
19
     * @return void
20
     */
21 10
    public function __construct(ManagerRegistry $doctrine)
22
    {
23 10
        $this->doctrine = $doctrine;
24
25 10
        parent::__construct($doctrine, Book::class);
26
    }
27
28
    /**
29
     * returnBook.
30
     *
31
     * Return book object (works like a pseudo constructor)
32
     */
33 2
    public function returnBook(string $title, ?string $isbn = null, ?string $author = null, ?string $img = null): Book
34
    {
35 2
        $book = new Book();
36
37 2
        $book->setTitle($title);
38 2
        $book->setISBN($isbn);
39 2
        $book->setAuthor($author);
40 2
        $book->setImg($img);
41
42 2
        return $book;
43
    }
44
45
    /**
46
     * saveBook.
47
     *
48
     * Saves a book to the database
49
     */
50 2
    public function saveBook(string $title, ?string $isbn = null, ?string $author = null, ?string $img = null): void
51
    {
52
        // If title is empty
53 2
        if ('' === $title) {
54 1
            return;
55
        }
56
57 2
        $book = $this->returnBook($title, $isbn, $author, $img);
58
59 2
        $entityManager = $this->doctrine->getManager();
60
61
        // tell Doctrine you want to (eventually) save the Book
62
        // (no queries yet)
63 2
        $entityManager->persist($book);
64
65
        // actually executes the queries (i.e. the INSERT query)
66 2
        $entityManager->flush();
67
    }
68
69
    /**
70
     * updateBook.
71
     *
72
     * Saves a book to the database
73
     */
74 2
    public function updateBook(int $id, string $title, ?string $isbn = null, ?string $author = null, ?string $img = null): void
75
    {
76 2
        $book = $this->findOneBy(['id' => $id]);
77
78
        // If the book can't be found
79 2
        if (null === $book) {
80 1
            return;
81
        }
82
83
        // If title is empty
84 2
        if ('' === $title) {
85 1
            return;
86
        }
87
88 2
        $book->setTitle($title);
89
90
        // If you have new ISBN
91 2
        if (null !== $isbn) {
92 2
            $book->setIsbn($isbn);
93
        }
94
95
        // If you have new Author
96 2
        if (null !== $author) {
97 2
            $book->setAuthor($author);
98
        }
99
100
        // If you have new image
101 2
        if (null !== $img) {
102 1
            $book->setImg($img);
103
        }
104
105
        // Get the entity manager
106 2
        $entityManager = $this->doctrine->getManager();
107
108
        // Persist is optional for existing entities, but it doesn't hurt
109 2
        $entityManager->persist($book);
110
111
        // Flush to save changes
112 2
        $entityManager->flush();
113
    }
114
115
    /**
116
     * deleteBook.
117
     */
118 2
    public function deleteBook(int $id): void
119
    {
120 2
        $book = $this->findOneBy(['id' => $id]);
121
122
        // If the book can't be found
123 2
        if (null === $book) {
124 1
            return;
125
        }
126
127
        // Get the entity manager
128 2
        $entityManager = $this->doctrine->getManager();
129
130
        // Remove the book
131 2
        $entityManager->remove($book);
132 2
        $entityManager->flush();
133
    }
134
135
    /**
136
     * readAllBooks.
137
     *
138
     * Reads all books from the database and returns an array of books.
139
     *
140
     * @return array
141
     *               An associative array with a key 'books' containing an array of book details:
142
     *               - books: array<array{
143
     *               id: int|null,
144
     *               title: string|null,
145
     *               isbn: string|null,
146
     *               author: string|null,
147
     *               img: string|null
148
     *               }>
149
     *
150
     * @phpstan-return array{
151
     *   books: array<array{
152
     *     id: int|null,
153
     *     title: string|null,
154
     *     isbn: string|null,
155
     *     author: string|null,
156
     *     img: string|null
157
     *   }>
158
     * }
159
     */
160 10
    public function readAllBooks(): array
161
    {
162 10
        $data = [
163 10
            'books' => [],
164 10
        ];
165
166 10
        $books = $this->findAll();
167
168 10
        $numBooks = count($books);
169
170 10
        for ($i = 0; $i < $numBooks; ++$i) {
171 10
            $data['books'][$i]['id'] = $books[$i]->getId();
172 10
            $data['books'][$i]['title'] = $books[$i]->getTitle();
173 10
            $data['books'][$i]['isbn'] = $books[$i]->getISBN();
174 10
            $data['books'][$i]['author'] = $books[$i]->getAuthor();
175 10
            $data['books'][$i]['img'] = $books[$i]->getImg();
176
        }
177
178 10
        return $data;
179
    }
180
181
    /**
182
     * readOneBook.
183
     *
184
     * @return array
185
     *               An associative array with key 'book' containing the details of a single book:
186
     *               - book: array{
187
     *               id: int,
188
     *               title: string|null,
189
     *               isbn: string|null,
190
     *               author: string|null,
191
     *               img: string|null
192
     *               }
193
     *
194
     * @phpstan-return array{
195
     *   book: array{
196
     *     id: int,
197
     *     title: string|null,
198
     *     isbn: string|null,
199
     *     author: string|null,
200
     *     img: string|null
201
     *   }
202
     * }
203
     */
204 4
    public function readOneBook(int $id): array
205
    {
206 4
        $data = [
207 4
            'book' => [
208 4
                'id' => $id,
209 4
                'title' => '',
210 4
                'isbn' => null,
211 4
                'author' => null,
212 4
                'img' => null,
213 4
            ],
214 4
        ];
215
216
        // If id is less then 0
217 4
        if ($id < 0) {
218 1
            return $data;
219
        }
220
221 4
        $book = $this->findOneBy(['id' => $id]);
222
223
        // If no book is found
224 4
        if (null === $book) {
225 2
            return $data;
226
        }
227
228 4
        $data['book']['title'] = $book->getTitle();
229 4
        $data['book']['isbn'] = $book->getISBN();
230 4
        $data['book']['author'] = $book->getAuthor();
231 4
        $data['book']['img'] = $book->getImg();
232
233 4
        return $data;
234
    }
235
236
    /**
237
     * readOneBookISBN.
238
     *
239
     * @return array
240
     *               An associative array with key 'book' containing the details of a single book:
241
     *               - book: array{
242
     *               id: int|null,
243
     *               title: string|null,
244
     *               isbn: string,
245
     *               author: string|null,
246
     *               img: string|null
247
     *               }
248
     *
249
     * @phpstan-return array{
250
     *   book: array{
251
     *     id: int|null,
252
     *     title: string|null,
253
     *     isbn: string,
254
     *     author: string|null,
255
     *     img: string|null
256
     *   }
257
     * }
258
     */
259 3
    public function readOneBookISBN(string $isbn): array
260
    {
261 3
        $data = [
262 3
            'book' => [
263 3
                'id' => -1,
264 3
                'title' => '',
265 3
                'isbn' => $isbn,
266 3
                'author' => null,
267 3
                'img' => null,
268 3
            ],
269 3
        ];
270
271
        // isbn is not 13 digits return the empty data
272 3
        if (13 != strlen($isbn)) {
273 1
            return $data;
274
        }
275
276 3
        $book = $this->findOneBy(['isbn' => $isbn]);
277
278
        // If no book is found
279 3
        if (null === $book) {
280 2
            return $data;
281
        }
282
283 3
        $data['book']['id'] = $book->getId();
284 3
        $data['book']['title'] = $book->getTitle();
285 3
        $data['book']['author'] = $book->getAuthor();
286 3
        $data['book']['img'] = $book->getImg();
287
288 3
        return $data;
289
    }
290
}
291