Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class MailQueue
- {
- /* PDO object for database operations */
- private $pdo;
- public function __construct(PDO &$pdo)
- {
- /* Set the PDO object */
- $this->pdo = $pdo;
- }
- /* Public function to add a new message to the queue. Returns the message ID */
- public function add_message(array $message): int
- {
- /* Check if the message is valid, otherwise throws an exception. */
- if ($this->validate_message($message))
- {
- /* Insert the base message to the queue */
- $queue_id = $this->insert_message($message);
- /* Insert the CC(N) addresses */
- $this->insert_cc($queue_id, $message['cc']);
- /* Insert the file attachments */
- $this->insert_attachments($queue_id, $message['attachments']);
- }
- else
- {
- /* Invalid message */
- throw new Exception('Invalid message.');
- }
- return $queue_id;
- }
- private function insert_message(array $message): int
- {
- /* New message ID */
- $queue_id = NULL;
- /* Insert query */
- $query = 'INSERT INTO mailqueue.queue (queue_app_id, queue_from, queue_recipient, queue_subject, queue_text_body, queue_html_body, queue_priority, queue_added, queue_schedule) VALUES (:app_id, :from, :recipient, :subject, :text_body, :html_body, :priority, NOW(), :schedule)';
- /* Query values */
- $params = array(
- ':app_id' => $message[app_id],
- ':from' => $message['from'],
- ':recipient' => $message['recipient'],
- ':subject' => $message['subject'],
- ':text_body' => $message['text_body'],
- ':html_body' => $message['html_body'],
- ':priority' => $message['priority'],
- ':schedule' => $message['schedule']);
- /* Check for PDO Exceptions (query errors) */
- try
- {
- /* Execute the query */
- $res = $this->pdo->prepare($query, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
- $res->execute($params);
- /* Retrieve the new message id */
- $queue_id = $this->pdo->lastInsertId();
- }
- catch (PDOException $e)
- {
- /* Throw a standard exception */
- throw new Exception('Database error: ' . $e->getMessage());
- }
- return $queue_id;
- }
- private function validate_message(array $message): bool
- {
- /* TODO */
- return TRUE;
- }
- private function insert_cc(int $queue_id, array $message)
- {
- /* TODO */
- return TRUE;
- }
- private function insert_attachments(int $queue_id, array $message)
- {
- /* TODO */
- return TRUE;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement