Elgg  Version 5.1
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  const TABLE_NAME = 'delayed_email_queue';
23 
24  protected Database $db;
25 
31  public function __construct(Database $db) {
32  $this->db = $db;
33  }
34 
44  public function queueEmail(int $recipient_guid, string $delivery_interval, $item): bool {
45  $insert = Insert::intoTable(self::TABLE_NAME);
46  $insert->values([
47  'recipient_guid' => $insert->param($recipient_guid, ELGG_VALUE_GUID),
48  'delivery_interval' => $insert->param($delivery_interval, ELGG_VALUE_STRING),
49  'data' => $insert->param(serialize($item), ELGG_VALUE_STRING),
50  'timestamp' => $insert->param($this->getCurrentTime()->getTimestamp(), ELGG_VALUE_TIMESTAMP),
51  ]);
52 
53  return $this->db->insertData($insert) !== 0;
54  }
55 
63  public function getRow(int $id): ?DatabaseRecord {
64  $select = Select::fromTable(self::TABLE_NAME);
65  $select->select('*')
66  ->where($select->compare('id', '=', $id, ELGG_VALUE_ID));
67 
68  return $this->db->getDataRow($select, [$this, 'rowToRecord']);
69  }
70 
81  public function getRecipientRows(int $recipient_guid, string $delivery_interval, int $timestamp = null, int $max_results = 0): array {
82  $select = Select::fromTable(self::TABLE_NAME);
83  $select->select('*')
84  ->where($select->compare('recipient_guid', '=', $recipient_guid, ELGG_VALUE_GUID))
85  ->andWhere($select->compare('delivery_interval', '=', $delivery_interval, ELGG_VALUE_STRING))
86  ->andWhere($select->compare('timestamp', '<', $timestamp ?? $this->getCurrentTime()->getTimestamp(), ELGG_VALUE_TIMESTAMP))
87  ->orderBy('timestamp', 'ASC')
88  ->addOrderBy('id', 'ASC');
89 
90  if ($max_results > 0) {
91  $select->setMaxResults($max_results);
92  }
93 
94  return $this->db->getData($select, [$this, 'rowToRecord']);
95  }
96 
105  public function getNextRecipientGUID(string $delivery_interval, int $timestamp = null): ?int {
106  $select = Select::fromTable(self::TABLE_NAME);
107  $select->select('recipient_guid')
108  ->where($select->compare('delivery_interval', '=', $delivery_interval, ELGG_VALUE_STRING))
109  ->andWhere($select->compare('timestamp', '<', $timestamp ?? $this->getCurrentTime()->getTimestamp()))
110  ->orderBy('timestamp', 'ASC')
111  ->addOrderBy('id', 'ASC')
112  ->setMaxResults(1);
113 
114  $row = $this->db->getDataRow($select);
115  if (empty($row)) {
116  return null;
117  }
118 
119  return (int) $row->recipient_guid;
120  }
121 
129  public function deleteRow(int $id): int {
130  $delete = Delete::fromTable(self::TABLE_NAME);
131  $delete->where($delete->compare('id', '=', $id, ELGG_VALUE_ID));
132 
133  return $this->db->deleteData($delete);
134  }
135 
146  public function deleteRecipientRows(int $recipient_guid, string $delivery_interval, int $timestamp = null, int $max_id = 0): int {
147  $delete = Delete::fromTable(self::TABLE_NAME);
148  $delete->where($delete->compare('recipient_guid', '=', $recipient_guid, ELGG_VALUE_GUID))
149  ->andWhere($delete->compare('delivery_interval', '=', $delivery_interval, ELGG_VALUE_STRING))
150  ->andWhere($delete->compare('timestamp', '<', $timestamp ?? $this->getCurrentTime()->getTimestamp(), ELGG_VALUE_INTEGER))
151  ->orderBy('timestamp', 'ASC')
152  ->addOrderBy('id', 'ASC');
153 
154  if ($max_id > 0) {
155  $delete->andWhere($delete->compare('id', '<=', $max_id, ELGG_VALUE_ID));
156  }
157 
158  return $this->db->deleteData($delete);
159  }
160 
168  public function deleteAllRecipientRows(int $recipient_guid): int {
169  $delete = Delete::fromTable(self::TABLE_NAME);
170  $delete->where($delete->compare('recipient_guid', '=', $recipient_guid, ELGG_VALUE_GUID));
171 
172  return $this->db->deleteData($delete);
173  }
174 
183  public function updateRecipientInterval(int $recipient_guid, string $delivery_interval): bool {
184  $update = Update::table(self::TABLE_NAME);
185  $update->set('delivery_interval', $update->param($delivery_interval, ELGG_VALUE_STRING))
186  ->where($update->compare('recipient_guid', '=', $recipient_guid, ELGG_VALUE_GUID));
187 
188  return $this->db->updateData($update);
189  }
190 
198  public function rowToRecord(\stdClass $row): DatabaseRecord {
199  return new DatabaseRecord($row);
200  }
201 }
if(!$items) $item
Definition: delete.php:13
queueEmail(int $recipient_guid, string $delivery_interval, $item)
Insert a delayed email into the queue.
static table($table, $alias=null)
{}
Definition: Update.php:13
The Elgg database.
Definition: Database.php:25
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
getCurrentTime($modifier= '')
Get the (cloned) time.
Definition: TimeUsing.php:25
static intoTable($table)
{}
Definition: Insert.php:13
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.
rowToRecord(\stdClass $row)
Convert a database row to a manageable object.
deleteAllRecipientRows(int $recipient_guid)
Deletes all the queue items from the database for the given recipient.
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
static fromTable($table, $alias=null)
{}
Definition: Select.php:13
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
updateRecipientInterval(int $recipient_guid, string $delivery_interval)
Update the queued notifications for the recipient to a new delivery interval.
__construct(Database $db)
Create new service.
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.
static fromTable($table, $alias=null)
{}
Definition: Delete.php:13