LogRepository.php
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Log; |
| 4 | |
| 5 | use Give\Framework\Exceptions\Primitives\InvalidArgumentException; |
| 6 | use Give\Framework\Database\DB; |
| 7 | use WP_REST_Request; |
| 8 | use DateTime; |
| 9 | |
| 10 | /** |
| 11 | * Class LogRepository |
| 12 | * @package Give\Log\Repositories |
| 13 | * |
| 14 | * @since 2.10.0 |
| 15 | */ |
| 16 | class LogRepository { |
| 17 | |
| 18 | const SORTABLE_COLUMNS = [ 'id', 'category', 'source', 'log_type', 'date' ]; |
| 19 | |
| 20 | const LOGS_PER_PAGE = 10; |
| 21 | |
| 22 | /** |
| 23 | * @var string |
| 24 | */ |
| 25 | private $log_table; |
| 26 | |
| 27 | /** |
| 28 | * LogRepository constructor. |
| 29 | */ |
| 30 | public function __construct() { |
| 31 | global $wpdb; |
| 32 | $this->log_table = "{$wpdb->prefix}give_log"; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Insert Log into database |
| 37 | * |
| 38 | * @param LogModel $model |
| 39 | * |
| 40 | * @return int inserted log id |
| 41 | */ |
| 42 | public function insertLog( LogModel $model ) { |
| 43 | DB::insert( |
| 44 | $this->log_table, |
| 45 | [ |
| 46 | 'log_type' => $model->getType(), |
| 47 | 'data' => $model->getData( $jsonEncode = true ), |
| 48 | 'category' => $model->getCategory(), |
| 49 | 'source' => $model->getSource(), |
| 50 | 'date' => $model->getDate(), |
| 51 | ], |
| 52 | null |
| 53 | ); |
| 54 | |
| 55 | return DB::last_insert_id(); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Update log |
| 60 | * |
| 61 | * @param LogModel $model |
| 62 | * |
| 63 | * @return false|int |
| 64 | */ |
| 65 | public function updateLog( LogModel $model ) { |
| 66 | return DB::update( |
| 67 | $this->log_table, |
| 68 | [ |
| 69 | 'log_type' => $model->getType(), |
| 70 | 'data' => $model->getData( $jsonEncode = true ), |
| 71 | 'category' => $model->getCategory(), |
| 72 | 'source' => $model->getSource(), |
| 73 | 'date' => $model->getDate(), |
| 74 | ], |
| 75 | [ |
| 76 | 'id' => $model->getId(), |
| 77 | ] |
| 78 | ); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Get all logs |
| 83 | * |
| 84 | * @return LogModel[] |
| 85 | */ |
| 86 | public function getLogs() { |
| 87 | $logs = []; |
| 88 | $result = DB::get_results( "SELECT * FROM {$this->log_table} ORDER BY id DESC" ); |
| 89 | |
| 90 | if ( $result ) { |
| 91 | foreach ( $result as $log ) { |
| 92 | $data = json_decode( $log->data, true ); |
| 93 | |
| 94 | $logs[] = LogFactory::make( |
| 95 | $log->log_type, |
| 96 | $data['message'], |
| 97 | $log->category, |
| 98 | $log->source, |
| 99 | $data['context'], |
| 100 | $log->id, |
| 101 | $log->date |
| 102 | ); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | return $logs; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Get all logs for request |
| 111 | * |
| 112 | * @param WP_REST_Request $request |
| 113 | * |
| 114 | * @return LogModel[] |
| 115 | */ |
| 116 | public function getLogsForRequest( WP_REST_Request $request ) { |
| 117 | $logs = []; |
| 118 | |
| 119 | $type = $request->get_param( 'type' ); |
| 120 | $category = $request->get_param( 'category' ); |
| 121 | $source = $request->get_param( 'source' ); |
| 122 | $page = $request->get_param( 'page' ); |
| 123 | $sortBy = $request->get_param( 'sort' ); |
| 124 | $startDate = $request->get_param( 'start' ); |
| 125 | $endDate = $request->get_param( 'end' ); |
| 126 | $sortDirection = $request->get_param( 'direction' ); |
| 127 | |
| 128 | $offset = ( $page - 1 ) * self::LOGS_PER_PAGE; |
| 129 | |
| 130 | $query = "SELECT * FROM {$this->log_table} WHERE 1=1"; |
| 131 | |
| 132 | if ( $type ) { |
| 133 | $query .= sprintf( ' AND log_type = "%s"', esc_sql( $type ) ); |
| 134 | } |
| 135 | |
| 136 | if ( $category ) { |
| 137 | $query .= sprintf( ' AND category = "%s"', esc_sql( $category ) ); |
| 138 | } |
| 139 | |
| 140 | if ( $source ) { |
| 141 | $query .= sprintf( ' AND source = "%s"', esc_sql( $source ) ); |
| 142 | } |
| 143 | |
| 144 | if ( $startDate ) { |
| 145 | $startDate = new DateTime( $startDate ); |
| 146 | $query .= sprintf( " AND date(date) >= '%s'", $startDate->format( 'Y-m-d' ) ); |
| 147 | } |
| 148 | |
| 149 | if ( $endDate ) { |
| 150 | $endDate = new DateTime( $endDate ); |
| 151 | $query .= sprintf( " AND date(date) <= '%s'", $endDate->format( 'Y-m-d' ) ); |
| 152 | } |
| 153 | |
| 154 | if ( $sortBy ) { |
| 155 | $column = ( in_array( $sortBy, self::SORTABLE_COLUMNS, true ) ) ? $sortBy : 'id'; |
| 156 | $direction = ( $sortDirection && strtoupper( $sortDirection ) === 'ASC' ) ? 'ASC' : 'DESC'; |
| 157 | |
| 158 | $query .= " ORDER BY `{$column}` {$direction}"; |
| 159 | } else { |
| 160 | $query .= ' ORDER BY id DESC'; |
| 161 | } |
| 162 | |
| 163 | // Limit |
| 164 | $query .= sprintf( ' LIMIT %d', self::LOGS_PER_PAGE ); |
| 165 | |
| 166 | // Offset |
| 167 | if ( $offset > 1 ) { |
| 168 | $query .= sprintf( ' OFFSET %d', $offset ); |
| 169 | } |
| 170 | |
| 171 | $result = DB::get_results( $query ); |
| 172 | |
| 173 | if ( $result ) { |
| 174 | foreach ( $result as $log ) { |
| 175 | $data = json_decode( $log->data, true ); |
| 176 | |
| 177 | $logs[] = LogFactory::make( |
| 178 | $log->log_type, |
| 179 | $data['message'], |
| 180 | $log->category, |
| 181 | $log->source, |
| 182 | $data['context'], |
| 183 | $log->id, |
| 184 | $log->date |
| 185 | ); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | return $logs; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Get log by ID |
| 194 | * |
| 195 | * @param int $logId |
| 196 | * |
| 197 | * @return LogModel|null |
| 198 | */ |
| 199 | public function getLog( $logId ) { |
| 200 | $log = DB::get_row( |
| 201 | DB::prepare( "SELECT * FROM {$this->log_table} WHERE id = %d", $logId ) |
| 202 | ); |
| 203 | |
| 204 | if ( $log ) { |
| 205 | $data = json_decode( $log->data, true ); |
| 206 | |
| 207 | return LogFactory::make( |
| 208 | $log->log_type, |
| 209 | $data['message'], |
| 210 | $log->category, |
| 211 | $log->source, |
| 212 | $data['context'], |
| 213 | $log->id, |
| 214 | $log->date |
| 215 | ); |
| 216 | } |
| 217 | |
| 218 | return null; |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Get logs by type |
| 223 | * |
| 224 | * @param string $type |
| 225 | * |
| 226 | * @return LogModel[] |
| 227 | */ |
| 228 | public function getLogsByType( $type ) { |
| 229 | $logs = []; |
| 230 | |
| 231 | $result = DB::get_results( |
| 232 | DB::prepare( "SELECT * FROM {$this->log_table} WHERE log_type = %s", $type ) |
| 233 | ); |
| 234 | |
| 235 | if ( $result ) { |
| 236 | foreach ( $result as $log ) { |
| 237 | $data = json_decode( $log->data, true ); |
| 238 | |
| 239 | $logs[] = LogFactory::make( |
| 240 | $log->log_type, |
| 241 | $data['message'], |
| 242 | $log->category, |
| 243 | $log->source, |
| 244 | $data['context'], |
| 245 | $log->id, |
| 246 | $log->date |
| 247 | ); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | return $logs; |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Get logs by category |
| 256 | * |
| 257 | * @param string $category |
| 258 | * |
| 259 | * @return LogModel[] |
| 260 | */ |
| 261 | public function getLogsByCategory( $category ) { |
| 262 | $logs = []; |
| 263 | |
| 264 | $result = DB::get_results( |
| 265 | DB::prepare( "SELECT * FROM {$this->log_table} WHERE category = %s", $category ) |
| 266 | ); |
| 267 | |
| 268 | if ( $result ) { |
| 269 | foreach ( $result as $log ) { |
| 270 | $data = json_decode( $log->data, true ); |
| 271 | |
| 272 | $logs[] = LogFactory::make( |
| 273 | $log->log_type, |
| 274 | $data['message'], |
| 275 | $log->category, |
| 276 | $log->source, |
| 277 | $data['context'], |
| 278 | $log->id, |
| 279 | $log->date |
| 280 | ); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | return $logs; |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Get logs categories |
| 289 | * |
| 290 | * @return array |
| 291 | */ |
| 292 | public function getCategories() { |
| 293 | $categories = []; |
| 294 | $result = DB::get_results( "SELECT DISTINCT category FROM {$this->log_table}" ); |
| 295 | |
| 296 | if ( $result ) { |
| 297 | foreach ( $result as $category ) { |
| 298 | $categories[] = $category->category; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | return $categories; |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Get logs sources |
| 307 | * |
| 308 | * @return array |
| 309 | */ |
| 310 | public function getSources() { |
| 311 | $sources = []; |
| 312 | $result = DB::get_results( "SELECT DISTINCT source FROM {$this->log_table}" ); |
| 313 | |
| 314 | if ( $result ) { |
| 315 | foreach ( $result as $source ) { |
| 316 | $sources[] = $source->source; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | return $sources; |
| 321 | } |
| 322 | |
| 323 | |
| 324 | /** |
| 325 | * Delete all logs |
| 326 | */ |
| 327 | public function deleteLogs() { |
| 328 | DB::query( "DELETE FROM {$this->log_table}" ); |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * Delete log by ID |
| 333 | * |
| 334 | * @param int $logId |
| 335 | */ |
| 336 | public function deleteLog( $logId ) { |
| 337 | DB::query( |
| 338 | DB::prepare( "DELETE FROM {$this->log_table} WHERE id = %d", $logId ) |
| 339 | ); |
| 340 | } |
| 341 | |
| 342 | public function getTotalCount() { |
| 343 | return DB::get_var( "SELECT count(id) FROM {$this->log_table}" ); |
| 344 | } |
| 345 | |
| 346 | /** |
| 347 | * Get log count by column name containing value |
| 348 | * |
| 349 | * @param string $columnName |
| 350 | * @param string $value |
| 351 | * |
| 352 | * @return string|null |
| 353 | */ |
| 354 | public function getLogCountBy( $columnName, $value ) { |
| 355 | if ( ! in_array( $columnName, self::SORTABLE_COLUMNS, true ) ) { |
| 356 | throw new InvalidArgumentException( |
| 357 | sprintf( 'Invalid column %s', $columnName ) |
| 358 | ); |
| 359 | } |
| 360 | return DB::get_var( |
| 361 | DB::prepare( "SELECT count(id) FROM {$this->log_table} WHERE {$columnName}=%s", $value ) |
| 362 | ); |
| 363 | } |
| 364 | |
| 365 | |
| 366 | /** |
| 367 | * Get log count for request |
| 368 | * |
| 369 | * @param WP_REST_Request $request |
| 370 | * |
| 371 | * @return int|null |
| 372 | */ |
| 373 | public function getLogCountForRequest( WP_REST_Request $request ) { |
| 374 | $type = $request->get_param( 'type' ); |
| 375 | $category = $request->get_param( 'category' ); |
| 376 | $source = $request->get_param( 'source' ); |
| 377 | $startDate = $request->get_param( 'start' ); |
| 378 | $endDate = $request->get_param( 'end' ); |
| 379 | |
| 380 | $query = "SELECT count(id) FROM {$this->log_table} WHERE 1=1"; |
| 381 | |
| 382 | if ( $type ) { |
| 383 | $query .= sprintf( ' AND log_type = "%s"', esc_sql( $type ) ); |
| 384 | } |
| 385 | |
| 386 | if ( $category ) { |
| 387 | $query .= sprintf( ' AND category = "%s"', esc_sql( $category ) ); |
| 388 | } |
| 389 | |
| 390 | if ( $source ) { |
| 391 | $query .= sprintf( ' AND source = "%s"', esc_sql( $source ) ); |
| 392 | } |
| 393 | |
| 394 | if ( $startDate ) { |
| 395 | $startDate = new DateTime( $startDate ); |
| 396 | $query .= sprintf( " AND date(date) >= '%s'", $startDate->format( 'Y-m-d' ) ); |
| 397 | } |
| 398 | |
| 399 | if ( $endDate ) { |
| 400 | $endDate = new DateTime( $endDate ); |
| 401 | $query .= sprintf( " AND date(date) <= '%s'", $endDate->format( 'Y-m-d' ) ); |
| 402 | } |
| 403 | |
| 404 | return DB::get_var( $query ); |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * Get sortable columns |
| 409 | * |
| 410 | * @return string[] |
| 411 | */ |
| 412 | public function getSortableColumns() { |
| 413 | return self::SORTABLE_COLUMNS; |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * Get logs per page limit |
| 418 | * |
| 419 | * @return int |
| 420 | */ |
| 421 | public function getLogsPerPageLimit() { |
| 422 | return self::LOGS_PER_PAGE; |
| 423 | } |
| 424 | |
| 425 | /** |
| 426 | * Flush logs |
| 427 | */ |
| 428 | public function flushLogs() { |
| 429 | DB::query( "DELETE FROM {$this->log_table}" ); |
| 430 | } |
| 431 | } |
| 432 |