Auto stash before merge of "main" and "origin/main"
This commit is contained in:
80
src/Bill/Bill.php
Normal file
80
src/Bill/Bill.php
Normal file
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
namespace Hpz937\BillReminder\Bill;
|
||||
|
||||
use Hpz937\BillReminder\Database\DatabaseInterface;
|
||||
use Exception;
|
||||
|
||||
class Bill
|
||||
{
|
||||
private $db;
|
||||
|
||||
public function __construct(DatabaseInterface $db)
|
||||
{
|
||||
$this->db = $db;
|
||||
}
|
||||
|
||||
public function addBill($userId, $dueDate, $amount, $description)
|
||||
{
|
||||
$sql = "INSERT INTO bills (user_id, due_date, amount, description, is_paid) VALUES (:user_id, :due_date, :amount, :description, 0)";
|
||||
$params = [
|
||||
':user_id' => $userId,
|
||||
':due_date' => $dueDate,
|
||||
':amount' => $amount,
|
||||
':description' => $description
|
||||
];
|
||||
|
||||
try {
|
||||
$this->db->query($sql, $params);
|
||||
return true;
|
||||
} catch (Exception $e) {
|
||||
// Handle or log the error appropriately
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function editBill($billId, $dueDate, $amount, $description)
|
||||
{
|
||||
$sql = "UPDATE bills SET due_date = :due_date, amount = :amount, description = :description WHERE id = :id";
|
||||
$params = [
|
||||
':due_date' => $dueDate,
|
||||
':amount' => $amount,
|
||||
':description' => $description,
|
||||
':id' => $billId
|
||||
];
|
||||
|
||||
try {
|
||||
$this->db->query($sql, $params);
|
||||
return true;
|
||||
} catch (Exception $e) {
|
||||
// Handle or log the error appropriately
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function markAsPaid($billId)
|
||||
{
|
||||
$sql = "UPDATE bills SET is_paid = 1 WHERE id = :id";
|
||||
$params = [':id' => $billId];
|
||||
|
||||
try {
|
||||
$this->db->query($sql, $params);
|
||||
return true;
|
||||
} catch (Exception $e) {
|
||||
// Handle or log the error appropriately
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function getBillsByUserId($userId) {
|
||||
$sql = "SELECT * FROM bills WHERE user_id = :user_id ORDER BY due_date ASC";
|
||||
$params = [':user_id' => $userId];
|
||||
|
||||
try {
|
||||
return $this->db->query($sql, $params);
|
||||
} catch (Exception $e) {
|
||||
// Handle or log the error appropriately
|
||||
return [];
|
||||
}
|
||||
}
|
||||
}
|
||||
10
src/Database/DatabaseInterface.php
Normal file
10
src/Database/DatabaseInterface.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Hpz937\BillReminder\Database;
|
||||
|
||||
interface DatabaseInterface
|
||||
{
|
||||
public function connect();
|
||||
public function query(string $query, array $params = []);
|
||||
public function close();
|
||||
}
|
||||
47
src/Database/SQLiteAdapter.php
Normal file
47
src/Database/SQLiteAdapter.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace Hpz937\BillReminder\Database;
|
||||
|
||||
use PDO;
|
||||
use PDOException;
|
||||
|
||||
class SQLiteAdapter implements DatabaseInterface
|
||||
{
|
||||
private $connection;
|
||||
|
||||
public function connect()
|
||||
{
|
||||
if ($this->connection === null) {
|
||||
try {
|
||||
$this->connection = new PDO('sqlite:' . __DIR__ . '/../../database.db');
|
||||
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
} catch (PDOException $e) {
|
||||
// In a real application, you might want to use a more sophisticated error handling approach
|
||||
die("Connection error: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
return $this->connection;
|
||||
}
|
||||
|
||||
public function query(string $query, array $params = [])
|
||||
{
|
||||
$stmt = $this->connect()->prepare($query);
|
||||
|
||||
if (!$stmt->execute($params)) {
|
||||
// Again, consider a more sophisticated error handling in a real application
|
||||
die("Query error: " . implode(", ", $stmt->errorInfo()));
|
||||
}
|
||||
|
||||
if (preg_match('/^(SELECT|SHOW|PRAGMA)/i', $query)) {
|
||||
return $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function close()
|
||||
{
|
||||
$this->connection = null;
|
||||
}
|
||||
}
|
||||
50
src/Notification/NftyNotification.php
Normal file
50
src/Notification/NftyNotification.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace Hpz937\BillReminder\Notification;
|
||||
|
||||
use Hpz937\Restclient\RestClient;
|
||||
use Exception;
|
||||
|
||||
class NftyNotification implements NotificationInterface
|
||||
{
|
||||
private $restClient;
|
||||
private $apiKey;
|
||||
|
||||
public function __construct(RestClient $restClient, string $apiKey)
|
||||
{
|
||||
$this->restClient = $restClient;
|
||||
$this->apiKey = $apiKey;
|
||||
|
||||
// Set the base URL for the nfty.sh API if it's consistent for all requests
|
||||
$this->restClient->setBaseUrl('https://api.nfty.sh');
|
||||
|
||||
// Set the headers required for authentication and content type, if not already set in RestClient
|
||||
$this->restClient->setHeaders([
|
||||
'Authorization: Bearer ' . $this->apiKey,
|
||||
'Content-Type: application/json' // Assuming JSON content type, adjust if necessary
|
||||
]);
|
||||
}
|
||||
|
||||
public function send(string $to, string $message): bool
|
||||
{
|
||||
$endpoint = '/send'; // Adjust the endpoint if necessary
|
||||
$data = [
|
||||
'to' => $to,
|
||||
'message' => $message,
|
||||
];
|
||||
|
||||
try {
|
||||
$this->restClient->post($endpoint, $data);
|
||||
$response = $this->restClient->getResponse();
|
||||
|
||||
// Assuming the API returns a JSON response indicating success, adjust as necessary
|
||||
$decodedResponse = $this->restClient->decodeJson();
|
||||
|
||||
// Check the decoded response for success indication, adjust the condition based on actual API response structure
|
||||
return isset($decodedResponse['success']) && $decodedResponse['success'];
|
||||
} catch (Exception $e) {
|
||||
// Log or handle the error as needed
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
8
src/Notification/NotificationInterface.php
Normal file
8
src/Notification/NotificationInterface.php
Normal file
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace Hpz937\BillReminder\Notification;
|
||||
|
||||
interface NotificationInterface
|
||||
{
|
||||
public function send(string $to, string $message): bool;
|
||||
}
|
||||
69
src/Settings/Settings.php
Normal file
69
src/Settings/Settings.php
Normal file
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace Hpz937\BillReminder\Settings;
|
||||
|
||||
use Hpz937\BillReminder\Database\DatabaseInterface;
|
||||
use Exception;
|
||||
|
||||
class Settings
|
||||
{
|
||||
private $db;
|
||||
|
||||
public function __construct(DatabaseInterface $db)
|
||||
{
|
||||
$this->db = $db;
|
||||
}
|
||||
|
||||
public function saveSettings($userId, $notificationPeriod)
|
||||
{
|
||||
// Check if settings already exist for the user
|
||||
if ($this->settingsExist($userId)) {
|
||||
// Update existing settings
|
||||
$sql = "UPDATE settings SET notification_period = :notification_period WHERE user_id = :user_id";
|
||||
} else {
|
||||
// Insert new settings
|
||||
$sql = "INSERT INTO settings (user_id, notification_period) VALUES (:user_id, :notification_period)";
|
||||
}
|
||||
|
||||
$params = [
|
||||
':user_id' => $userId,
|
||||
':notification_period' => $notificationPeriod,
|
||||
];
|
||||
|
||||
try {
|
||||
$this->db->query($sql, $params);
|
||||
return true;
|
||||
} catch (Exception $e) {
|
||||
// Handle or log the error appropriately
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function getSettings($userId)
|
||||
{
|
||||
$sql = "SELECT * FROM settings WHERE user_id = :user_id";
|
||||
$params = [':user_id' => $userId];
|
||||
|
||||
try {
|
||||
$settings = $this->db->query($sql, $params);
|
||||
return $settings ? $settings[0] : null;
|
||||
} catch (Exception $e) {
|
||||
// Handle or log the error appropriately
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private function settingsExist($userId)
|
||||
{
|
||||
$sql = "SELECT id FROM settings WHERE user_id = :user_id";
|
||||
$params = [':user_id' => $userId];
|
||||
|
||||
try {
|
||||
$result = $this->db->query($sql, $params);
|
||||
return !empty($result);
|
||||
} catch (Exception $e) {
|
||||
// Handle or log the error appropriately
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
76
src/User/User.php
Normal file
76
src/User/User.php
Normal file
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
namespace Hpz937\BillReminder\User;
|
||||
|
||||
use Hpz937\BillReminder\Database\DatabaseInterface;
|
||||
use Exception;
|
||||
|
||||
class User
|
||||
{
|
||||
private $db;
|
||||
|
||||
public function __construct(DatabaseInterface $db)
|
||||
{
|
||||
$this->db = $db;
|
||||
}
|
||||
|
||||
public function register($username, $password, $email)
|
||||
{
|
||||
// Check if the username or email already exists
|
||||
if ($this->userExists($username, $email)) {
|
||||
throw new Exception("Username or email already exists.");
|
||||
}
|
||||
|
||||
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
|
||||
$sql = "INSERT INTO users (username, password, email) VALUES (:username, :password, :email)";
|
||||
$params = [
|
||||
':username' => $username,
|
||||
':password' => $hashedPassword,
|
||||
':email' => $email
|
||||
];
|
||||
|
||||
try {
|
||||
$this->db->query($sql, $params);
|
||||
return true;
|
||||
} catch (Exception $e) {
|
||||
// Handle or log the error appropriately
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function login($username, $password)
|
||||
{
|
||||
$sql = "SELECT * FROM users WHERE username = :username";
|
||||
$params = [':username' => $username];
|
||||
|
||||
try {
|
||||
$user = $this->db->query($sql, $params);
|
||||
if ($user && password_verify($password, $user[0]['password'])) {
|
||||
// Set session or token here as per your session management strategy
|
||||
$_SESSION['user_id'] = $user[0]['id'];
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
} catch (Exception $e) {
|
||||
// Handle or log the error appropriately
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private function userExists($username, $email)
|
||||
{
|
||||
$sql = "SELECT id FROM users WHERE username = :username OR email = :email";
|
||||
$params = [
|
||||
':username' => $username,
|
||||
':email' => $email
|
||||
];
|
||||
|
||||
try {
|
||||
$result = $this->db->query($sql, $params);
|
||||
return !empty($result);
|
||||
} catch (Exception $e) {
|
||||
// Handle or log the error appropriately
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user