Passed
Push — develop ( eb8792...6aee1f )
by Laurent
06:12
created

DoctrineUserFinder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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;
0 ignored issues
show
Bug introduced by
The type Doctrine\Bundle\Doctrine...ServiceEntityRepository was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
use Doctrine\ORM\NonUniqueResultException;
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\NonUniqueResultException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
use Doctrine\Persistence\ManagerRegistry;
0 ignored issues
show
Bug introduced by
The type Doctrine\Persistence\ManagerRegistry was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
                    $user->getRoles()
100
                );
101
            }, $statement)
102
        );
103
    }
104
}
105