Server Messaging made easy

by atillaordog

I will talk in this article about a library I made for making easy server side messaging.

It is called ServerMessage and is a library for creating and sending messages on server. By default it saves the messages in DB, but the storage can be changed by implementing the storage interface and passing the new storage when creating the instance.

Also it has a very basic validation that can also be changed in the way described above.

It is a very flexible library, it can be used for a lot of things and in a lot of ways.
A functionality I am proud of is the filtering, currently it has two filter options added, one filters out emails and the other filters out urls. Other filters can be added, too, by implementing the filter interface and passing the filter when filtering.

A very basic example of usage:

include('autoload.php');

// Instantiate the class using explicit values
$message = new ServerMessage(
array(
'db' => array(
'server' => 'localhost',
'user' => 'root',
'pass' => '',
'database' => 'test',
'table_name' => 'messages'
)
)
);

// Check if storage exists and create it if does not
if ( !$message->storage_exists() )
{
$message->install_storage();
}

// Create message
$message->set_subject('Testing message abc@bcd.com');
$message->set_body('Body of the message, also containing filterable things like http://www.filterurl.dev or abcd@efgh.dev');
$message->set_sender(1, 'user');
$message->set_reciever(1, 'support');
$message->set_meta(array('meta_key' => 'meta_value'));
$message->send();

// Now the message is also set in the inner container, thus we can change one thing and send the message again.
$message->set_reciever(2, 'support');
$message->send();

// You can also update the message
$message->set_subject('Changed');
$message->update();

// To reset inner message, you can call
$message->reset_inner_message();

// Filtering messages is easy like
$message->filter_message();

It can be used in a lot of ways. Here is the full list of functions with docx descriptions for reference:

/**
* Constructor.
* @param array $config Can overwrite default config upon creation
* If no storage class is provided, the config default will be used
* @param ServerMessageInterfacesStorage $storage
* @param ServerMessageInterfacesValidation $validation The validation class that validates the message, can be set externally
*/
public function __construct(Array $config = array(), StorageInterface $storage = null, ValidationInterface $validation = null)

public function set_subject($subject = '')

public function set_body($body = '')

public function set_sender($sender_id = 0, $sender_type = '')

public function set_reciever($reciever_id = 0, $reciever_type = '')

public function set_meta($meta)

/**
* Set the status of one or more messages
* @param array|int $id
* @param int $status Must be one from the predefined statuses
* @return boolean
*/
public function change_status($id, $status)

/**
* Set one or more messages to read or unread
* @param array|int $id
* @param boolean $read True or false
* @return boolean
*/
public function set_read($id, $read)

/**
* After the parts of the message have been set, saves to storage
* If validation is set to true in config, validates before sending
* @return boolean
*/
public function send()

/**
* Updates the loaded message in the storage
* It will validate the message if set in config
* @return boolean
*/
public function update()

/**
* If sending was not successful, we get the validation errors
* @return array
*/
public function get_validation_errors()

/**
* Gets the inbox of a given object
* @param int $obj_id The id of the reciever
* @param string $obj_type The type of the reciever
* @param int $status Optional, taken in consideration if not null
* @param int $limit
* @param int $offset
* @return array Returns an array with message objects
*/
public function get_inbox($obj_id, $obj_type, $status = null, $limit = null, $offset = null)

/**
* Gets the number of messages in inbox
* @param int $obj_id The id of the reciever
* @param string $obj_type The type of the reciever
* @param int $status (Optional) Status filter
* @param int $read (Optional) Read filter
* @return int
*/
public function get_total_inbox($obj_id, $obj_type, $status = null, $read = null)

/**
* Gets the outbox of a given object
* @param int $obj_id The id of the sender
* @param string $obj_type The type of the sender
* @param int $status Optional, taken in consideration if not null
* @param int $limit
* @param int $offset
* @return array Returns an array with message objects
*/
public function get_outbox($obj_id, $obj_type, $status = null, $limit = null, $offset = null)

/**
* Gets the number of unapproved messages
* @param int $obj_id The id of the reciever
* @param string $obj_type The type of the reciever
* @param int $status (Optional) Status filter
* @return int
*/
public function get_total_outbox($obj_id, $obj_type, $status = null)

/**
* Get all the messages, pagination dependent
* @param int $limit
* @param int $offset
* @return array
*/
public function get_all($limit = null, $offset = null)

/**
* Get the total number of messages
*/
public function get_total_messages()

/**
* Get a single message by ID or the current message set
* @param int $message_id If null, returns current message
* @return ServerMessageEntityMessage
*/
public function get_single($message_id = null)

/**
* Deletes one or more messages
* @param array | int $message_id
* @return boolean
*/
public function delete_message($message_id)

/**
* Checks if storage exists and can be used
* @return boolean
*/
public function storage_exists()

/**
* Creates storage if that does not exist
*/
public function install_storage()

/**
* Clear out the storage created for messages
*/
public function remove_storage()

/**
* Filters the given message
* @param ServerMessageEntityMessage $message Optional, if not set, the inner message will be used
* @param array $filters Optional, can be used to add filters (Ex. array('facebook' => new FacebookFilter()))
* The filters need to extend the filter interface
* @param boolean $subject_only Filter only the subject
* @param boolean $delete_found Remove the found matches from the message
* @param boolean $save If true, saves the message back to storage (used when deleting threats)
* @return array Returns an associative array with the filtered message and the found matches
*/
public function filter_message(MessageEntity $message = null, Array $filters = array(), $subject_only = false, $delete_found = false, $save = false)

/**
* Return the inner statuses set in config
*/
public function get_statuses()

/**
* We need to reset our message entity sometimes, so we do that with this function
*/
public function reset_inner_message()

The source code can be found here:

https://github.com/atillaordog/ServerMessage