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

DoctrineUserRepository::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
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 Core\Domain\Common\Model\VO\EmailField;
20
use Core\Domain\Common\Model\VO\NameField;
21
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...
22
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...
23
24
class DoctrineUserRepository implements UserRepositoryProtocol
25
{
26
    protected Connection $connection;
27
28
    public function __construct(Connection $connection)
29
    {
30
        $this->connection = $connection;
31
    }
32
33
    /**
34
     * @throws \Doctrine\DBAL\Driver\Exception|Exception
35
     */
36
    final public function add(User $user): void
37
    {
38
        $data = $this->getData($user);
39
40
        $statement = $this->connection->prepare(
41
            'INSERT INTO user
42
(uuid, username, email, password, roles) VALUES (:uuid, :username, :email, :password, :roles)'
43
        );
44
        $statement->execute($data);
45
    }
46
47
    /**
48
     * @throws \Doctrine\DBAL\Driver\Exception|Exception
49
     */
50
    final public function update(User $user): void
51
    {
52
        $data = $this->getData($user);
53
54
        $statement = $this->connection->prepare(
55
            'UPDATE user SET
56
uuid = :uuid, username = :username, email = :email, password = :password, roles = :roles
57
WHERE uuid = :uuid'
58
        );
59
        $statement->execute($data);
60
    }
61
62
    /**
63
     * @throws \Doctrine\DBAL\Driver\Exception|Exception
64
     */
65
    final public function delete(string $uuid): void
66
    {
67
        $statement = $this->connection->prepare('DELETE FROM user WHERE uuid = :uuid');
68
        $statement->bindParam('uuid', $uuid);
69
        $statement->execute();
70
    }
71
72
    /**
73
     * @throws \Doctrine\DBAL\Driver\Exception|Exception
74
     */
75
    final public function findOneByUuid(string $uuid): ?User
76
    {
77
        $query = <<<'SQL'
78
SELECT
79
    user.uuid as uuid,
80
    user.username as username,
81
    user.email as email,
82
    user.roles as roles
83
FROM user
84
WHERE uuid = :uuid
85
SQL;
86
        $result = $this->connection->executeQuery($query, ['uuid' => $uuid])->fetchAssociative();
87
88
        return User::create(
89
            UserUuid::fromString($uuid),
90
            NameField::fromString($result['username']),
91
            EmailField::fromString($result['email']),
92
            '',
93
            \explode(',', $result['roles'])
94
        );
95
    }
96
97
    /**
98
     * @throws \Doctrine\DBAL\Driver\Exception|Exception
99
     */
100
    final public function existWithUsername(string $username): bool
101
    {
102
        $query = <<<'SQL'
103
SELECT username FROM user
104
WHERE username = :username
105
SQL;
106
        $statement = $this->connection->executeQuery($query, ['username' => $username])->fetchAssociative();
107
108
        return false !== $statement;
109
    }
110
111
    /**
112
     * @throws \Doctrine\DBAL\Driver\Exception|Exception
113
     */
114
    final public function existWithEmail(string $email): bool
115
    {
116
        $query = <<<'SQL'
117
SELECT email FROM user
118
WHERE email = :email
119
SQL;
120
        $statement = $this->connection->executeQuery($query, ['email' => $email])->fetchAssociative();
121
122
        return false !== $statement;
123
    }
124
125
    private function getData(User $user): array
126
    {
127
        return [
128
            'uuid' => $user->uuid()->toString(),
129
            'username' => $user->username(),
130
            'email' => $user->email()->getValue(),
131
            'password' => $user->password(),
132
            'roles' => \implode(',', $user->roles()),
133
        ];
134
    }
135
}
136