2022-06-19 20:05:45 +03:00
|
|
|
|
<?php defined('BASEPATH') or exit('No direct script access allowed');
|
|
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------
|
2023-12-31 21:46:27 +03:00
|
|
|
|
* IFLRandevu - İzmir Fen Lisesi Randevu Portalı
|
2022-06-19 20:05:45 +03:00
|
|
|
|
*
|
|
|
|
|
* @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
|
|
|
|
|
*/
|
|
|
|
|
public function validate(array $webhook)
|
|
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
|
if (empty($webhook['name']) || empty($webhook['url']) || empty($webhook['actions'])) {
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|