This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace XHGui\Db; |
||
4 | |||
5 | use Generator; |
||
6 | use PDO; |
||
7 | use RuntimeException; |
||
8 | |||
9 | class PdoRepository |
||
10 | { |
||
11 | /** @var PDO */ |
||
12 | private $pdo; |
||
13 | |||
14 | /** @var string */ |
||
15 | private $table; |
||
16 | |||
17 | /** |
||
18 | * @param PDO $pdo An open database connection |
||
19 | * @param string $table Table name where Xhgui profiles are stored |
||
20 | */ |
||
21 | public function __construct(PDO $pdo, string $table) |
||
22 | { |
||
23 | $this->pdo = $pdo; |
||
24 | $this->table = sprintf('"%s"', $table); |
||
25 | $this->initSchema(); |
||
26 | } |
||
27 | |||
28 | View Code Duplication | public function getLatest(): array |
|
0 ignored issues
–
show
|
|||
29 | { |
||
30 | $query = sprintf(' |
||
31 | SELECT |
||
32 | "id", |
||
33 | "profile", |
||
34 | "url", |
||
35 | "SERVER", |
||
36 | "GET", |
||
37 | "ENV", |
||
38 | "simple_url", |
||
39 | "request_ts", |
||
40 | "request_ts_micro," |
||
41 | "request_date" |
||
42 | FROM %s |
||
43 | ORDER BY "request_date" ASC |
||
44 | LIMIT 1', |
||
45 | $this->table |
||
46 | ); |
||
47 | $stmt = $this->pdo->query($query); |
||
48 | |||
49 | $row = $stmt->fetch(PDO::FETCH_ASSOC); |
||
50 | if ($row === false) { |
||
51 | throw new RuntimeException('No profile available yet.'); |
||
52 | } |
||
53 | |||
54 | return $row; |
||
55 | } |
||
56 | |||
57 | View Code Duplication | public function getById(string $id): array |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
58 | { |
||
59 | $query = sprintf(' |
||
60 | SELECT |
||
61 | "profile", |
||
62 | "url", |
||
63 | "SERVER", |
||
64 | "GET", |
||
65 | "ENV", |
||
66 | "simple_url", |
||
67 | "request_ts", |
||
68 | "request_ts_micro", |
||
69 | "request_date" |
||
70 | FROM %s |
||
71 | WHERE id = :id |
||
72 | ', $this->table); |
||
73 | $stmt = $this->pdo->prepare($query); |
||
74 | $stmt->execute(['id' => $id]); |
||
75 | |||
76 | $row = $stmt->fetch(PDO::FETCH_ASSOC); |
||
77 | if ($row === false) { |
||
78 | throw new RuntimeException('No profile data found.'); |
||
79 | } |
||
80 | |||
81 | return $row; |
||
82 | } |
||
83 | |||
84 | public function countByUrl(string $url): int |
||
85 | { |
||
86 | $query = sprintf(' |
||
87 | SELECT COUNT(*) AS count |
||
88 | FROM %s |
||
89 | WHERE "simple_url" LIKE :url |
||
90 | ', $this->table); |
||
91 | $stmt = $this->pdo->prepare($query); |
||
92 | $stmt->execute(['url' => '%' . $url . '%']); |
||
93 | |||
94 | return (int)$stmt->fetchColumn(); |
||
95 | } |
||
96 | |||
97 | public function findByUrl(string $url, string $direction, int $skip, int $perPage): Generator |
||
98 | { |
||
99 | $query = sprintf(' |
||
100 | SELECT |
||
101 | "id", |
||
102 | "url", |
||
103 | "SERVER", |
||
104 | "GET", |
||
105 | "ENV", |
||
106 | "simple_url", |
||
107 | "request_ts", |
||
108 | "request_ts_micro", |
||
109 | "request_date", |
||
110 | "main_wt", |
||
111 | "main_ct", |
||
112 | "main_cpu", |
||
113 | "main_mu", |
||
114 | "main_pmu" |
||
115 | FROM %s |
||
116 | WHERE "simple_url" LIKE :url |
||
117 | ORDER BY "request_ts" %s |
||
118 | LIMIT %d OFFSET %d', |
||
119 | $this->table, |
||
120 | $direction, |
||
121 | $perPage, |
||
122 | $skip |
||
123 | ); |
||
124 | $stmt = $this->pdo->prepare($query); |
||
125 | $stmt->execute(['url' => '%' . $url . '%']); |
||
126 | |||
127 | while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { |
||
128 | yield $row; |
||
129 | } |
||
130 | } |
||
131 | |||
132 | public function deleteById(string $id): void |
||
133 | { |
||
134 | $stmt = $this->pdo->prepare(sprintf(' |
||
135 | DELETE FROM %s |
||
136 | WHERE id = :id |
||
137 | ', $this->table)); |
||
138 | |||
139 | $stmt->execute(['id' => $id]); |
||
140 | } |
||
141 | |||
142 | public function deleteAll() |
||
143 | { |
||
144 | return is_int( |
||
145 | $this->pdo->exec(sprintf('DELETE FROM %s', $this->table)) |
||
146 | ); |
||
147 | } |
||
148 | |||
149 | public function getStatistics() |
||
150 | { |
||
151 | $stmt = $this->pdo->query( |
||
152 | sprintf( |
||
153 | ' |
||
154 | SELECT |
||
155 | COUNT(*) AS profiles, |
||
156 | MAX("request_ts") AS latest, |
||
157 | SUM(LENGTH("profile")) AS bytes |
||
158 | FROM %s', |
||
159 | $this->table |
||
160 | ), |
||
161 | PDO::FETCH_ASSOC |
||
162 | ); |
||
163 | |||
164 | $row = $stmt->fetch(PDO::FETCH_ASSOC); |
||
165 | |||
166 | return $row ?: null; |
||
167 | } |
||
168 | |||
169 | public function initSchema(): void |
||
170 | { |
||
171 | $this->pdo->exec(sprintf(' |
||
172 | CREATE TABLE IF NOT EXISTS %s ( |
||
173 | "id" CHAR(24) PRIMARY KEY, |
||
174 | "profile" TEXT NOT NULL, |
||
175 | "url" TEXT NULL, |
||
176 | "SERVER" TEXT NULL, |
||
177 | "GET" TEXT NULL, |
||
178 | "ENV" TEXT NULL, |
||
179 | "simple_url" TEXT NULL, |
||
180 | "request_ts" INTEGER NOT NULL, |
||
181 | "request_ts_micro" NUMERIC(15, 4) NOT NULL, |
||
182 | "request_date" DATE NOT NULL, |
||
183 | "main_wt" INTEGER NOT NULL, |
||
184 | "main_ct" INTEGER NOT NULL, |
||
185 | "main_cpu" INTEGER NOT NULL, |
||
186 | "main_mu" INTEGER NOT NULL, |
||
187 | "main_pmu" INTEGER NOT NULL |
||
188 | ) |
||
189 | ', $this->table)); |
||
190 | } |
||
191 | |||
192 | public function saveProfile(array $data): void |
||
193 | { |
||
194 | $stmt = $this->pdo->prepare(sprintf(' |
||
195 | INSERT INTO %s ( |
||
196 | "id", |
||
197 | "profile", |
||
198 | "url", |
||
199 | "SERVER", |
||
200 | "GET", |
||
201 | "ENV", |
||
202 | "simple_url", |
||
203 | "request_ts", |
||
204 | "request_ts_micro", |
||
205 | "request_date", |
||
206 | "main_wt", |
||
207 | "main_ct", |
||
208 | "main_cpu", |
||
209 | "main_mu", |
||
210 | "main_pmu" |
||
211 | ) VALUES ( |
||
212 | :id, |
||
213 | :profile, |
||
214 | :url, |
||
215 | :SERVER, |
||
216 | :GET, |
||
217 | :ENV, |
||
218 | :simple_url, |
||
219 | :request_ts, |
||
220 | :request_ts_micro, |
||
221 | :request_date, |
||
222 | :main_wt, |
||
223 | :main_ct, |
||
224 | :main_cpu, |
||
225 | :main_mu, |
||
226 | :main_pmu |
||
227 | ) |
||
228 | ', $this->table)); |
||
229 | $stmt->execute($data); |
||
230 | } |
||
231 | } |
||
232 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.