callback = $callback; $this->arguments = $arguments; $this->title = $title; } /** * Perform the task. * * @param \CRM_Queue_TaskContext $taskCtx * @throws Exception * @return bool * TRUE if task completes successfully. * FALSE or exception if task fails. */ public function run($taskCtx) { $args = $this->arguments; array_unshift($args, $taskCtx); if ($this->runAs !== NULL) { $equals = function($a, $b) { return $a === $b || (is_numeric($a) && is_numeric($b) && $a == $b); }; if (array_key_exists('contactId', $this->runAs) && !$equals(CRM_Core_Session::getLoggedInContactID(), $this->runAs['contactId'])) { throw new Exception(sprintf('Cannot execute queue task. Unexpected contact "%s" for job "%s"', CRM_Core_Session::getLoggedInContactID(), $this->getSummary())); } if (array_key_exists('domainId', $this->runAs) && !$equals(CRM_Core_BAO_Domain::getDomain()->id, $this->runAs['domainId'])) { throw new Exception(sprintf('Cannot execute queue task. Unexpected domain "%s" for job "%s"', CRM_Core_BAO_Domain::getDomain()->id, $this->getSummary())); } } if (is_callable($this->callback)) { $result = call_user_func_array($this->callback, $args); return $result; } else { throw new Exception('Failed to call callback: ' . $this->getSummary()); } } private function getSummary(): string { return json_encode(['title' => $this->title, 'runAs' => $this->runAs, 'callback' => $this->callback], JSON_UNESCAPED_SLASHES); } }