Passed
Pull Request — master (#18)
by Luc
31:17 queued 22:24
created

UserDoctrineRepository::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php declare(strict_types=1);
2
3
namespace VSV\GVQ_API\User\Repositories;
4
5
use Ramsey\Uuid\UuidInterface;
6
use VSV\GVQ_API\Common\Repositories\AbstractDoctrineRepository;
7
use VSV\GVQ_API\User\Models\User;
8
use VSV\GVQ_API\User\Models\Users;
9
use VSV\GVQ_API\User\Repositories\Entities\UserEntity;
10
use VSV\GVQ_API\User\ValueObjects\Email;
11
12
class UserDoctrineRepository extends AbstractDoctrineRepository implements UserRepository
13
{
14
    /**
15
     * @inheritdoc
16
     */
17
    protected function getRepositoryName(): string
18
    {
19
        return UserEntity::class;
20
    }
21
22
    /**
23
     * @inheritdoc
24
     */
25
    public function save(User $user): void
26
    {
27
        $userEntity = UserEntity::fromUser($user);
28
29
        $this->entityManager->persist($userEntity);
30
        $this->entityManager->flush();
31
    }
32
33
    /**
34
     * @inheritdoc
35
     */
36
    public function update(User $user): void
37
    {
38
        $this->entityManager->merge(
39
            UserEntity::fromUser($user)
40
        );
41
        $this->entityManager->flush();
42
    }
43
44
    /**
45
     * @inheritdoc
46
     */
47
    public function getById(UuidInterface $id): ?User
48
    {
49
        /** @var UserEntity|null $userEntity */
50
        $userEntity = $this->objectRepository->findOneBy(
51
            [
52
                'id' => $id->toString(),
53
            ]
54
        );
55
56
        return $userEntity ? $userEntity->toUser() : null;
57
    }
58
59
    /**
60
     * @inheritdoc
61
     */
62
    public function getByEmail(Email $email): ?User
63
    {
64
        /** @var UserEntity|null $userEntity */
65
        $userEntity = $this->objectRepository->findOneBy(
66
            [
67
                'email' => $email->toNative(),
68
            ]
69
        );
70
71
        return $userEntity ? $userEntity->toUser() : null;
72
    }
73
74
    /**
75
     * @inheritdoc
76
     */
77
    public function getAll(): ?Users
78
    {
79
        /** @var UserEntity[] $userEntities */
80
        $userEntities = $this->objectRepository->findAll();
81
82
        if (empty($userEntities)) {
83
            return null;
84
        }
85
86
        return new Users(
87
            ...array_map(
88
                function (UserEntity $questionEntity) {
89
                    return $questionEntity->toUser();
90
                },
91
                $userEntities
92
            )
93
        );
94
    }
95
}
96