Passed
Push — feature/VSVGVQ-7 ( 05f4d2...f0f3aa )
by Luc
02:30
created

CategoryDoctrineRepository::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 7
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(
30
            CategoryEntity::class
31
        );
32
    }
33
34
    /**
35
     * @param Category $category
36
     * @throws \Doctrine\ORM\ORMException
37
     * @throws \Doctrine\ORM\OptimisticLockException
38
     */
39
    public function save(Category $category): void
40
    {
41
        $this->entityManager->persist(
42
            CategoryEntity::fromCategory($category)
43
        );
44
        $this->entityManager->flush();
45
    }
46
47
    /**
48
     * @param Category $category
49
     * @throws \Doctrine\ORM\ORMException
50
     * @throws \Doctrine\ORM\OptimisticLockException
51
     */
52
    public function update(Category $category): void
53
    {
54
        $this->entityManager->merge(
55
            CategoryEntity::fromCategory($category)
56
        );
57
        $this->entityManager->flush();
58
    }
59
60
    /**
61
     * @param Category $category
62
     * @throws \Doctrine\ORM\ORMException
63
     * @throws \Doctrine\ORM\OptimisticLockException
64
     */
65
    public function delete(Category $category): void
66
    {
67
        $categoryEntity = $this->entityManager->merge(
68
            CategoryEntity::fromCategory($category)
69
        );
70
        $this->entityManager->remove($categoryEntity);
71
        $this->entityManager->flush();
72
    }
73
74
    /**
75
     * @param UuidInterface $id
76
     * @return Category|null
77
     */
78
    public function getById(UuidInterface $id): ?Category
79
    {
80
        /** @var CategoryEntity|null $categoryEntity */
81
        $categoryEntity = $this->objectRepository->findOneBy(
82
            [
83
                'id' => $id
84
            ]
85
        );
86
87
        if ($categoryEntity === null) {
88
            return null;
89
        }
90
91
        return $categoryEntity->toCategory();
92
    }
93
94
    /**
95
     * @return Categories|null
96
     */
97
    public function getAll(): ?Categories
98
    {
99
        $categoryEntities = $this->objectRepository->findAll();
100
101
        if (empty($categoryEntities)) {
102
            return null;
103
        }
104
105
        return new Categories(
106
            ...array_map(
107
                function (CategoryEntity $categoryEntity) {
108
                    return $categoryEntity->toCategory();
109
                },
110
                $categoryEntities
111
            )
112
        );
113
    }
114
}
115