1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Domain\User\Repository; |
4
|
|
|
|
5
|
|
|
use App\Domain\User\Data\UserData; |
6
|
|
|
use App\Domain\User\Data\UserResultData; |
7
|
|
|
use App\Infrastructure\Factory\QueryFactory; |
|
|
|
|
8
|
|
|
use App\Infrastructure\Utility\Hydrator; |
9
|
|
|
|
10
|
|
|
class UserFinderRepository |
11
|
|
|
{ |
12
|
|
|
// Fields without password |
13
|
|
|
private array $fields = [ |
14
|
|
|
'id', |
15
|
|
|
'first_name', |
16
|
|
|
'surname', |
17
|
|
|
'email', |
18
|
|
|
'user_role_id', |
19
|
|
|
'status', |
20
|
|
|
'updated_at', |
21
|
|
|
'created_at', |
22
|
|
|
'theme', |
23
|
|
|
'language', |
24
|
|
|
]; |
25
|
|
|
|
26
|
202 |
|
public function __construct( |
27
|
|
|
private readonly QueryFactory $queryFactory, |
28
|
|
|
private readonly Hydrator $hydrator |
29
|
|
|
) { |
30
|
202 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Return all users. |
34
|
|
|
* |
35
|
|
|
* @return UserData[] |
36
|
|
|
*/ |
37
|
21 |
|
public function findAllUsers(): array |
38
|
|
|
{ |
39
|
|
|
// Convert to list of objects |
40
|
21 |
|
return $this->hydrator->hydrate($this->findAllUserRows(), UserData::class); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Return all users with as UserResultData instance. |
45
|
|
|
* |
46
|
|
|
* @return UserResultData[] |
47
|
|
|
*/ |
48
|
4 |
|
public function findAllUsersForList(): array |
49
|
|
|
{ |
50
|
4 |
|
return $this->hydrator->hydrate($this->findAllUserRows(), UserResultData::class); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Returns array of user rows. |
55
|
|
|
* |
56
|
|
|
* @return array |
57
|
|
|
*/ |
58
|
25 |
|
public function findAllUserRows(): array |
59
|
|
|
{ |
60
|
25 |
|
$query = $this->queryFactory->selectQuery()->select($this->fields)->from('user')->where( |
61
|
25 |
|
['deleted_at IS' => null] |
62
|
25 |
|
); |
63
|
|
|
|
64
|
25 |
|
return $query->execute()->fetchAll('assoc') ?: []; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Return user with given id if it exists |
69
|
|
|
* otherwise null. |
70
|
|
|
* |
71
|
|
|
* @param int $id |
72
|
|
|
* |
73
|
|
|
* @return array user row |
74
|
|
|
*/ |
75
|
156 |
|
public function findUserById(int $id): array |
76
|
|
|
{ |
77
|
156 |
|
$query = $this->queryFactory->selectQuery()->select($this->fields)->from('user')->where( |
78
|
156 |
|
['deleted_at IS' => null, 'id' => $id] |
79
|
156 |
|
); |
80
|
|
|
|
81
|
|
|
// Empty array if not found |
82
|
156 |
|
return $query->execute()->fetch('assoc') ?: []; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Return user with password hash if it exists |
87
|
|
|
* otherwise null. |
88
|
|
|
* |
89
|
|
|
* @param int $id |
90
|
|
|
* |
91
|
|
|
* @return UserData |
92
|
|
|
*/ |
93
|
4 |
|
public function findUserByIdWithPasswordHash(int $id): UserData |
94
|
|
|
{ |
95
|
4 |
|
$query = $this->queryFactory->selectQuery()->select(['*'])->from('user')->where( |
96
|
4 |
|
['deleted_at IS' => null, 'id' => $id] |
97
|
4 |
|
); |
98
|
4 |
|
$userValues = $query->execute()->fetch('assoc') ?: []; |
99
|
|
|
|
100
|
|
|
// Empty user object if not found |
101
|
|
|
// $notRestricted true as values are safe as they come from the database. It's not a user input. |
102
|
4 |
|
return new UserData($userValues); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Return user with given id if it exists |
107
|
|
|
* If there is no user, an empty object is returned because: |
108
|
|
|
* > It is considered a best practice to NEVER return null when returning a collection or enumerable |
109
|
|
|
* Source: https://stackoverflow.com/a/1970001/9013718. |
110
|
|
|
* |
111
|
|
|
* @param string|null $email |
112
|
|
|
* |
113
|
|
|
* @return UserData |
114
|
|
|
*/ |
115
|
8 |
|
public function findUserByEmail(?string $email): UserData |
116
|
|
|
{ |
117
|
8 |
|
$query = $this->queryFactory->selectQuery()->select(['*'])->from('user')->andWhere( |
118
|
8 |
|
['deleted_at IS' => null, 'email' => $email] |
119
|
8 |
|
); |
120
|
|
|
|
121
|
8 |
|
$userValues = $query->execute()->fetch('assoc') ?: []; |
122
|
|
|
|
123
|
|
|
// Empty user object if not found |
124
|
|
|
// $notRestricted true as values are safe as they come from the database. It's not a user input. |
125
|
8 |
|
return new UserData($userValues); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Checks if user with given email already exists. |
130
|
|
|
* |
131
|
|
|
* @param string $email |
132
|
|
|
* @param int|null $userIdToExclude exclude user that already has the email from check (for update) |
133
|
|
|
* |
134
|
|
|
* @return bool |
135
|
|
|
*/ |
136
|
12 |
|
public function userWithEmailAlreadyExists(string $email, ?int $userIdToExclude = null): bool |
137
|
|
|
{ |
138
|
12 |
|
$query = $this->queryFactory->selectQuery()->select(['id'])->from('user')->andWhere( |
139
|
12 |
|
['deleted_at IS' => null, 'email' => $email] |
140
|
12 |
|
); |
141
|
|
|
|
142
|
12 |
|
if ($userIdToExclude !== null) { |
143
|
6 |
|
$query->andWhere(['id !=' => $userIdToExclude]); |
144
|
|
|
} |
145
|
|
|
|
146
|
12 |
|
return $query->execute()->fetch('assoc') !== false; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths