Completed
Pull Request — master (#1)
by steven
06:09 queued 03:07
created

CategoryDoctrineRepository::getById()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace VSV\GVQ_API\Question\Repositories;
4
5
use Doctrine\Common\Persistence\ObjectRepository;
6
use Doctrine\ORM\EntityManager;
7
use Ramsey\Uuid\UuidInterface;
8
use VSV\GVQ_API\Question\Models\Categories;
9
use VSV\GVQ_API\Question\Models\Category;
10
11
class CategoryDoctrineRepository implements CategoryRepository
12
{
13
    /**
14
     * @var EntityManager
15
     */
16
    private $entityManager;
17
18
    /**
19
     * @var ObjectRepository
20
     */
21
    private $objectRepository;
22
23
    /**
24
     * @param EntityManager $entityManager
25
     */
26
    public function __construct(EntityManager $entityManager)
27
    {
28
        $this->entityManager = $entityManager;
29
        $this->objectRepository = $this->entityManager->getRepository(Category::class);
30
    }
31
32
    /**
33
     * @param Category $category
34
     * @throws \Doctrine\ORM\ORMException
35
     * @throws \Doctrine\ORM\OptimisticLockException
36
     */
37
    public function save(Category $category): void
38
    {
39
        $this->entityManager->persist($category);
40
        $this->entityManager->flush();
41
    }
42
43
    /**
44
     * @param Category $category
45
     * @throws \Doctrine\ORM\ORMException
46
     * @throws \Doctrine\ORM\OptimisticLockException
47
     */
48
    public function update(Category $category): void
49
    {
50
        $this->entityManager->merge($category);
51
        $this->entityManager->flush();
52
    }
53
54
    /**
55
     * @param Category $category
56
     * @throws \Doctrine\ORM\ORMException
57
     * @throws \Doctrine\ORM\OptimisticLockException
58
     */
59
    public function delete(Category $category): void
60
    {
61
        $category = $this->entityManager->merge($category);
62
        $this->entityManager->remove($category);
63
        $this->entityManager->flush();
64
    }
65
66
    /**
67
     * @param UuidInterface $id
68
     * @return Category|null
69
     */
70
    public function getById(UuidInterface $id): ?Category
71
    {
72
        /** @var Category|null $category */
73
        $category = $this->objectRepository->findOneBy(
74
            [
75
                'id' => $id
76
            ]
77
        );
78
79
        return $category;
80
    }
81
82
    /**
83
     * @return Categories|null
84
     */
85
    public function getAll(): ?Categories
86
    {
87
        /** @var Category[] $categories */
88
        $categories = $this->objectRepository->findAll();
89
90
        if (empty($categories)) {
91
            return null;
92
        }
93
94
        return new Categories(...$categories);
95
    }
96
}
97