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