From 586497e1f30832e862c73cebe69223a12f171b75 Mon Sep 17 00:00:00 2001 From: Mohammad Farouk Date: Thu, 3 Oct 2024 01:31:51 +0300 Subject: [PATCH] add adhoc task for deferred transactions --- classes/task/queue_trasaction.php | 89 +++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 classes/task/queue_trasaction.php diff --git a/classes/task/queue_trasaction.php b/classes/task/queue_trasaction.php new file mode 100644 index 00000000..d67e6ef1 --- /dev/null +++ b/classes/task/queue_trasaction.php @@ -0,0 +1,89 @@ +. + +namespace enrol_wallet\task; + +use enrol_wallet\util\balance_op; +use enrol_wallet\util\balance; +/** + * Class queue_trasaction + * + * @package enrol_wallet + * @copyright 2024 Mohammad Farouk + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class queue_trasaction extends \core\task\adhoc_task { + /** + * Name for this task. + * + * @return string + */ + public function get_name() { + return 'queue transactions'; + } + /** + * Perform the transaction. + * @return void + */ + public function execute() { + $data = $this->get_custom_data(); + $op = new balance_op($data['userid'], $data['catid'] ?? 0); + if ($data['method'] == 'credit') { + $op->credit($data['amount'], $data['by'], $data['thingid'], $data['desc'], $data['refundable'], $data['trigger']); + } else if ($data['method'] == 'debit') { + $op->debit($data['amount'], $data['by'], $data['thingid'], $data['desc'], $data['neg']); + } + } + /** + * Queue credit + * @param \enrol_wallet\util\balance $op + * @param array $args + * @return void + */ + public static function queue_credit(balance $op, array $args) { + $args['method'] = 'credit'; + $args['userid'] = $op->get_user_id(); + $args['catid'] = $op->get_catid(); + return self::queue_transaction($args); + } + /** + * Queue debit + * @param \enrol_wallet\util\balance $op + * @param array $args + * @return void + */ + public static function queue_debit(balance $op, array $args) { + $args['method'] = 'debit'; + $args['userid'] = $op->get_user_id(); + $args['catid'] = $op->get_catid(); + return self::queue_transaction($args); + } + /** + * Queue transaction + * @param array $args + * @return void + */ + protected static function queue_transaction(array $args) { + $task = new self(); + $task->set_custom_data($args); + if (!PHPUNIT_TEST) { + $task->set_next_run_time(time() - 1); + \core\task\manager::queue_adhoc_task($task); + } else { + $task->execute(); + } + } +}