| Total Complexity | 5 |
| Total Lines | 33 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 16 | final class BookRepository implements BookRepositoryInterface |
||
| 17 | { |
||
| 18 | use OrmRepositoryTrait; |
||
| 19 | |||
| 20 | public function __construct(EntityManagerInterface $em, string $className = Book::class) |
||
| 21 | { |
||
| 22 | $this->em = $em; |
||
| 23 | $this->className = $className; |
||
| 24 | } |
||
| 25 | |||
| 26 | public function byId(BookId $id): ?Book |
||
| 27 | { |
||
| 28 | /** @var Book $book */ |
||
| 29 | $book = $this->em->find($this->className, $id); |
||
| 30 | |||
| 31 | return $book; |
||
| 32 | } |
||
| 33 | |||
| 34 | public function add(Book $book): void |
||
| 35 | { |
||
| 36 | $this->em->persist($book); |
||
| 37 | $this->em->flush($book); |
||
| 38 | } |
||
| 39 | |||
| 40 | public function paginate(AuthorId $authorId = null): Pagerfanta |
||
| 41 | { |
||
| 42 | $qb = $this->createQueryBuilder('b')->orderBy('b.createdAt', 'DESC'); |
||
| 43 | |||
| 44 | if ($authorId) { |
||
| 45 | $qb->where('b.authorId = :author_id')->setParameter('author_id', $authorId); |
||
| 46 | } |
||
| 47 | |||
| 48 | return new Pagerfanta(new DoctrineORMAdapter($qb, true, false)); |
||
| 49 | } |
||
| 51 |