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  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) !== false;
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 
80  public function getRecipientRows(int $recipient_guid, string $delivery_interval, int $timestamp = null): array {
81  $select = Select::fromTable(self::TABLE_NAME);
82  $select->select('*')
83  ->where($select->compare('recipient_guid', '=', $recipient_guid, ELGG_VALUE_GUID))
84  ->andWhere($select->compare('delivery_interval', '=', $delivery_interval, ELGG_VALUE_STRING))
85  ->andWhere($select->compare('timestamp', '<', $timestamp ?? $this->getCurrentTime()->getTimestamp(), ELGG_VALUE_TIMESTAMP));
86 
87  return $this->db->getData($select, [$this, 'rowToRecord']);
88  }
89 
98  public function getIntervalRows(string $delivery_interval, int $timestamp = null): array {
99  $select = Select::fromTable(self::TABLE_NAME);
100  $select->select('*')
101  ->where($select->compare('delivery_interval', '=', $delivery_interval, ELGG_VALUE_STRING))
102  ->andWhere($select->compare('timestamp', '<', $timestamp ?? $this->getCurrentTime()->getTimestamp()))
103  ->orderBy('recipient_guid', 'ASC')
104  ->addOrderBy('timestamp', 'ASC');
105 
106  return $this->db->getData($select, [$this, 'rowToRecord']);
107  }
108 
116  public function deleteRow(int $id): int {
117  $delete = Delete::fromTable(self::TABLE_NAME);
118  $delete->where($delete->compare('id', '=', $id, ELGG_VALUE_ID));
119 
120  return $this->db->deleteData($delete);
121  }
122 
132  public function deleteRecipientRows(int $recipient_guid, string $delivery_interval, int $timestamp = null): int {
133  $delete = Delete::fromTable(self::TABLE_NAME);
134  $delete->where($delete->compare('recipient_guid', '=', $recipient_guid, ELGG_VALUE_GUID))
135  ->andWhere($delete->compare('delivery_interval', '=', $delivery_interval, ELGG_VALUE_STRING))
136  ->andWhere($delete->compare('timestamp', '<', $timestamp ?? $this->getCurrentTime()->getTimestamp(), ELGG_VALUE_INTEGER));
137 
138  return $this->db->deleteData($delete);
139  }
140 
148  public function deleteAllRecipientRows(int $recipient_guid): int {
149  $delete = Delete::fromTable(self::TABLE_NAME);
150  $delete->where($delete->compare('recipient_guid', '=', $recipient_guid, ELGG_VALUE_GUID));
151 
152  return $this->db->deleteData($delete);
153  }
154 
163  public function updateRecipientInterval(int $recipient_guid, string $delivery_interval): bool {
164  $update = Update::table(self::TABLE_NAME);
165  $update->set('delivery_interval', $update->param($delivery_interval, ELGG_VALUE_STRING))
166  ->where($update->compare('recipient_guid', '=', $recipient_guid, ELGG_VALUE_GUID));
167 
168  return $this->db->updateData($update);
169  }
170 
178  public function rowToRecord(\stdClass $row): DatabaseRecord {
179  return new DatabaseRecord($row);
180  }
181 }
if(!$items) $item
Definition: delete.php:13
getIntervalRows(string $delivery_interval, int $timestamp=null)
Get the queued items from the database for a given interval.
deleteRecipientRows(int $recipient_guid, string $delivery_interval, int $timestamp=null)
Delete all the queue items from the database for the given recipient and interval.
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:36
getCurrentTime($modifier= '')
Get the (cloned) time.
Definition: TimeUsing.php:25
static intoTable($table)
{}
Definition: Insert.php:13
getRecipientRows(int $recipient_guid, string $delivery_interval, int $timestamp=null)
Get all the rows in the queue for a given recipient.
rowToRecord(\stdClass $row)
Convert a database row to a managable 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
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
static fromTable($table, $alias=null)
{}
Definition: Delete.php:13