Passed
Push — master ( 75052b...8ecf83 )
by Dmitri
01:50
created

BookRepository::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Doctrine\Orm;
6
7
use App\Domain\Model\AuthorId;
8
use App\Domain\Model\Book;
9
use App\Domain\Model\BookId;
10
use App\Domain\Model\BookRepository as BookRepositoryInterface;
11
use Damax\Common\Doctrine\Orm\OrmRepositoryTrait;
12
use Doctrine\ORM\EntityManagerInterface;
13
use Pagerfanta\Adapter\DoctrineORMAdapter;
14
use Pagerfanta\Pagerfanta;
15
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
    }
50
}
51