🎉 initial commit
This commit is contained in:
commit
c93a06972b
27 changed files with 4189 additions and 0 deletions
177
Civi/Mailinglistsync/QueueHelper.php
Normal file
177
Civi/Mailinglistsync/QueueHelper.php
Normal file
|
@ -0,0 +1,177 @@
|
|||
<?php
|
||||
|
||||
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 = [];
|
||||
|
||||
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' => 'drop',
|
||||
]);
|
||||
$this->eventQueue = \Civi::queue(
|
||||
'propeace-mailinglist-group-queue', [
|
||||
'type' => 'SqlParallel',
|
||||
'is_autorun' => FALSE,
|
||||
'reset' => FALSE,
|
||||
'error' => 'drop',
|
||||
]);
|
||||
$this->emailQueue = \Civi::queue(
|
||||
'propeace-mailinglist-group-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];
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 instance of the given class in the queue helper singleton.
|
||||
* Meant to be passed as callback to the queue.
|
||||
*
|
||||
* @param int $entityId
|
||||
* @param string $class
|
||||
*
|
||||
* @return void
|
||||
* @throws \Civi\Mailinglistsync\Exceptions\MailinglistException
|
||||
*/
|
||||
public static function storeInstance(int $entityId, string $class): void {
|
||||
|
||||
// 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
|
||||
$instance = new $class();
|
||||
$instance->load($entityId);
|
||||
|
||||
// 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
|
||||
),
|
||||
};
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue