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

AuthorRepository::byId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 6
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\Author;
8
use App\Domain\Model\AuthorId;
9
use App\Domain\Model\AuthorRepository as AuthorRepositoryInterface;
10
use Damax\Common\Doctrine\Orm\OrmRepositoryTrait;
11
use Doctrine\ORM\EntityManagerInterface;
12
13
final class AuthorRepository implements AuthorRepositoryInterface
14
{
15
    use OrmRepositoryTrait;
16
17
    public function __construct(EntityManagerInterface $em, string $className = Author::class)
18
    {
19
        $this->em = $em;
20
        $this->className = $className;
21
    }
22
23
    public function byId(AuthorId $id): ?Author
24
    {
25
        /** @var Author $author */
26
        $author = $this->em->find($this->className, $id);
27
28
        return $author;
29
    }
30
31
    public function add(Author $author): void
32
    {
33
        $this->em->persist($author);
34
        $this->em->flush($author);
35
    }
36
37
    public function update(Author $author): void
38
    {
39
        $this->em->persist($author);
40
        $this->em->flush($author);
41
    }
42
43
    public function all(): array
44
    {
45
        return $this->createQueryBuilder('a')->getQuery()->getResult();
46
    }
47
}
48