Passed
Push — develop ( 370f48...cc396d )
by Laurent
07:33 queued 04:53
created

DoctrineUserFinder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the G.L.S.R. Apps package.
7
 *
8
 * (c) Dev-Int Création <[email protected]>.
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Administration\Infrastructure\Finders\DoctrineOrm;
15
16
use Administration\Application\Protocol\Finders\UserFinderProtocol;
17
use Administration\Application\User\ReadModel\User as UserReadModel;
18
use Administration\Application\User\ReadModel\Users;
19
use Administration\Domain\User\Model\VO\UserUuid;
20
use Administration\Infrastructure\Finders\Exceptions\UserNotFound;
21
use Core\Domain\Common\Model\VO\EmailField;
22
use Core\Domain\Common\Model\VO\NameField;
23
use Core\Domain\Model\User as UserDomainModel;
24
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
25
use Doctrine\ORM\NonUniqueResultException;
26
use Doctrine\Persistence\ManagerRegistry;
27
28
class DoctrineUserFinder extends ServiceEntityRepository implements UserFinderProtocol
29
{
30
    public function __construct(ManagerRegistry $registry)
31
    {
32
        parent::__construct($registry, UserDomainModel ::class);
33
    }
34
35
    /**
36
     * @throws NonUniqueResultException
37
     */
38
    public function findOneByUsername(string $username): UserDomainModel
39
    {
40
        $result = $this->createQueryBuilder('u')
41
            ->where('u.username = :username')
42
            ->setParameter('username', $username)
43
            ->getQuery()
44
            ->getOneOrNullResult()
45
        ;
46
47
        if (null === $result) {
48
            throw new UserNotFound();
49
        }
50
51
        return new UserDomainModel(
52
            UserUuid::fromString($result->getUuid()),
53
            NameField::fromString($result->getUsername()),
54
            EmailField::fromString($result->getEmail()),
55
            $result->getPassword(),
56
            $result->getRoles()
57
        );
58
    }
59
60
    /**
61
     * @throws NonUniqueResultException
62
     */
63
    final public function findOneByUuid(string $uuid): UserDomainModel
64
    {
65
        $result = $this->createQueryBuilder('u')
66
            ->where('u.uuid = :uuid')
67
            ->setParameter('uuid', $uuid)
68
            ->getQuery()
69
            ->getOneOrNullResult()
70
        ;
71
72
        if (null === $result) {
73
            throw new UserNotFound();
74
        }
75
76
        return new UserDomainModel(
77
            UserUuid::fromString($result->getUuid()),
78
            NameField::fromString($result->getUsername()),
79
            EmailField::fromString($result->getEmail()),
80
            $result->getPassword(),
81
            $result->getRoles()
82
        );
83
    }
84
85
    final public function findAllUsers(): Users
86
    {
87
        $statement = $this->createQueryBuilder('u')
88
            ->getQuery()
89
            ->getResult()
90
        ;
91
92
        return new Users(
93
            ...\array_map(static function (UserDomainModel $user) {
94
                return new UserReadModel(
95
                    $user->getUuid(),
96
                    $user->getUsername(),
97
                    $user->getEmail(),
98
                    $user->getPassword()
99
                );
100
            }, $statement)
101
        );
102
    }
103
}
104