Passed
Pull Request — develop (#152)
by Laurent
01:25
created

DoctrineUserRepository::getData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 8
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\Persistence\DoctrineOrm\Repositories;
15
16
use Administration\Domain\Protocol\Repository\UserRepositoryProtocol;
17
use Administration\Domain\User\Model\User;
18
use Administration\Domain\User\Model\VO\UserUuid;
19
use Administration\Infrastructure\User\Mapper\UserModelMapper;
20
use Core\Domain\Common\Model\VO\EmailField;
21
use Core\Domain\Common\Model\VO\NameField;
22
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...
23
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...
24
25
class DoctrineUserRepository implements UserRepositoryProtocol
26
{
27
    protected Connection $connection;
28
29
    public function __construct(Connection $connection)
30
    {
31
        $this->connection = $connection;
32
    }
33
34
    /**
35
     * @throws Exception
36
     */
37
    final public function add(User $user): void
38
    {
39
        $data = (new UserModelMapper())->getDataFromUser($user);
40
41
        $this->connection->createQueryBuilder()
42
            ->insert('user')
43
            ->values(['uuid' => '?', 'username' => '?', 'email' => '?', 'password' => '?', 'roles' => '?'])
44
            ->setParameters([$data['uuid'], $data['username'], $data['email'], $data['password'], $data['roles']])
45
            ->execute()
46
        ;
47
    }
48
49
    /**
50
     * @throws Exception
51
     */
52
    final public function update(User $user): void
53
    {
54
        $data = (new UserModelMapper())->getDataFromUser($user);
55
56
        $this->connection->createQueryBuilder()
57
            ->update('user')
58
            ->set('username', '?')
59
            ->set('email', '?')
60
            ->set('password', '?')
61
            ->set('roles', '?')
62
            ->where('uuid = ?')
63
            ->setParameters([0 => $data['username'], 1 => $data['email'], 2 => $data['password'],
64
                3 => $data['roles'], 4 => $data['uuid'], ])
65
            ->execute()
66
        ;
67
    }
68
69
    /**
70
     * @throws Exception
71
     */
72
    final public function delete(string $uuid): void
73
    {
74
        $this->connection->createQueryBuilder()
75
            ->delete('user')
76
            ->where('uuid = :uuid')
77
            ->setParameter('uuid', $uuid)
78
            ->execute()
79
        ;
80
    }
81
82
    /**
83
     * @throws Exception
84
     */
85
    final public function findOneByUuid(string $uuid): ?User
86
    {
87
        $result = $this->connection->createQueryBuilder()
88
            ->select('uuid', 'username', 'email', 'password', 'roles')
89
            ->from('user')
90
            ->where('uuid = :uuid')
91
            ->setParameter('uuid', $uuid)
92
            ->execute()
93
            ->fetchAssociative()
94
        ;
95
96
        return User::create(
97
            UserUuid::fromString($uuid),
98
            NameField::fromString($result['username']),
99
            EmailField::fromString($result['email']),
100
            '',
101
            \explode(',', $result['roles'])
102
        );
103
    }
104
105
    /**
106
     * @throws Exception
107
     */
108
    final public function existWithUsername(string $username): bool
109
    {
110
        $statement = $this->connection->createQueryBuilder()
111
            ->select('username')
112
            ->from('user')
113
            ->where('username = :username')
114
            ->setParameter('username', $username)
115
            ->execute()
116
            ->fetchOne()
117
        ;
118
119
        return false !== $statement;
120
    }
121
122
    /**
123
     * @throws Exception
124
     */
125
    final public function existWithEmail(string $email): bool
126
    {
127
        $statement = $this->connection->createQueryBuilder()
128
            ->select('email')
129
            ->from('user')
130
            ->where('email = :email')
131
            ->setParameter('email', $email)
132
            ->execute()
133
            ->fetchOne()
134
        ;
135
136
        return false !== $statement;
137
    }
138
}
139