161 lines
3.8 KiB
PHP
161 lines
3.8 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Civi\Mailinglistsync;
|
|
|
|
use Civi\Mailinglistsync\Exceptions\MailinglistException;
|
|
use CRM_Queue_Queue;
|
|
|
|
class QueueHelper {
|
|
use Singleton;
|
|
|
|
private CRM_Queue_Queue $groupQueue;
|
|
private CRM_Queue_Queue $eventQueue;
|
|
private CRM_Queue_Queue $emailQueue;
|
|
|
|
private array $groups;
|
|
|
|
private array $events;
|
|
|
|
private array $emails;
|
|
|
|
/**
|
|
* Protect singleton from being instantiated.
|
|
*/
|
|
protected function __construct() {
|
|
// Get queues
|
|
$this->groupQueue = \Civi::queue(
|
|
'propeace-mailinglist-group-queue', [
|
|
'type' => 'SqlParallel',
|
|
'is_autorun' => FALSE,
|
|
'reset' => FALSE,
|
|
'error' => 'abort',
|
|
]);
|
|
$this->eventQueue = \Civi::queue(
|
|
'propeace-mailinglist-event-queue', [
|
|
'type' => 'SqlParallel',
|
|
'is_autorun' => FALSE,
|
|
'reset' => FALSE,
|
|
'error' => 'abort',
|
|
]);
|
|
$this->emailQueue = \Civi::queue(
|
|
'propeace-mailinglist-email-queue', [
|
|
'type' => 'SqlParallel',
|
|
'is_autorun' => FALSE,
|
|
'reset' => FALSE,
|
|
'error' => 'abort',
|
|
]);
|
|
|
|
$this->groups = [];
|
|
$this->events = [];
|
|
$this->emails = [];
|
|
}
|
|
|
|
/**
|
|
* Getter for the group queue.
|
|
*
|
|
* @return \CRM_Queue_Queue
|
|
*/
|
|
public function getGroupQueue(): CRM_Queue_Queue {
|
|
return $this->groupQueue;
|
|
}
|
|
|
|
/**
|
|
* Getter for the event queue.
|
|
*
|
|
* @return \CRM_Queue_Queue
|
|
*/
|
|
public function getEventQueue(): CRM_Queue_Queue {
|
|
return $this->eventQueue;
|
|
}
|
|
|
|
/**
|
|
* Getter for the email queue.
|
|
*
|
|
* @return \CRM_Queue_Queue
|
|
*/
|
|
public function getEmailQueue(): CRM_Queue_Queue {
|
|
return $this->emailQueue;
|
|
}
|
|
|
|
public function getGroups(): array {
|
|
return $this->groups;
|
|
}
|
|
|
|
public function addToGroups(GroupMailingList $group): void {
|
|
$this->groups[] = $group;
|
|
}
|
|
|
|
public function getEvents(): array {
|
|
return $this->events;
|
|
}
|
|
|
|
public function addToEvents(EventMailingList $event): void {
|
|
$this->events[] = $event;
|
|
}
|
|
|
|
public function getEmails(): array {
|
|
return $this->emails;
|
|
}
|
|
|
|
public function addToEmails(string $email): void {
|
|
$this->emails[] = $email;
|
|
}
|
|
|
|
/**
|
|
* Stores an email address in the queue helper singleton.
|
|
*
|
|
* @param \CRM_Queue_TaskContext $context
|
|
* @param string $email
|
|
*
|
|
* @return bool
|
|
*/
|
|
public static function storeEmail(\CRM_Queue_TaskContext $context, string $email): bool {
|
|
self::getInstance()->addToEmails($email);
|
|
return TRUE;
|
|
}
|
|
|
|
/**
|
|
* Stores an instance of the given class in the queue helper singleton.
|
|
* Meant to be passed as callback to the queue.
|
|
*
|
|
* @param \CRM_Queue_TaskContext $context
|
|
* @param int $entityId
|
|
* @param string $class
|
|
*
|
|
* @return bool TRUE if the instance was stored successfully.
|
|
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
|
|
*/
|
|
public static function storeInstance(\CRM_Queue_TaskContext $context, int $entityId, string $class): bool {
|
|
|
|
// Throw exception if class is invalid
|
|
if ($class != GroupMailingList::class &&
|
|
$class != ADGroupMailingList::class &&
|
|
$class != EventMailingList::class) {
|
|
throw new MailinglistException(
|
|
"Invalid class '$class'",
|
|
MailinglistException::ERROR_CODE_INVALID_CLASS
|
|
);
|
|
}
|
|
|
|
// Instantiate the mailing list object
|
|
/* @var $instance GroupMailingList|ADGroupMailingList|EventMailingList */
|
|
$instance = new $class($entityId);
|
|
|
|
// Ignore disabled mailing lists
|
|
if (!$instance->isEnabled()) {
|
|
return TRUE;
|
|
}
|
|
|
|
// Store instance in the queue helper singleton
|
|
if (
|
|
$instance::class === GroupMailingList::class
|
|
|| $instance::class === ADGroupMailingList::class
|
|
) {
|
|
self::getInstance()->addToGroups($instance);
|
|
} else {
|
|
self::getInstance()->addToEvents($instance);
|
|
}
|
|
return TRUE;
|
|
}
|
|
}
|