2022-06-19 20:05:45 +03:00
|
|
|
<?php defined('BASEPATH') or exit('No direct script access allowed');
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
* Easy!Appointments - Online Appointment Scheduler
|
|
|
|
*
|
|
|
|
* @package EasyAppointments
|
|
|
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
|
|
|
* @copyright Copyright (c) Alex Tselegidis
|
|
|
|
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
|
|
|
|
* @link https://easyappointments.org
|
|
|
|
* @since v1.3.2
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Webhooks model.
|
|
|
|
*
|
|
|
|
* Handles all the database operations of the webhook resource.
|
|
|
|
*
|
|
|
|
* @package Models
|
|
|
|
*/
|
2023-11-29 12:24:09 +03:00
|
|
|
class Webhooks_model extends EA_Model
|
|
|
|
{
|
2022-06-19 20:05:45 +03:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2023-03-13 11:06:18 +03:00
|
|
|
protected array $casts = [
|
2022-06-19 20:05:45 +03:00
|
|
|
'id' => 'integer',
|
|
|
|
'is_active' => 'boolean',
|
2023-12-22 13:35:41 +03:00
|
|
|
'is_ssl_verified' => 'boolean',
|
2022-06-19 20:05:45 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2023-03-13 11:06:18 +03:00
|
|
|
protected array $api_resource = [
|
2022-06-19 20:05:45 +03:00
|
|
|
'id' => 'id',
|
|
|
|
'name' => 'name',
|
|
|
|
'url' => 'url',
|
|
|
|
'action' => 'action',
|
|
|
|
'secretToken' => 'secret_token',
|
|
|
|
'isActive' => 'is_active',
|
|
|
|
'isSslVerified' => 'is_ssl_verified',
|
2023-12-22 13:35:41 +03:00
|
|
|
'notes' => 'notes',
|
2022-06-19 20:05:45 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save (insert or update) a webhook.
|
|
|
|
*
|
|
|
|
* @param array $webhook Associative array with the webhook data.
|
|
|
|
*
|
|
|
|
* @return int Returns the webhook ID.
|
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function save(array $webhook): int
|
|
|
|
{
|
|
|
|
$this->validate($webhook);
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (empty($webhook['id'])) {
|
2022-06-19 20:05:45 +03:00
|
|
|
return $this->insert($webhook);
|
2023-11-29 12:24:09 +03:00
|
|
|
} else {
|
2022-06-19 20:05:45 +03:00
|
|
|
return $this->update($webhook);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate the webhook data.
|
|
|
|
*
|
|
|
|
* @param array $webhook Associative array with the webhook data.
|
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
2024-04-26 17:36:14 +03:00
|
|
|
public function validate(array $webhook): void
|
2022-06-19 20:05:45 +03:00
|
|
|
{
|
2024-05-11 17:12:14 +03:00
|
|
|
if (empty($webhook['name']) || empty($webhook['url'])) {
|
2023-11-29 12:24:09 +03:00
|
|
|
throw new InvalidArgumentException('Not all required fields are provided: ' . print_r($webhook, true));
|
2022-06-19 20:05:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert a new webhook into the database.
|
|
|
|
*
|
|
|
|
* @param array $webhook Associative array with the webhook data.
|
|
|
|
*
|
|
|
|
* @return int Returns the webhook ID.
|
|
|
|
*
|
|
|
|
* @throws RuntimeException
|
|
|
|
*/
|
|
|
|
protected function insert(array $webhook): int
|
|
|
|
{
|
|
|
|
$webhook['create_datetime'] = date('Y-m-d H:i:s');
|
|
|
|
$webhook['update_datetime'] = date('Y-m-d H:i:s');
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!$this->db->insert('webhooks', $webhook)) {
|
2022-06-19 20:05:45 +03:00
|
|
|
throw new RuntimeException('Could not insert webhook.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->db->insert_id();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update an existing webhook.
|
|
|
|
*
|
|
|
|
* @param array $webhook Associative array with the webhook data.
|
|
|
|
*
|
|
|
|
* @return int Returns the webhook ID.
|
|
|
|
*
|
|
|
|
* @throws RuntimeException
|
|
|
|
*/
|
|
|
|
protected function update(array $webhook): int
|
|
|
|
{
|
|
|
|
$webhook['update_datetime'] = date('Y-m-d H:i:s');
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!$this->db->update('webhooks', $webhook, ['id' => $webhook['id']])) {
|
2022-06-19 20:05:45 +03:00
|
|
|
throw new RuntimeException('Could not update webhook.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $webhook['id'];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove an existing webhook from the database.
|
|
|
|
*
|
|
|
|
* @param int $webhook_id Webhook ID.
|
|
|
|
*
|
|
|
|
* @throws RuntimeException
|
|
|
|
*/
|
2023-10-19 17:25:45 +03:00
|
|
|
public function delete(int $webhook_id): void
|
2022-06-19 20:05:45 +03:00
|
|
|
{
|
2023-10-19 17:25:45 +03:00
|
|
|
$this->db->delete('webhooks', ['id' => $webhook_id]);
|
2022-06-19 20:05:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a specific webhook from the database.
|
|
|
|
*
|
|
|
|
* @param int $webhook_id The ID of the record to be returned.
|
|
|
|
*
|
|
|
|
* @return array Returns an array with the webhook data.
|
|
|
|
*/
|
2023-10-19 17:25:45 +03:00
|
|
|
public function find(int $webhook_id): array
|
2022-06-19 20:05:45 +03:00
|
|
|
{
|
|
|
|
$webhook = $this->db->get_where('webhooks', ['id' => $webhook_id])->row_array();
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!$webhook) {
|
2022-06-19 20:05:45 +03:00
|
|
|
throw new InvalidArgumentException('The provided webhook ID was not found in the database: ' . $webhook_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->cast($webhook);
|
|
|
|
|
|
|
|
return $webhook;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a specific field value from the database.
|
|
|
|
*
|
|
|
|
* @param int $webhook_id Webhook ID.
|
|
|
|
* @param string $field Name of the value to be returned.
|
|
|
|
*
|
2023-01-14 11:39:14 +03:00
|
|
|
* @return mixed Returns the selected webhook value from the database.
|
2022-06-19 20:05:45 +03:00
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
2023-01-14 11:39:14 +03:00
|
|
|
public function value(int $webhook_id, string $field): mixed
|
2022-06-19 20:05:45 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
if (empty($field)) {
|
2022-06-19 20:05:45 +03:00
|
|
|
throw new InvalidArgumentException('The field argument is cannot be empty.');
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (empty($webhook_id)) {
|
2022-06-19 20:05:45 +03:00
|
|
|
throw new InvalidArgumentException('The webhook ID argument cannot be empty.');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether the webhook exists.
|
|
|
|
$query = $this->db->get_where('webhooks', ['id' => $webhook_id]);
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!$query->num_rows()) {
|
2022-06-19 20:05:45 +03:00
|
|
|
throw new InvalidArgumentException('The provided webhook ID was not found in the database: ' . $webhook_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the required field is part of the webhook data.
|
|
|
|
$webhook = $query->row_array();
|
|
|
|
|
|
|
|
$this->cast($webhook);
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!array_key_exists($field, $webhook)) {
|
2022-06-19 20:05:45 +03:00
|
|
|
throw new InvalidArgumentException('The requested field was not found in the webhook data: ' . $field);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $webhook[$field];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the query builder interface, configured for use with the webhooks table.
|
|
|
|
*
|
|
|
|
* @return CI_DB_query_builder
|
|
|
|
*/
|
|
|
|
public function query(): CI_DB_query_builder
|
|
|
|
{
|
|
|
|
return $this->db->from('webhooks');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search webhooks by the provided keyword.
|
|
|
|
*
|
|
|
|
* @param string $keyword Search keyword.
|
|
|
|
* @param int|null $limit Record limit.
|
|
|
|
* @param int|null $offset Record offset.
|
|
|
|
* @param string|null $order_by Order by.
|
|
|
|
*
|
|
|
|
* @return array Returns an array of webhooks.
|
|
|
|
*/
|
2023-11-29 12:24:09 +03:00
|
|
|
public function search(string $keyword, int $limit = null, int $offset = null, string $order_by = null): array
|
2022-06-19 20:05:45 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
$webhooks = $this->db
|
2022-06-19 20:05:45 +03:00
|
|
|
->select()
|
|
|
|
->from('webhooks')
|
|
|
|
->group_start()
|
|
|
|
->like('name', $keyword)
|
|
|
|
->or_like('url', $keyword)
|
|
|
|
->or_like('actions', $keyword)
|
|
|
|
->group_end()
|
|
|
|
->limit($limit)
|
|
|
|
->offset($offset)
|
|
|
|
->order_by($order_by)
|
|
|
|
->get()
|
|
|
|
->result_array();
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
foreach ($webhooks as &$webhook) {
|
|
|
|
$this->cast($webhook);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $webhooks;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all webhooks that match the provided criteria.
|
|
|
|
*
|
|
|
|
* @param array|string|null $where Where conditions.
|
|
|
|
* @param int|null $limit Record limit.
|
|
|
|
* @param int|null $offset Record offset.
|
|
|
|
* @param string|null $order_by Order by.
|
|
|
|
*
|
|
|
|
* @return array Returns an array of webhooks.
|
|
|
|
*/
|
|
|
|
public function get(
|
|
|
|
array|string $where = null,
|
|
|
|
int $limit = null,
|
|
|
|
int $offset = null,
|
2023-12-22 13:35:41 +03:00
|
|
|
string $order_by = null,
|
2023-11-29 12:24:09 +03:00
|
|
|
): array {
|
|
|
|
if ($where !== null) {
|
|
|
|
$this->db->where($where);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($order_by !== null) {
|
|
|
|
$this->db->order_by($order_by);
|
|
|
|
}
|
|
|
|
|
|
|
|
$webhooks = $this->db->get('webhooks', $limit, $offset)->result_array();
|
|
|
|
|
|
|
|
foreach ($webhooks as &$webhook) {
|
2022-06-19 20:05:45 +03:00
|
|
|
$this->cast($webhook);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $webhooks;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load related resources to a webhook.
|
|
|
|
*
|
|
|
|
* @param array $webhook Associative array with the webhook data.
|
|
|
|
* @param array $resources Resource names to be attached.
|
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function load(array &$webhook, array $resources)
|
|
|
|
{
|
2023-10-19 17:25:45 +03:00
|
|
|
// Webhooks do not currently have any related resources.
|
2022-06-19 20:05:45 +03:00
|
|
|
}
|
2024-01-05 19:06:49 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert the database webhook record to the equivalent API resource.
|
|
|
|
*
|
|
|
|
* @param array $webhook Webhook data.
|
|
|
|
*/
|
|
|
|
public function api_encode(array &$webhook): void
|
|
|
|
{
|
|
|
|
$encoded_resource = [
|
|
|
|
'id' => array_key_exists('id', $webhook) ? (int) $webhook['id'] : null,
|
|
|
|
'name' => $webhook['name'],
|
|
|
|
'url' => $webhook['url'],
|
|
|
|
'actions' => $webhook['actions'],
|
|
|
|
'secret_token' => $webhook['secret_token'],
|
|
|
|
'is_ssl_verified' => $webhook['is_ssl_verified'],
|
|
|
|
'notes' => $webhook['notes'],
|
|
|
|
];
|
|
|
|
|
|
|
|
$webhook = $encoded_resource;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert the API resource to the equivalent database webhook record.
|
|
|
|
*
|
|
|
|
* @param array $webhook API resource.
|
|
|
|
* @param array|null $base Base webhook data to be overwritten with the provided values (useful for updates).
|
|
|
|
*/
|
2024-04-26 17:36:14 +03:00
|
|
|
public function api_decode(array &$webhook, array $base = null): void
|
2024-01-05 19:06:49 +03:00
|
|
|
{
|
|
|
|
$decoded_resource = $base ?: [];
|
|
|
|
|
|
|
|
if (array_key_exists('id', $webhook)) {
|
|
|
|
$decoded_resource['id'] = $webhook['id'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists('name', $webhook)) {
|
|
|
|
$decoded_resource['name'] = $webhook['name'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists('url', $webhook)) {
|
|
|
|
$decoded_resource['url'] = $webhook['url'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists('actions', $webhook)) {
|
|
|
|
$decoded_resource['actions'] = $webhook['actions'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists('secretToken', $webhook)) {
|
|
|
|
$decoded_resource['secret_token'] = $webhook['secretToken'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists('isSslVerified', $webhook)) {
|
|
|
|
$decoded_resource['is_ssl_verified'] = $webhook['isSslVerified'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists('notes', $webhook)) {
|
|
|
|
$decoded_resource['notes'] = $webhook['notes'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$webhook = $decoded_resource;
|
|
|
|
}
|
2022-06-19 20:05:45 +03:00
|
|
|
}
|