Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
67.95% covered (warning)
67.95%
53 / 78
66.67% covered (warning)
66.67%
6 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
UsersEntityDao
67.95% covered (warning)
67.95%
53 / 78
66.67% covered (warning)
66.67%
6 / 9
13.29
0.00% covered (danger)
0.00%
0 / 1
 selectExistsSetupUser
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 selectExistsLoginId
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 selectUserIdByLoginId
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
 selectUserInfoData
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 selectUserEditData
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 selectEmail
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 insertUser
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
1
 updateUserState
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 updateUserSetting
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace PeServer\App\Models\Dao\Entities;
6
7use PeServer\App\Models\Data\Dto\UserInformationDto;
8use PeServer\Core\Database\DaoBase;
9use PeServer\Core\Database\DaoTrait;
10use PeServer\Core\Database\DatabaseRowResult;
11use PeServer\Core\Database\IDatabaseContext;
12
13class UsersEntityDao extends DaoBase
14{
15    use DaoTrait;
16
17    #region function
18
19    public function selectExistsSetupUser(): bool
20    {
21        return (bool)$this->context->selectSingleCount(
22            <<<SQL
23
24            select
25                COUNT(*) as count
26            from
27                users
28            where
29                users.level = 'setup'
30                and
31                users.state = 'enabled'
32
33            SQL
34        );
35    }
36
37    public function selectExistsLoginId(string $loginId): bool
38    {
39        return (bool)$this->context->selectSingleCount(
40            <<<SQL
41
42            select
43                COUNT(*) as count
44            from
45                users
46            where
47                users.login_id = :login_id
48
49            SQL,
50            [
51                'login_id' => $loginId
52            ]
53        );
54    }
55
56    /**
57     * @param string $loginId
58     * @return string|null
59     */
60    public function selectUserIdByLoginId(string $loginId): string|null
61    {
62        /** @var DatabaseRowResult<array{user_id:string}>|null */
63        $row = $this->context->queryFirstOrNull(
64            <<<SQL
65
66            select
67                users.user_id
68            from
69                users
70            where
71                users.login_id = :login_id
72
73            SQL,
74            [
75                'login_id' => $loginId
76            ]
77        );
78
79        if ($row === null) {
80            return $row;
81        }
82        return $row->fields['user_id'];
83    }
84
85    /**
86     * @param string $userId
87     * @return UserInformationDto
88     */
89    public function selectUserInfoData(string $userId): UserInformationDto
90    {
91        $result = $this->context->querySingle(
92            <<<SQL
93
94            select
95                users.user_id,
96                users.login_id,
97                users.level,
98                users.name,
99                users.email,
100                users.website
101            from
102                users
103            where
104                users.user_id = :user_id
105
106            SQL,
107            [
108                'user_id' => $userId
109            ]
110        );
111
112        return $result->mapping(UserInformationDto::class);
113    }
114
115    /**
116     * @template TFieldArray of array{name:string,website:string}
117     * @param string $userId
118     * @return DatabaseRowResult
119     * @phpstan-return DatabaseRowResult<TFieldArray>
120     */
121    public function selectUserEditData(string $userId): DatabaseRowResult
122    {
123        /** @phpstan-var DatabaseRowResult<TFieldArray> */
124        return $this->context->querySingle(
125            <<<SQL
126
127            select
128                users.name,
129                users.website,
130                users.description
131            from
132                users
133            where
134                users.user_id = :user_id
135
136            SQL,
137            [
138                'user_id' => $userId
139            ]
140        );
141    }
142
143    /**
144     * @param string $userId
145     * @return string
146     */
147    public function selectEmail(string $userId): string
148    {
149        /** @phpstan-var DatabaseRowResult<array{email:string}> */
150        $result = $this->context->querySingle(
151            <<<SQL
152
153            select
154                users.email
155            from
156                users
157            where
158                users.user_id = :user_id
159
160            SQL,
161            [
162                'user_id' => $userId
163            ]
164        );
165
166        return $result->fields['email'];
167    }
168
169    public function insertUser(string $userId, string $loginId, string $level, string $state, string $userName, string $email, int $markEmail, string $website, string $description, string $note): void
170    {
171        $this->context->insertSingle(
172            <<<SQL
173
174            insert into
175                users
176                (
177                    user_id,
178                    login_id,
179                    level,
180                    state,
181                    name,
182                    email,
183                    mark_email,
184                    website,
185                    description,
186                    note
187                )
188                values
189                (
190                    :user_id,
191                    :login_id,
192                    :level,
193                    :state,
194                    :name,
195                    :email,
196                    :mark_email,
197                    :website,
198                    :description,
199                    :note
200                )
201
202            SQL,
203            [
204                'user_id' => $userId,
205                'login_id' => $loginId,
206                'level' => $level,
207                'state' => $state,
208                'name' => $userName,
209                'email' => $email,
210                'mark_email' => $markEmail,
211                'website' => $website,
212                'description' => $description,
213                'note' => $note,
214            ]
215        );
216    }
217
218    public function updateUserState(string $userId, string $state): void
219    {
220        $this->context->updateByKey(
221            <<<SQL
222
223            update
224                users
225            set
226                state = :state
227            where
228                user_id = :user_id
229
230            SQL,
231            [
232                'user_id' => $userId,
233                'state' => $state,
234            ]
235        );
236    }
237
238    public function updateUserSetting(string $userId, string $userName, string $website, string $description): void
239    {
240        $this->context->updateByKey(
241            <<<SQL
242
243            update
244                users
245            set
246                name = :name,
247                website = :website,
248                description = :description
249            where
250                user_id = :user_id
251
252            SQL,
253            [
254                'user_id' => $userId,
255                'name' => $userName,
256                'website' => $website,
257                'description' => $description,
258            ]
259        );
260    }
261
262    #endregion
263}