Passed
Push — develop ( 6aee1f...eb8792 )
by Laurent
01:33
created

DoctrineUserFinder   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 57
c 1
b 0
f 0
dl 0
loc 93
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A findOneByUsername() 0 23 2
A findOneByUuid() 0 23 2
A findAllUsers() 0 22 1
A __construct() 0 3 1
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\Doctrine;
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\Infrastructure\Finders\Exceptions\UserNotFound;
20
use Doctrine\DBAL\Connection;
0 ignored issues
show
Bug introduced by
The type Doctrine\DBAL\Connection 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...
21
use Doctrine\DBAL\Exception;
0 ignored issues
show
Bug introduced by
The type Doctrine\DBAL\Exception 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...
22
23
class DoctrineUserFinder implements UserFinderProtocol
24
{
25
    private Connection $connection;
26
27
    public function __construct(Connection $connection)
28
    {
29
        $this->connection = $connection;
30
    }
31
32
    /**
33
     * @throws Exception
34
     * @throws \Doctrine\DBAL\Driver\Exception
35
     */
36
    public function findOneByUsername(string $username): UserReadModel
37
    {
38
        $query = <<<'SQL'
39
SELECT
40
    user.uuid as uuid,
41
    user.username as username,
42
    user.email as email,
43
    user.roles as roles
44
FROM user
45
WHERE username = :username
46
SQL;
47
        $result = $this->connection->executeQuery($query, ['username' => $username])->fetchAssociative();
48
49
        if (false === $result) {
50
            throw new UserNotFound();
51
        }
52
53
        return new UserReadModel(
54
            $result['uuid'],
55
            $result['username'],
56
            $result['email'],
57
            \explode(',', $result['roles']),
58
            null,
59
        );
60
    }
61
62
    /**
63
     * @throws \Doctrine\DBAL\Driver\Exception|Exception
64
     */
65
    final public function findOneByUuid(string $uuid): UserReadModel
66
    {
67
        $query = <<<'SQL'
68
SELECT
69
    user.uuid as uuid,
70
    user.username as username,
71
    user.email as email,
72
    user.roles as roles
73
FROM user
74
WHERE uuid = :uuid
75
SQL;
76
        $result = $this->connection->executeQuery($query, ['uuid' => $uuid])->fetchAssociative();
77
78
        if (false === $result) {
79
            throw new UserNotFound();
80
        }
81
82
        return new UserReadModel(
83
            $result['uuid'],
84
            $result['username'],
85
            $result['email'],
86
            \explode(',', $result['roles']),
87
            null,
88
        );
89
    }
90
91
    /**
92
     * @throws \Doctrine\DBAL\Driver\Exception|Exception
93
     */
94
    final public function findAllUsers(): Users
95
    {
96
        $query = <<<'SQL'
97
SELECT
98
    user.uuid as uuid,
99
    user.username as username,
100
    user.email as email,
101
    user.roles as roles
102
FROM user
103
SQL;
104
        $result = $this->connection->executeQuery($query)->fetchAllAssociative();
105
106
        return new Users(
107
            ...\array_map(static function (array $user) {
108
                return new UserReadModel(
109
                    $user['uuid'],
110
                    $user['username'],
111
                    $user['email'],
112
                    \explode(',', $user['roles']),
113
                    null,
114
                );
115
            }, $result)
116
        );
117
    }
118
}
119