|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Services; |
| 4 | + |
| 5 | +use PDO; // Import the PDO class for database connection |
| 6 | +use PDOException; // Import the PDOException class for handling connection errors |
| 7 | + |
| 8 | +// Database class responsible for managing database connections and queries |
| 9 | +class Database |
| 10 | +{ |
| 11 | + private PDO $pdo; // PDO instance for interacting with the database |
| 12 | + |
| 13 | + // Constructor method to initialize the database connection |
| 14 | + public function __construct(array $config) |
| 15 | + { |
| 16 | + try { |
| 17 | + // Data Source Name (DSN) for MySQL connection with UTF-8 charset |
| 18 | + $dsn = "mysql:host={$config['host']};dbname={$config['dbname']};charset=utf8"; |
| 19 | + // Create a new PDO instance with the provided configuration (host, dbname, user, pass) |
| 20 | + $this->pdo = new PDO($dsn, $config['user'], $config['pass']); |
| 21 | + // Set the PDO error mode to throw exceptions for better error handling |
| 22 | + $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
| 23 | + } catch (PDOException $e) { |
| 24 | + // If the connection fails, output the error message and stop execution |
| 25 | + die("Database connection error: " . $e->getMessage()); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + // Method to retrieve all pages that need to be monitored from the 'pages' table |
| 30 | + public function getPages(): array |
| 31 | + { |
| 32 | + // Prepare a SQL query to fetch 'id', 'url', and 'optional_text' fields from the 'pages' table |
| 33 | + $stmt = $this->pdo->prepare("SELECT id, url, optional_text FROM pages"); |
| 34 | + // Execute the query |
| 35 | + $stmt->execute(); |
| 36 | + // Fetch and return the results as an associative array |
| 37 | + return $stmt->fetchAll(PDO::FETCH_ASSOC); |
| 38 | + } |
| 39 | + |
| 40 | + // Method to retrieve all admin email addresses from the 'admins' table |
| 41 | + public function getAdmins(): array |
| 42 | + { |
| 43 | + // Prepare a SQL query to fetch the 'email' field from the 'admins' table |
| 44 | + $stmt = $this->pdo->prepare("SELECT email FROM admins"); |
| 45 | + // Execute the query |
| 46 | + $stmt->execute(); |
| 47 | + // Fetch and return the email addresses as a simple array (1-dimensional) |
| 48 | + return $stmt->fetchAll(PDO::FETCH_COLUMN); |
| 49 | + } |
| 50 | + |
| 51 | + // Method to insert monitoring results into the 'page_monitoring' table |
| 52 | + public function insertMonitoringResult(array $data): void |
| 53 | + { |
| 54 | + // SQL query to insert monitoring data into the 'page_monitoring' table with placeholders for dynamic data |
| 55 | + $sql = "INSERT INTO page_monitoring (page_id, response_code, load_time, html_length, text_found, total_time, checked_at, error_message, admin_notified) |
| 56 | + VALUES (:page_id, :response_code, :load_time, :html_length, :text_found, :total_time, NOW(), :error_message, :admin_notified)"; |
| 57 | + // Prepare the SQL statement |
| 58 | + $stmt = $this->pdo->prepare($sql); |
| 59 | + // Execute the prepared statement with the provided data array |
| 60 | + $stmt->execute($data); |
| 61 | + } |
| 62 | +} |
0 commit comments