Completed
Pull Request — master (#18)
by steven
09:09 queued 06:00
created

UserDoctrineRepository::getAll()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 0
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 getById(UuidInterface $id): ?User
37
    {
38
        /** @var UserEntity|null $userEntity */
39
        $userEntity = $this->objectRepository->findOneBy(
40
            [
41
                'id' => $id->toString(),
42
            ]
43
        );
44
45
        return $userEntity ? $userEntity->toUser() : null;
46
    }
47
48
    /**
49
     * @inheritdoc
50
     */
51
    public function getByEmail(Email $email): ?User
52
    {
53
        /** @var UserEntity|null $userEntity */
54
        $userEntity = $this->objectRepository->findOneBy(
55
            [
56
                'email' => $email->toNative(),
57
            ]
58
        );
59
60
        return $userEntity ? $userEntity->toUser() : null;
61
    }
62
63
    /**
64
     * @inheritdoc
65
     */
66
    public function getAll(): ?Users
67
    {
68
        /** @var UserEntity[] $userEntities */
69
        $userEntities = $this->objectRepository->findAll();
70
71
        if (empty($userEntities)) {
72
            return null;
73
        }
74
75
        return new Users(
76
            ...array_map(
77
                function (UserEntity $questionEntity) {
78
                    return $questionEntity->toUser();
79
                },
80
                $userEntities
81
            )
82
        );
83
    }
84
85
    /**
86
     * @inheritdoc
87
     */
88
    public function update(User $user): void
89
    {
90
        $this->entityManager->merge(
91
            UserEntity::fromUser($user)
92
        );
93
        $this->entityManager->flush();
94
    }
95
}
96