WizdomWeb/app/Models/ClientModel.php

106 lines
2.7 KiB
PHP

<?php
namespace WizdomNetworks\WizeWeb\Models;
use WizdomNetworks\WizeWeb\Utils\Logger;
use WizdomNetworks\WizeWeb\Utils\ErrorHandler;
/**
* Client Model
*
* Handles database operations related to clients.
*/
class ClientModel
{
private $db;
public function __construct($db)
{
$this->db = $db;
}
/**
* Get a client by ID.
*
* @param int $id
* @return array|null
*/
public function getClientById(int $id): ?array
{
try {
Logger::info("[DEBUG] Fetching client with ID: $id");
$stmt = $this->db->prepare("SELECT * FROM clients WHERE id = :id");
$stmt->bindParam(':id', $id, \PDO::PARAM_INT);
$stmt->execute();
$client = $stmt->fetch(\PDO::FETCH_ASSOC);
Logger::info("[DEBUG] Client data retrieved: " . json_encode($client));
return $client ?: null;
} catch (\Exception $e) {
Logger::error("[ERROR] Failed to fetch client with ID $id: " . $e->getMessage());
ErrorHandler::exception($e);
return null;
}
}
/**
* Add a new client to the database.
*
* @param array $clientData
* @return bool
*/
public function addClient(array $clientData): bool
{
try {
Logger::info("[DEBUG] Adding new client: " . json_encode($clientData));
$stmt = $this->db->prepare(
"INSERT INTO clients (name, email, phone) VALUES (:name, :email, :phone)"
);
$stmt->bindParam(':name', $clientData['name']);
$stmt->bindParam(':email', $clientData['email']);
$stmt->bindParam(':phone', $clientData['phone']);
$stmt->execute();
Logger::info("[DEBUG] Client successfully added.");
return true;
} catch (\Exception $e) {
Logger::error("[ERROR] Failed to add client: " . $e->getMessage());
ErrorHandler::exception($e);
return false;
}
}
/**
* Delete a client by ID.
*
* @param int $id
* @return bool
*/
public function deleteClientById(int $id): bool
{
try {
Logger::info("[DEBUG] Deleting client with ID: $id");
$stmt = $this->db->prepare("DELETE FROM clients WHERE id = :id");
$stmt->bindParam(':id', $id, \PDO::PARAM_INT);
$stmt->execute();
Logger::info("[DEBUG] Client with ID $id successfully deleted.");
return true;
} catch (\Exception $e) {
Logger::error("[ERROR] Failed to delete client with ID $id: " . $e->getMessage());
ErrorHandler::exception($e);
return false;
}
}
}