Elgg  Version 4.3
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 
27  protected $db;
28 
34  public function __construct(Database $db) {
35  $this->db = $db;
36  }
37 
47  public function queueEmail(int $recipient_guid, string $delivery_interval, $item): bool {
48  $insert = Insert::intoTable(self::TABLE_NAME);
49  $insert->values([
50  'recipient_guid' => $insert->param($recipient_guid, ELGG_VALUE_GUID),
51  'delivery_interval' => $insert->param($delivery_interval, ELGG_VALUE_STRING),
52  'data' => $insert->param(serialize($item), ELGG_VALUE_STRING),
53  'timestamp' => $insert->param($this->getCurrentTime()->getTimestamp(), ELGG_VALUE_TIMESTAMP),
54  ]);
55 
56  return $this->db->insertData($insert) !== false;
57  }
58 
66  public function getRow(int $id): ?DatabaseRecord {
67  $select = Select::fromTable(self::TABLE_NAME);
68  $select->select('*')
69  ->where($select->compare('id', '=', $id, ELGG_VALUE_ID));
70 
71  return $this->db->getDataRow($select, [$this, 'rowToRecord']);
72  }
73 
83  public function getRecipientRows(int $recipient_guid, string $delivery_interval, int $timestamp = null): array {
84  $select = Select::fromTable(self::TABLE_NAME);
85  $select->select('*')
86  ->where($select->compare('recipient_guid', '=', $recipient_guid, ELGG_VALUE_GUID))
87  ->andWhere($select->compare('delivery_interval', '=', $delivery_interval, ELGG_VALUE_STRING))
88  ->andWhere($select->compare('timestamp', '<', $timestamp ?? $this->getCurrentTime()->getTimestamp(), ELGG_VALUE_TIMESTAMP));
89 
90  return $this->db->getData($select, [$this, 'rowToRecord']);
91  }
92 
101  public function getIntervalRows(string $delivery_interval, int $timestamp = null): array {
102  $select = Select::fromTable(self::TABLE_NAME);
103  $select->select('*')
104  ->where($select->compare('delivery_interval', '=', $delivery_interval, ELGG_VALUE_STRING))
105  ->andWhere($select->compare('timestamp', '<', $timestamp ?? $this->getCurrentTime()->getTimestamp()))
106  ->orderBy('recipient_guid', 'ASC')
107  ->addOrderBy('timestamp', 'ASC');
108 
109  return $this->db->getData($select, [$this, 'rowToRecord']);
110  }
111 
119  public function deleteRow(int $id): int {
120  $delete = Delete::fromTable(self::TABLE_NAME);
121  $delete->where($delete->compare('id', '=', $id, ELGG_VALUE_ID));
122 
123  return $this->db->deleteData($delete);
124  }
125 
135  public function deleteRecipientRows(int $recipient_guid, string $delivery_interval, int $timestamp = null): int {
136  $delete = Delete::fromTable(self::TABLE_NAME);
137  $delete->where($delete->compare('recipient_guid', '=', $recipient_guid, ELGG_VALUE_GUID))
138  ->andWhere($delete->compare('delivery_interval', '=', $delivery_interval, ELGG_VALUE_STRING))
139  ->andWhere($delete->compare('timestamp', '<', $timestamp ?? $this->getCurrentTime()->getTimestamp(), ELGG_VALUE_INTEGER));
140 
141  return $this->db->deleteData($delete);
142  }
143 
151  public function deleteAllRecipientRows(int $recipient_guid): int {
152  $delete = Delete::fromTable(self::TABLE_NAME);
153  $delete->where($delete->compare('recipient_guid', '=', $recipient_guid, ELGG_VALUE_GUID));
154 
155  return $this->db->deleteData($delete);
156  }
157 
166  public function updateRecipientInterval(int $recipient_guid, string $delivery_interval): bool {
167  $update = Update::table(self::TABLE_NAME);
168  $update->set('delivery_interval', $update->param($delivery_interval, ELGG_VALUE_STRING))
169  ->where($update->compare('recipient_guid', '=', $recipient_guid, ELGG_VALUE_GUID));
170 
171  return $this->db->updateData($update);
172  }
173 
181  public function rowToRecord(\stdClass $row): DatabaseRecord {
182  return new DatabaseRecord($row);
183  }
184 }
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:126
const ELGG_VALUE_GUID
Definition: constants.php:128
$delete
const ELGG_VALUE_ID
Definition: constants.php:129
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:130
static fromTable($table, $alias=null)
{}
Definition: Select.php:13
const ELGG_VALUE_STRING
Definition: constants.php:127
$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