Elgg  Version master
DelayedEmailQueueTable.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Database;
4 
5 use Elgg\Database;
8 
16 
17  use TimeUsing;
18 
22  public const TABLE_NAME = 'delayed_email_queue';
23 
29  public function __construct(protected Database $db) {
30  }
31 
41  public function queueEmail(int $recipient_guid, string $delivery_interval, $item): bool {
42  $insert = Insert::intoTable(self::TABLE_NAME);
43  $insert->values([
44  'recipient_guid' => $insert->param($recipient_guid, ELGG_VALUE_GUID),
45  'delivery_interval' => $insert->param($delivery_interval, ELGG_VALUE_STRING),
46  'data' => $insert->param(serialize($item), ELGG_VALUE_STRING),
47  'timestamp' => $insert->param($this->getCurrentTime()->getTimestamp(), ELGG_VALUE_TIMESTAMP),
48  ]);
49 
50  return $this->db->insertData($insert) !== 0;
51  }
52 
60  public function getRow(int $id): ?DatabaseRecord {
61  $select = Select::fromTable(self::TABLE_NAME);
62  $select->select('*')
63  ->where($select->compare('id', '=', $id, ELGG_VALUE_ID));
64 
65  return $this->db->getDataRow($select, [$this, 'rowToRecord']);
66  }
67 
78  public function getRecipientRows(int $recipient_guid, string $delivery_interval, int $timestamp = null, int $max_results = 0): array {
79  $select = Select::fromTable(self::TABLE_NAME);
80  $select->select('*')
81  ->where($select->compare('recipient_guid', '=', $recipient_guid, ELGG_VALUE_GUID))
82  ->andWhere($select->compare('delivery_interval', '=', $delivery_interval, ELGG_VALUE_STRING))
83  ->andWhere($select->compare('timestamp', '<', $timestamp ?? $this->getCurrentTime()->getTimestamp(), ELGG_VALUE_TIMESTAMP))
84  ->orderBy('timestamp', 'ASC')
85  ->addOrderBy('id', 'ASC');
86 
87  if ($max_results > 0) {
88  $select->setMaxResults($max_results);
89  }
90 
91  return $this->db->getData($select, [$this, 'rowToRecord']);
92  }
93 
102  public function getNextRecipientGUID(string $delivery_interval, int $timestamp = null): ?int {
103  $select = Select::fromTable(self::TABLE_NAME);
104  $select->select('recipient_guid')
105  ->where($select->compare('delivery_interval', '=', $delivery_interval, ELGG_VALUE_STRING))
106  ->andWhere($select->compare('timestamp', '<', $timestamp ?? $this->getCurrentTime()->getTimestamp()))
107  ->orderBy('timestamp', 'ASC')
108  ->addOrderBy('id', 'ASC')
109  ->setMaxResults(1);
110 
111  $row = $this->db->getDataRow($select);
112  if (empty($row)) {
113  return null;
114  }
115 
116  return (int) $row->recipient_guid;
117  }
118 
126  public function deleteRow(int $id): int {
127  $delete = Delete::fromTable(self::TABLE_NAME);
128  $delete->where($delete->compare('id', '=', $id, ELGG_VALUE_ID));
129 
130  return $this->db->deleteData($delete);
131  }
132 
143  public function deleteRecipientRows(int $recipient_guid, string $delivery_interval, int $timestamp = null, int $max_id = 0): int {
144  $delete = Delete::fromTable(self::TABLE_NAME);
145  $delete->where($delete->compare('recipient_guid', '=', $recipient_guid, ELGG_VALUE_GUID))
146  ->andWhere($delete->compare('delivery_interval', '=', $delivery_interval, ELGG_VALUE_STRING))
147  ->andWhere($delete->compare('timestamp', '<', $timestamp ?? $this->getCurrentTime()->getTimestamp(), ELGG_VALUE_INTEGER))
148  ->orderBy('timestamp', 'ASC')
149  ->addOrderBy('id', 'ASC');
150 
151  if ($max_id > 0) {
152  $delete->andWhere($delete->compare('id', '<=', $max_id, ELGG_VALUE_ID));
153  }
154 
155  return $this->db->deleteData($delete);
156  }
157 
165  public function deleteAllRecipientRows(int $recipient_guid): int {
166  $delete = Delete::fromTable(self::TABLE_NAME);
167  $delete->where($delete->compare('recipient_guid', '=', $recipient_guid, ELGG_VALUE_GUID));
168 
169  return $this->db->deleteData($delete);
170  }
171 
180  public function updateRecipientInterval(int $recipient_guid, string $delivery_interval): bool {
181  $update = Update::table(self::TABLE_NAME);
182  $update->set('delivery_interval', $update->param($delivery_interval, ELGG_VALUE_STRING))
183  ->where($update->compare('recipient_guid', '=', $recipient_guid, ELGG_VALUE_GUID));
184 
185  return $this->db->updateData($update);
186  }
187 
195  public function rowToRecord(\stdClass $row): DatabaseRecord {
196  return new DatabaseRecord($row);
197  }
198 }
if(!$items) $item
Definition: delete.php:13
static table(string $table)
Returns a QueryBuilder for updating data in a given table.
Definition: Update.php:17
queueEmail(int $recipient_guid, string $delivery_interval, $item)
Insert a delayed email into the queue.
The Elgg database.
Definition: Database.php:26
const ELGG_VALUE_INTEGER
Value types.
Definition: constants.php:111
const ELGG_VALUE_GUID
Definition: constants.php:113
$delete
const ELGG_VALUE_ID
Definition: constants.php:114
trait TimeUsing
Adds methods for setting the current time (for testing)
Definition: TimeUsing.php:10
$timestamp
Definition: date.php:34
static intoTable(string $table)
Returns a QueryBuilder for inserting data in a given table.
Definition: Insert.php:17
getCurrentTime($modifier= '')
Get the (cloned) time.
Definition: TimeUsing.php:25
deleteRecipientRows(int $recipient_guid, string $delivery_interval, int $timestamp=null, int $max_id=0)
Delete all the queue items from the database for the given recipient and interval.
foreach($recommendedExtensions as $extension) if(empty(ini_get('session.gc_probability'))||empty(ini_get('session.gc_divisor'))) $db
rowToRecord(\stdClass $row)
Convert a database row to a manageable object.
static fromTable(string $table)
Returns a QueryBuilder for deleting data from a given table.
Definition: Delete.php:17
deleteAllRecipientRows(int $recipient_guid)
Deletes all the queue items from the database for the given recipient.
__construct(protected Database $db)
Create new service.
deleteRow(int $id)
Remove a queue items from the database.
Interfaces with the database to perform operations on the delayed_email_queue table.
const ELGG_VALUE_TIMESTAMP
Definition: constants.php:115
getRecipientRows(int $recipient_guid, string $delivery_interval, int $timestamp=null, int $max_results=0)
Get all the rows in the queue for a given recipient.
const ELGG_VALUE_STRING
Definition: constants.php:112
$recipient_guid
Definition: mute.php:7
static fromTable(string $table, string $alias=null)
Returns a QueryBuilder for selecting data from a given table.
Definition: Select.php:18
updateRecipientInterval(int $recipient_guid, string $delivery_interval)
Update the queued notifications for the recipient to a new delivery interval.
getRow(int $id)
Get a row from the queue.
$id
Generic annotation delete action.
Definition: delete.php:6
getNextRecipientGUID(string $delivery_interval, int $timestamp=null)
Fetch the GUID of the next recipient to process.