Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
39 / 39
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
ApiKeysEntityDao
100.00% covered (success)
100.00%
39 / 39
100.00% covered (success)
100.00%
5 / 5
5
100.00% covered (success)
100.00%
1 / 1
 selectExistsApiKeyByUserId
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 selectExistsApiKeyByApiKey
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 selectApiKeyByUserId
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 insertApiKey
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 deleteApiKeyByUserId
100.00% covered (success)
100.00%
7 / 7
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 DateTimeImmutable;
8use PeServer\Core\Database\DaoBase;
9use PeServer\Core\Database\DaoTrait;
10use PeServer\Core\Database\DatabaseRowResult;
11use PeServer\Core\Database\IDatabaseContext;
12
13class ApiKeysEntityDao extends DaoBase
14{
15    use DaoTrait;
16
17    #region function
18
19    public function selectExistsApiKeyByUserId(string $userId): bool
20    {
21        $result = $this->context->selectSingleCount(
22            <<<SQL
23
24            select
25                count(*)
26            from
27                api_keys
28            where
29                api_keys.user_id = :user_id
30
31            SQL,
32            [
33                'user_id' => $userId,
34            ]
35        );
36
37        return $result === 1;
38    }
39
40    public function selectExistsApiKeyByApiKey(string $apiKey): bool
41    {
42        $result = $this->context->selectSingleCount(
43            <<<SQL
44
45            select
46                count(*)
47            from
48                api_keys
49            where
50                api_keys.api_key = :api_key
51
52            SQL,
53            [
54                'api_key' => $apiKey,
55            ]
56        );
57
58        return $result === 1;
59    }
60
61    /**
62     * Undocumented function
63     *
64     * @param string $userId
65     * @return DatabaseRowResult
66     * @phpstan-return DatabaseRowResult<array{api_key:string,user_id:string,secret_key:string,created_timestamp:string}>
67     */
68    public function selectApiKeyByUserId(string $userId): DatabaseRowResult
69    {
70        /** @phpstan-var DatabaseRowResult<array{api_key:string,user_id:string,secret_key:string,created_timestamp:string}> */
71        return $this->context->querySingle(
72            <<<SQL
73
74            select
75                api_keys.api_key,
76                api_keys.user_id,
77                api_keys.secret_key,
78                api_keys.created_timestamp
79            from
80                api_keys
81            where
82                api_keys.user_id = :user_id
83
84            SQL,
85            [
86                'user_id' => $userId,
87            ]
88        );
89    }
90
91    public function insertApiKey(string $userId, string $apiKey, string $secret): void
92    {
93        $this->context->insertSingle(
94            <<<SQL
95
96            insert into
97                api_keys
98                (
99                    api_key,
100                    user_id,
101                    secret_key,
102                    created_timestamp
103                )
104                values
105                (
106                    :api_key,
107                    :user_id,
108                    :secret_key,
109                    CURRENT_TIMESTAMP
110                )
111
112            SQL,
113            [
114                'user_id' => $userId,
115                'api_key' => $apiKey,
116                'secret_key' => $secret,
117            ]
118        );
119    }
120
121    public function deleteApiKeyByUserId(string $userId): int
122    {
123        return $this->context->delete(
124            <<<SQL
125
126            delete from
127                api_keys
128            where
129                api_keys.user_id = :user_id
130
131            SQL,
132            [
133                'user_id' => $userId,
134            ]
135        );
136    }
137
138    #endregion
139}