aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--classes/local/api.php198
-rw-r--r--classes/local/record.php (renamed from classes/record.php)4
-rw-r--r--lib.php216
-rw-r--r--tests/api_test.php (renamed from tests/lib_test.php)55
4 files changed, 228 insertions, 245 deletions
diff --git a/classes/local/api.php b/classes/local/api.php
new file mode 100644
index 0000000..2ccec4b
--- /dev/null
+++ b/classes/local/api.php
@@ -0,0 +1,198 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
+
+namespace local_webhooks\local;
+
+defined('MOODLE_INTERNAL') || die();
+
+define('LW_TABLE_EVENTS', 'local_webhooks_events');
+define('LW_TABLE_SERVICES', 'local_webhooks_service');
+
+global $CFG;
+
+require_once($CFG->dirroot . '/local/webhooks/classes/local/record.php');
+
+use coding_exception;
+use function define;
+use function defined;
+use function is_array;
+use function is_int;
+use function is_object;
+
+/**
+ * The main class for the plugin.
+ *
+ * @copyright 2019 'Valentin Popov' <info@valentineus.link>
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @package local_webhooks\local
+ */
+final class api {
+ /**
+ * Create a new record in the database.
+ *
+ * @param \local_webhooks\local\record $record
+ *
+ * @return int
+ * @throws \coding_exception
+ * @throws \dml_exception
+ */
+ public static function create_service(record $record): int {
+ global $DB;
+
+ $id = $DB->insert_record(LW_TABLE_SERVICES, $record);
+
+ if (is_int($id) && is_array($record->events)) {
+ $DB->insert_records(LW_TABLE_EVENTS, array_map(static function (string $name) use ($id) {
+ return ['name' => $name, 'serviceid' => $id];
+ }, $record->events));
+ }
+
+ if (!is_int($id)) {
+ throw new coding_exception('Variable \'id\' must be type \'Integer\'');
+ }
+
+ return $id;
+ }
+
+ /**
+ * Delete an existing record in the database.
+ *
+ * @param int $id
+ *
+ * @return bool
+ * @throws \dml_exception
+ */
+ public static function delete_service(int $id): bool {
+ global $DB;
+
+ $DB->delete_records(LW_TABLE_EVENTS, ['serviceid' => $id]);
+
+ return $DB->delete_records(LW_TABLE_SERVICES, ['id' => $id]);
+ }
+
+ /**
+ * Get an existing record from the database.
+ *
+ * @param int $id
+ *
+ * @return \local_webhooks\local\record
+ * @throws \dml_exception
+ */
+ public static function get_service(int $id): record {
+ global $DB;
+
+ $record = $DB->get_record(LW_TABLE_SERVICES, ['id' => $id], '*', MUST_EXIST);
+ $events = $DB->get_records(LW_TABLE_EVENTS, ['serviceid' => $id]);
+
+ $service = new record();
+ $service->events = array_column($events, 'name');
+ $service->header = $record->header;
+ $service->id = $record->id;
+ $service->name = $record->name;
+ $service->point = $record->point;
+ $service->status = $record->status;
+ $service->token = $record->token;
+
+ return $service;
+ }
+
+ /**
+ * Get list records from the database.
+ *
+ * @param array|null $conditions
+ * @param int|null $limitfrom
+ * @param int|null $limitnum
+ *
+ * @return \local_webhooks\local\record[]
+ * @throws \dml_exception
+ */
+ public static function get_services(array $conditions = null, int $limitfrom = null, int $limitnum = null): array {
+ global $DB;
+
+ $records = $DB->get_records(LW_TABLE_SERVICES, $conditions ?? [], '', '*', $limitfrom ?? 0, $limitnum ?? 0);
+
+ $services = [];
+ foreach ($records as $record) {
+ if (!is_object($record)) {
+ continue;
+ }
+
+ $events = $DB->get_records(LW_TABLE_EVENTS, ['serviceid' => $record->id]);
+
+ $service = new record();
+ $service->events = array_column($events, 'name');
+ $service->header = $record->header;
+ $service->id = $record->id;
+ $service->name = $record->name;
+ $service->point = $record->point;
+ $service->status = $record->status;
+ $service->token = $record->token;
+
+ $services[] = $service;
+ }
+
+ return $services;
+ }
+
+ /**
+ * Get list records from the database by the event's name.
+ *
+ * @param string $name
+ *
+ * @return \local_webhooks\local\record[]
+ * @throws \dml_exception
+ */
+ public static function get_services_by_event(string $name): array {
+ global $DB;
+
+ $events = $DB->get_records(LW_TABLE_EVENTS, ['name' => $name]);
+
+ $services = [];
+ foreach ($events as $event) {
+ if (!is_object($event)) {
+ continue;
+ }
+
+ $services[] = self::get_service($event->serviceid);
+ }
+
+ return $services;
+ }
+
+ /**
+ * Update an existing record in the database.
+ *
+ * @param \local_webhooks\local\record $service
+ *
+ * @return bool
+ * @throws \coding_exception
+ * @throws \dml_exception
+ */
+ public static function update_service(record $service): bool {
+ global $DB;
+
+ $result = $DB->update_record(LW_TABLE_SERVICES, $service);
+ $DB->delete_records(LW_TABLE_EVENTS, ['serviceid' => $service->id]);
+
+ if ($result && is_array($service->events)) {
+ $DB->insert_records(LW_TABLE_EVENTS, array_map(static function (string $name) use ($service) {
+ return ['name' => $name, 'serviceid' => $service->id];
+ }, $service->events));
+ }
+
+ return $result;
+ }
+} \ No newline at end of file
diff --git a/classes/record.php b/classes/local/record.php
index 238fd2c..29dadce 100644
--- a/classes/record.php
+++ b/classes/local/record.php
@@ -26,9 +26,9 @@ use function defined;
*
* @copyright 2019 'Valentin Popov' <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- * @package local_webhooks\local\local_webhooks_record
+ * @package local_webhooks\local
*/
-final class local_webhooks_record extends stdClass {
+final class record extends stdClass {
/**
* List of some events.
*
diff --git a/lib.php b/lib.php
deleted file mode 100644
index aefb783..0000000
--- a/lib.php
+++ /dev/null
@@ -1,216 +0,0 @@
-<?php
-// This file is part of Moodle - http://moodle.org/
-//
-// Moodle is free software: you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-//
-// Moodle is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
-
-/**
- * This file contains the functions used by the plugin.
- *
- * @copyright 2018 'Valentin Popov' <info@valentineus.link>
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- * @package local_webhooks
- */
-
-defined('MOODLE_INTERNAL') || die();
-
-define('LW_TABLE_SERVICES', 'local_webhooks_service');
-define('LW_TABLE_EVENTS', 'local_webhooks_events');
-
-/**
- * Class local_webhooks_api
- *
- * @copyright 2018 'Valentin Popov' <info@valentineus.link>
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- * @package local_webhooks
- */
-class local_webhooks_api {
- /**
- * Get information about the service.
- *
- * @param int $serviceid
- *
- * @return object
- * @throws \dml_exception
- * @throws \moodle_exception
- */
- public static function get_service($serviceid = 0) {
- global $DB;
-
- if (!is_numeric($serviceid) || $serviceid === 0) {
- print_error('unknowparamtype', 'error', null, 'serviceid');
- }
-
- $service = $DB->get_record(LW_TABLE_SERVICES, array('id' => $serviceid), '*', MUST_EXIST);
- $events = $DB->get_records(LW_TABLE_EVENTS, array('serviceid' => $serviceid), '', '*', 0, 0);
-
- $service->events = array();
- foreach ($events as $event) {
- $service->events[] = $event->name;
- }
-
- return $service;
- }
-
- /**
- * Get a list of services.
- * By default, the entire list of services is given.
- *
- * @param array $conditions
- * @param int $limitfrom
- * @param int $limitnum
- *
- * @return array
- * @throws \dml_exception
- */
- public static function get_services(array $conditions = array(), $limitfrom = 0, $limitnum = 0) {
- global $DB;
-
- $services = $DB->get_records(LW_TABLE_SERVICES, $conditions, '', '*', $limitfrom, $limitnum);
-
- foreach ($services as $service) {
- $events = $DB->get_records(LW_TABLE_EVENTS, array('serviceid' => $service->id), '', '*', 0, 0);
-
- $service->events = array();
- foreach ($events as $event) {
- $service->events[] = $event->name;
- }
- }
-
- return $services;
- }
-
- /**
- * Get the list of services subscribed to the event.
- *
- * @param string $eventname
- *
- * @return array
- * @throws \dml_exception
- * @throws \moodle_exception
- */
- public static function get_services_by_event($eventname = '') {
- global $DB;
-
- if (!is_string($eventname) || $eventname === '') {
- print_error('unknowparamtype', 'error', null, 'eventname');
- }
-
- $events = $DB->get_records(LW_TABLE_EVENTS, array('name' => $eventname), '', '*', 0, 0);
-
- $services = array();
- foreach ($events as $event) {
- $services[] = self::get_service($event->serviceid);
- }
-
- return $services;
- }
-
- /**
- * Create service data in the database.
- *
- * @param array $service
- *
- * @return int
- * @throws \dml_exception
- * @throws \moodle_exception
- */
- public static function create_service(array $service = array()) {
- global $DB;
-
- if (!is_array($service) || count($service) === 0) {
- print_error('unknowparamtype', 'error', null, 'service');
- }
-
- $serviceid = $DB->insert_record(LW_TABLE_SERVICES, (object) $service, true, false);
- if ($serviceid && !empty($service['events']) && is_array($service['events'])) {
- self::insert_events($service['events'], $serviceid);
- }
-
- // TODO: Mark the log.
-
- return (int) $serviceid;
- }
-
- /**
- * Delete the service data from the database.
- *
- * @param int $serviceid
- *
- * @return bool
- * @throws \dml_exception
- * @throws \moodle_exception
- */
- public static function delete_service($serviceid = 0) {
- global $DB;
-
- if (!is_numeric($serviceid) || $serviceid === 0) {
- print_error('unknowparamtype', 'error', null, 'serviceid');
- }
-
- // TODO: Mark the log.
-
- $DB->delete_records(LW_TABLE_EVENTS, array('serviceid' => $serviceid));
-
- return $DB->delete_records(LW_TABLE_SERVICES, array('id' => $serviceid));
- }
-
- /**
- * Update the service data in the database.
- *
- * @param array $service
- *
- * @return bool
- * @throws \dml_exception
- * @throws \moodle_exception
- */
- public static function update_service(array $service = array()) {
- global $DB;
-
- if (!is_array($service) || count($service) === 0 || !isset($service['id'])) {
- print_error('unknowparamtype', 'error', null, 'service');
- }
-
- // TODO: Add transactions for operations.
- $result = $DB->update_record(LW_TABLE_SERVICES, (object) $service, false);
- $DB->delete_records(LW_TABLE_EVENTS, array('serviceid' => $service['id']));
-
- if ($result && is_array($service['events']) && count($service) !== 0) {
- self::insert_events($service['events'], $service['id']);
- }
-
- // TODO: Mark the log.
-
- return $result;
- }
-
- /**
- * Save the list of events to the database.
- *
- * @param array $events
- * @param int $serviceid
- *
- * @throws \coding_exception
- * @throws \dml_exception
- */
- protected static function insert_events(array $events = array(), $serviceid = 0) {
- global $DB;
-
- $conditions = array();
- foreach ($events as $eventname) {
- $conditions[] = array('name' => $eventname, 'serviceid' => $serviceid);
- }
-
- $DB->insert_records(LW_TABLE_EVENTS, $conditions);
- }
-} \ No newline at end of file
diff --git a/tests/lib_test.php b/tests/api_test.php
index b813658..998db9e 100644
--- a/tests/lib_test.php
+++ b/tests/api_test.php
@@ -18,17 +18,18 @@ defined('MOODLE_INTERNAL') || die();
global $CFG;
-require_once($CFG->dirroot . '/local/webhooks/lib.php');
+require_once($CFG->dirroot . '/local/webhooks/classes/local/api.php');
-use local_webhooks\local\local_webhooks_record as stdRecord;
+use local_webhooks\local\api;
+use local_webhooks\local\record;
/**
- * Class local_webhooks_lib_testcase
+ * Class local_webhooks_api_testcase.
*
* @copyright 2019 'Valentin Popov' <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
-final class local_webhooks_lib_testcase extends advanced_testcase {
+final class local_webhooks_api_testcase extends advanced_testcase {
/**
* Testing creation of the service.
*
@@ -42,7 +43,7 @@ final class local_webhooks_lib_testcase extends advanced_testcase {
$this->resetAfterTest();
- $record = new stdRecord();
+ $record = new record();
$record->header = 'application/json';
$record->name = 'Example name';
$record->point = 'http://example.org/';
@@ -55,7 +56,7 @@ final class local_webhooks_lib_testcase extends advanced_testcase {
'\core\event\course_viewed',
];
- $record->id = local_webhooks_api::create_service($record);
+ $record->id = api::create_service($record);
$events = $DB->get_records(LW_TABLE_EVENTS);
$services = $DB->get_records(LW_TABLE_SERVICES);
@@ -90,7 +91,7 @@ final class local_webhooks_lib_testcase extends advanced_testcase {
$this->resetAfterTest();
- $record = new stdRecord();
+ $record = new record();
$record->header = 'application/json';
$record->name = 'Example name';
$record->point = 'http://example.org/';
@@ -103,8 +104,8 @@ final class local_webhooks_lib_testcase extends advanced_testcase {
'\core\event\course_viewed',
];
- $record->id = local_webhooks_api::create_service($record);
- self::assertTrue(local_webhooks_api::delete_service($record->id));
+ $record->id = api::create_service($record);
+ self::assertTrue(api::delete_service($record->id));
self::assertCount(0, $DB->get_records(LW_TABLE_EVENTS));
self::assertCount(0, $DB->get_records(LW_TABLE_SERVICES));
}
@@ -118,7 +119,7 @@ final class local_webhooks_lib_testcase extends advanced_testcase {
public function test_get_service() {
$this->resetAfterTest();
- $record = new stdRecord();
+ $record = new record();
$record->header = 'application/json';
$record->name = 'Example name';
$record->point = 'http://example.org/';
@@ -131,8 +132,8 @@ final class local_webhooks_lib_testcase extends advanced_testcase {
'\core\event\course_viewed',
];
- $record->id = local_webhooks_api::create_service($record);
- $service = local_webhooks_api::get_service($record->id);
+ $record->id = api::create_service($record);
+ $service = api::get_service($record->id);
self::assertEquals($record->header, $service->header);
self::assertEquals($record->id, $service->id);
@@ -157,7 +158,7 @@ final class local_webhooks_lib_testcase extends advanced_testcase {
public function test_get_services() {
$this->resetAfterTest();
- $record = new stdRecord();
+ $record = new record();
$record->header = 'application/json';
$record->name = 'Example name';
$record->point = 'http://example.org/';
@@ -173,10 +174,10 @@ final class local_webhooks_lib_testcase extends advanced_testcase {
$ids = [];
$total = random_int(5, 20);
for ($i = 0; $i < $total; $i++) {
- $ids[] = local_webhooks_api::create_service($record);
+ $ids[] = api::create_service($record);
}
- $services = local_webhooks_api::get_services();
+ $services = api::get_services();
self::assertCount(count($ids), $services);
foreach ($services as $service) {
@@ -204,7 +205,7 @@ final class local_webhooks_lib_testcase extends advanced_testcase {
public function test_get_services_by_event() {
$this->resetAfterTest();
- $record = new stdRecord();
+ $record = new record();
$record->header = 'application/json';
$record->name = 'Example name';
$record->point = 'http://example.org/';
@@ -220,11 +221,11 @@ final class local_webhooks_lib_testcase extends advanced_testcase {
$ids = [];
$total = random_int(5, 20);
for ($i = 0; $i < $total; $i++) {
- $ids[] = local_webhooks_api::create_service($record);
+ $ids[] = api::create_service($record);
}
$eventname = $record->events[random_int(1, count($record->events) - 1)];
- $services = local_webhooks_api::get_services_by_event($eventname);
+ $services = api::get_services_by_event($eventname);
self::assertCount(count($ids), $services);
foreach ($services as $service) {
@@ -252,7 +253,7 @@ final class local_webhooks_lib_testcase extends advanced_testcase {
public function test_get_services_with_conditions() {
$this->resetAfterTest();
- $record = new stdRecord();
+ $record = new record();
$record->header = 'application/json';
$record->status = true;
$record->token = '967b2286-0874-4938-b088-efdbcf8a79bc';
@@ -267,15 +268,15 @@ final class local_webhooks_lib_testcase extends advanced_testcase {
for ($i = 0; $i < $total; $i++) {
$record->name = 'Example name #' . $i;
$record->point = 'http://example.org/test_' . $i;
- local_webhooks_api::create_service($record);
+ api::create_service($record);
}
- self::assertCount(1, local_webhooks_api::get_services([
+ self::assertCount(1, api::get_services([
'name' => 'Example name #' . random_int(1, $total),
]));
- $limit = random_int(1, $total);
- self::assertCount($limit, local_webhooks_api::get_services([], 1, $limit));
+ $limit = intdiv($total, 2);
+ self::assertCount($limit, api::get_services([], 1, $limit));
}
/**
@@ -291,7 +292,7 @@ final class local_webhooks_lib_testcase extends advanced_testcase {
$this->resetAfterTest();
- $record1 = new stdRecord();
+ $record1 = new record();
$record1->header = 'application/json';
$record1->name = 'Example name';
$record1->point = 'http://example.org/';
@@ -304,7 +305,7 @@ final class local_webhooks_lib_testcase extends advanced_testcase {
'\core\event\course_viewed',
];
- $record2 = new stdRecord();
+ $record2 = new record();
$record2->header = 'application/x-www-form-urlencoded';
$record2->name = 'New name';
$record2->point = 'http://domain.local/example';
@@ -316,8 +317,8 @@ final class local_webhooks_lib_testcase extends advanced_testcase {
'\core\event\calendar_event_updated',
];
- $record2->id = local_webhooks_api::create_service($record1);
- self::assertTrue(local_webhooks_api::update_service($record2));
+ $record2->id = api::create_service($record1);
+ self::assertTrue(api::update_service($record2));
$events = $DB->get_records(LW_TABLE_EVENTS);
$services = $DB->get_records(LW_TABLE_SERVICES);