appwrite/src/Appwrite/Event/Messaging.php

108 lines
2.4 KiB
PHP
Raw Normal View History

<?php
namespace Appwrite\Event;
2023-10-13 12:56:54 +00:00
use Resque;
2023-10-09 11:59:26 +00:00
use ResqueScheduler;
2023-10-13 12:56:54 +00:00
use Utopia\Database\Document;
class Messaging extends Event
{
protected ?string $messageId = null;
2023-10-09 11:59:26 +00:00
private ?string $deliveryTime = null;
public function __construct()
{
parent::__construct(Event::MESSAGING_QUEUE_NAME, Event::MESSAGING_CLASS_NAME);
}
2023-09-05 17:40:33 +00:00
/**
* Sets message ID for the messaging event.
*
* @param string $message
* @return self
*/
public function setMessageId(string $messageId): self
{
$this->messageId = $messageId;
return $this;
}
/**
* Returns set message ID for the messaging event.
*
* @return string
*/
public function getMessageId(): string
{
return $this->messageId;
}
/**
2023-10-09 11:59:26 +00:00
* Sets Delivery time for the messaging event.
*
2023-10-09 11:59:26 +00:00
* @param string $deliveryTime
* @return self
*/
public function setDeliveryTime(string $deliveryTime): self
{
$this->deliveryTime = $deliveryTime;
return $this;
}
/**
* Returns set Delivery Time for the messaging event.
*
* @return string
*/
public function getDeliveryTime(): string
{
return $this->deliveryTime;
}
2023-10-13 12:56:54 +00:00
/**
* Set project for this event.
*
* @param Document $project
* @return self
*/
public function setProject(Document $project): self
{
$this->project = $project;
return $this;
}
2023-10-09 11:59:26 +00:00
/**
* Executes the event and sends it to the messaging worker.
2023-10-13 12:56:54 +00:00
* @return string|bool
* @throws \InvalidArgumentException
*/
2023-10-09 11:59:26 +00:00
public function trigger(): string | bool
{
2023-10-13 12:56:54 +00:00
return Resque::enqueue($this->queue, $this->class, [
'project' => $this->project,
'user' => $this->user,
'messageId' => $this->messageId,
]);
}
/**
* Schedules the messaging event and schedules it in the messaging worker queue.
*
* @return void
* @throws \Resque_Exception
* @throws \ResqueScheduler_InvalidTimestampException
*/
public function schedule(): void
{
ResqueScheduler::enqueueAt(new \DateTime($this->deliveryTime, new \DateTimeZone('UTC')), $this->queue, $this->class, [
'project' => $this->project,
'user' => $this->user,
'messageId' => $this->messageId,
]);
}
}