Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
11.29% |
7 / 62 |
|
11.11% |
1 / 9 |
CRAP | |
0.00% |
0 / 1 |
Sql | |
11.29% |
7 / 62 |
|
11.11% |
1 / 9 |
392.29 | |
0.00% |
0 / 1 |
getPersistence | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
store | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
20 | |||
cleanInputData | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
20 | |||
exists | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
insert | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
2 | |||
tableize | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
update | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
flush | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
castValue | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
5 |
1 | <?php |
2 | |
3 | /** |
4 | * This program is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU General Public License |
6 | * as published by the Free Software Foundation; under version 2 |
7 | * of the License (non-upgradable). |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write to the Free Software |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | * |
18 | * Copyright (c) 2015-2023 (original work) Open Assessment Technologies SA. |
19 | */ |
20 | |
21 | namespace oat\taoClientDiagnostic\model\storage; |
22 | |
23 | use common_persistence_Persistence; |
24 | use oat\oatbox\service\ConfigurableService; |
25 | use oat\taoClientDiagnostic\exception\StorageException; |
26 | use Doctrine\DBAL\DBALException; |
27 | |
28 | /** |
29 | * Class Sql |
30 | * @package oat\taoClientDiagnostic\model\storage |
31 | */ |
32 | class Sql extends ConfigurableService implements Storage |
33 | { |
34 | /** |
35 | * Constant for diagnostic table name |
36 | */ |
37 | public const DIAGNOSTIC_TABLE = 'diagnostic_report'; |
38 | |
39 | /** |
40 | * Constant for persistence option |
41 | */ |
42 | public const DIAGNOSTIC_PERSISTENCE = 'persistence'; |
43 | |
44 | /** |
45 | * @var common_persistence_Persistence |
46 | */ |
47 | private $persistence; |
48 | |
49 | private const FIELDS_INT = [ |
50 | self::DIAGNOSTIC_BANDWIDTH_SIZE, |
51 | self::DIAGNOSTIC_INTENSIVE_BANDWIDTH_SIZE, |
52 | self::DIAGNOSTIC_PERFORMANCE_COUNT, |
53 | self::DIAGNOSTIC_BANDWIDTH_COUNT, |
54 | self::DIAGNOSTIC_FINGERPRINT_ERRORS, |
55 | self::DIAGNOSTIC_FINGERPRINT_CHANGED, |
56 | self::DIAGNOSTIC_INTENSIVE_BANDWIDTH_COUNT |
57 | ]; |
58 | |
59 | private const FIELDS_STRING = [ |
60 | self::DIAGNOSTIC_BROWSERVERSION, |
61 | self::DIAGNOSTIC_SCHOOL_NUMBER, |
62 | self::DIAGNOSTIC_OSVERSION, |
63 | ]; |
64 | |
65 | /** |
66 | * Get persistence with configurable driver option of Sql Storage |
67 | * Get default driver if option is not set |
68 | * @return common_persistence_Persistence |
69 | */ |
70 | public function getPersistence() |
71 | { |
72 | $persistenceOption = $this->getOption(self::DIAGNOSTIC_PERSISTENCE); |
73 | $persistence = (!empty($persistenceOption)) ? $persistenceOption : 'default'; |
74 | return \common_persistence_Manager::getPersistence($persistence); |
75 | } |
76 | |
77 | |
78 | /** |
79 | * If record already exists, update it by new values |
80 | * Else insert new entry |
81 | * @param $id |
82 | * @param array $data |
83 | * @return boolean |
84 | * @throws StorageException |
85 | */ |
86 | public function store($id, $data = array()) |
87 | { |
88 | try { |
89 | if (empty($id)) { |
90 | throw new StorageException('Invalid id parameter.'); |
91 | } |
92 | $this->persistence = $this->getPersistence(); |
93 | |
94 | $data = $this->cleanInputData($data); |
95 | if (!$this->exists($id)) { |
96 | $this->insert($id, $data); |
97 | } else { |
98 | $this->update($id, $data); |
99 | } |
100 | return true; |
101 | } catch (DBALException $e) { |
102 | throw new StorageException($e->getMessage()); |
103 | } |
104 | } |
105 | |
106 | /** |
107 | * Check if $input keys are constants of Storage |
108 | * @param array $input |
109 | * @return array |
110 | * @throws StorageException |
111 | */ |
112 | protected function cleanInputData(array $input) |
113 | { |
114 | foreach ($input as $key => $value) { |
115 | $const = get_called_class() . '::DIAGNOSTIC_' . strtoupper($key); |
116 | if (defined($const)) { |
117 | $data[constant($const)] = $this->castValue(constant($const), $value); |
118 | } |
119 | } |
120 | if (empty($data)) { |
121 | throw new StorageException('No data to insert into storage'); |
122 | } |
123 | |
124 | return $data; |
125 | } |
126 | |
127 | /** |
128 | * Check if record is already in database by $this->id |
129 | * @param $id |
130 | * @return bool |
131 | */ |
132 | private function exists($id) |
133 | { |
134 | $query = sprintf( |
135 | 'SELECT %s FROM %s WHERE %s = ?', |
136 | self::DIAGNOSTIC_ID, |
137 | self::DIAGNOSTIC_TABLE, |
138 | self::DIAGNOSTIC_ID |
139 | ); |
140 | $statement = $this->persistence->query($query, array($id)); |
141 | return (bool)$statement->rowCount(); |
142 | } |
143 | |
144 | |
145 | /** |
146 | * Create new record in SQL table with $data & $this->id |
147 | * @param $id |
148 | * @param $data |
149 | * @return mixed |
150 | */ |
151 | private function insert($id, $data) |
152 | { |
153 | $platform = $this->persistence->getPlatform(); |
154 | $columns = array_merge(array( |
155 | self::DIAGNOSTIC_ID => $id, |
156 | self::DIAGNOSTIC_CREATED_AT => $platform->getNowExpression() |
157 | ), $data); |
158 | |
159 | $query = 'INSERT INTO ' |
160 | . self::DIAGNOSTIC_TABLE |
161 | . '(' . implode(', ', array_map([$this, 'tableize'], array_keys($columns))) . ')' |
162 | . ' VALUES (' . str_repeat("?,", count($columns) - 1) . '? )'; |
163 | |
164 | return $this->persistence->exec($query, array_values($columns)); |
165 | } |
166 | |
167 | /** |
168 | * @param $word |
169 | * @return string |
170 | * @throws StorageException |
171 | */ |
172 | private function tableize($word) |
173 | { |
174 | $tableized = preg_replace('~(?<=\\w)([A-Z])~u', '_$1', $word); |
175 | if ($tableized === null) { |
176 | throw new StorageException(sprintf( |
177 | 'Invalid column name "%s"', |
178 | $word |
179 | )); |
180 | } |
181 | return mb_strtolower($tableized); |
182 | } |
183 | |
184 | /** |
185 | * Update record in SQL table with $data by $this->id |
186 | * @param $id |
187 | * @param $data |
188 | * @return mixed |
189 | */ |
190 | private function update($id, $data) |
191 | { |
192 | foreach ($data as $key => $value) { |
193 | $fields[] = $key . ' = ?'; |
194 | } |
195 | $query = 'UPDATE ' . self::DIAGNOSTIC_TABLE . ' SET ' . implode(', ', $fields) . |
196 | ' WHERE ' . self::DIAGNOSTIC_ID . ' = ?'; |
197 | return $this->persistence->exec($query, array_merge(array_values($data), array($id))); |
198 | } |
199 | |
200 | |
201 | public function flush() |
202 | { |
203 | $query = 'DELETE FROM ' . self::DIAGNOSTIC_TABLE; |
204 | |
205 | try { |
206 | $this->getPersistence()->exec($query); |
207 | } catch (DBALException $e) { |
208 | return false; |
209 | } |
210 | return true; |
211 | } |
212 | |
213 | /** |
214 | * @param string $field |
215 | * @param mixed $value |
216 | * @return float|int|string |
217 | */ |
218 | private function castValue($field, $value) |
219 | { |
220 | if (in_array($field, self::FIELDS_STRING)) { |
221 | return (string)$value; |
222 | } |
223 | |
224 | if (is_string($value)) { |
225 | if (in_array($field, self::FIELDS_INT)) { |
226 | return (int)$value; |
227 | } |
228 | return is_numeric($value) ? (float)$value : $value; |
229 | } |
230 | return $value; |
231 | } |
232 | } |