🔖 Version 1.0.0-beta
This commit is contained in:
parent
c93a06972b
commit
460a811554
26 changed files with 2480 additions and 771 deletions
|
@ -1,20 +1,13 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Civi\Mailinglistsync;
|
||||
|
||||
use CRM_Mailinglistsync_ExtensionUtil as E;
|
||||
use Civi\Mailinglistsync\Exceptions\MailinglistException;
|
||||
use Civi\Mailinglistsync\Exceptions\MailinglistSyncException;
|
||||
use CRM_Queue_Queue;
|
||||
|
||||
class QueueHelper {
|
||||
|
||||
/**
|
||||
* Singleton instances.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private static array $instances = [];
|
||||
use Singleton;
|
||||
|
||||
private CRM_Queue_Queue $groupQueue;
|
||||
private CRM_Queue_Queue $eventQueue;
|
||||
|
@ -39,52 +32,23 @@ class QueueHelper {
|
|||
'error' => 'drop',
|
||||
]);
|
||||
$this->eventQueue = \Civi::queue(
|
||||
'propeace-mailinglist-group-queue', [
|
||||
'propeace-mailinglist-event-queue', [
|
||||
'type' => 'SqlParallel',
|
||||
'is_autorun' => FALSE,
|
||||
'reset' => FALSE,
|
||||
'error' => 'drop',
|
||||
]);
|
||||
$this->emailQueue = \Civi::queue(
|
||||
'propeace-mailinglist-group-queue', [
|
||||
'propeace-mailinglist-email-queue', [
|
||||
'type' => 'SqlParallel',
|
||||
'is_autorun' => FALSE,
|
||||
'reset' => FALSE,
|
||||
'error' => 'drop',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Protect singleton from being cloned.
|
||||
*/
|
||||
protected function __clone() { }
|
||||
|
||||
/**
|
||||
* Protect unserialize method to prevent cloning of the instance.
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function __wakeup()
|
||||
{
|
||||
throw new MailinglistSyncException(
|
||||
"Cannot unserialize a singleton.",
|
||||
MailinglistSyncException::ERROR_CODE_UNSERIALIZE_SINGLETON
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the singleton instance.
|
||||
*
|
||||
* @return \Civi\Mailinglistsync\QueueHelper
|
||||
*/
|
||||
public static function getInstance(): QueueHelper
|
||||
{
|
||||
$cls = static::class;
|
||||
if (!isset(self::$instances[$cls])) {
|
||||
self::$instances[$cls] = new static();
|
||||
\Civi::log()->debug(E::LONG_NAME . ": Created new instance of $cls");
|
||||
}
|
||||
|
||||
return self::$instances[$cls];
|
||||
$this->groups = [];
|
||||
$this->events = [];
|
||||
$this->emails = [];
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -138,17 +102,31 @@ class QueueHelper {
|
|||
$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 void
|
||||
* @return bool TRUE if the instance was stored successfully.
|
||||
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
|
||||
*/
|
||||
public static function storeInstance(int $entityId, string $class): void {
|
||||
public static function storeInstance(\CRM_Queue_TaskContext $context, int $entityId, string $class): bool {
|
||||
|
||||
// Throw exception if class is invalid
|
||||
if ($class != GroupMailingList::class &&
|
||||
|
@ -161,17 +139,23 @@ class QueueHelper {
|
|||
}
|
||||
|
||||
// Instantiate the mailing list object
|
||||
$instance = new $class();
|
||||
$instance->load($entityId);
|
||||
/* @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
|
||||
match ($class) {
|
||||
GroupMailingList::class, ADGroupMailingList::class => self::getInstance()->addToGroups($instance),
|
||||
EventMailingList::class => self::getInstance()->addToEvents($instance),
|
||||
default => throw new MailinglistException(
|
||||
"Invalid class '$class'",
|
||||
MailinglistException::ERROR_CODE_INVALID_CLASS
|
||||
),
|
||||
};
|
||||
if (
|
||||
$instance::class === GroupMailingList::class
|
||||
|| $instance::class === ADGroupMailingList::class
|
||||
) {
|
||||
self::getInstance()->addToGroups($instance);
|
||||
} else {
|
||||
self::getInstance()->addToEvents($instance);
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue