Passed
Pull Request — master (#4)
by steven
02:46
created

CategoryDoctrineRepository::getAll()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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