CategoryDoctrineRepository::save()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
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 Ramsey\Uuid\UuidInterface;
6
use VSV\GVQ_API\Common\Repositories\AbstractDoctrineRepository;
7
use VSV\GVQ_API\Question\Models\Categories;
8
use VSV\GVQ_API\Question\Models\Category;
9
use VSV\GVQ_API\Question\Repositories\Entities\CategoryEntity;
10
11
class CategoryDoctrineRepository extends AbstractDoctrineRepository implements CategoryRepository
12
{
13
    /**
14
     * @inheritdoc
15
     */
16
    public function getRepositoryName(): string
17
    {
18
        return CategoryEntity::class;
19
    }
20
21
    /**
22
     * @param Category $category
23
     */
24
    public function save(Category $category): void
25
    {
26
        $this->entityManager->persist(
27
            CategoryEntity::fromCategory($category)
28
        );
29
        $this->entityManager->flush();
30
    }
31
32
    /**
33
     * @param Category $category
34
     */
35
    public function update(Category $category): void
36
    {
37
        $this->entityManager->merge(
38
            CategoryEntity::fromCategory($category)
39
        );
40
        $this->entityManager->flush();
41
    }
42
43
    /**
44
     * @param Category $category
45
     */
46
    public function delete(Category $category): void
47
    {
48
        $category = $this->entityManager->merge(
49
            CategoryEntity::fromCategory($category)
50
        );
51
        $this->entityManager->remove($category);
52
        $this->entityManager->flush();
53
    }
54
55
    /**
56
     * @param UuidInterface $id
57
     * @return Category|null
58
     */
59
    public function getById(UuidInterface $id): ?Category
60
    {
61
        /** @var CategoryEntity|null $categoryEntity */
62
        $categoryEntity = $this->objectRepository->findOneBy(
63
            [
64
                'id' => $id,
65
            ]
66
        );
67
68
        return $categoryEntity ? $categoryEntity->toCategory() : null;
69
    }
70
71
    /**
72
     * @return Categories|null
73
     */
74
    public function getAll(): ?Categories
75
    {
76
        /** @var CategoryEntity[] $categories */
77
        $categoryEntities = $this->objectRepository->findAll();
78
79
        if (empty($categoryEntities)) {
80
            return null;
81
        }
82
83
        return new Categories(
84
            ...array_map(
85
                function (CategoryEntity $categoryEntity) {
86
                    return $categoryEntity->toCategory();
87
                },
88
                $categoryEntities
89
            )
90
        );
91
    }
92
}
93