Completed
Pull Request — master (#1)
by Luc
06:59 queued 03:12
created

CategoryDoctrineRepository::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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