<?php
/**
 * A class for logging information to an email inbox.
 */
class log_writer_email extends log_filter
{
    /**
     * @var array The email recipient(s).
     */
    protected $recipient;
    
    /**
     * @var string The email subject template.
     */
    protected $subject;
    
    /**
     * @var string The email message template.
     */
    protected $message;
    
    ////////////////////////////////////////////////////////////////////////////
    // Construct and destruct functions.

    /**
     * @param integer (optional) $filter The priority filter.
     * @param string/array (optional) $recipient The recipient(s).
     * @param string (optional) $subject The email subject template.
     * @param string (optional) $template The email message template.
     * @throws unexpectedvalueexception If the admin email address is not
     * present in server configurations.
     */
    public function __construct($filter = log::DEBUG, $recipient = null, $subject = ':timestamp (:priority) - :message', $message = ":timestamp (:priority)\n\n:message")
    {
        parent::__construct($filter);
        
        if ($recipient === null)
        {
            $recipient = getenv('SERVER_ADMIN');
            
            if ($recipient === false)
            {
                throw new unexpectedvalueexception('Administrator email address is not present in server configuration.');
            }
        }
        
        if (!is_array($recipient))
        {
            $recipient = array($recipient);
        }
        
        $this->recipient = $recipient;
        $this->subject = $subject;
        $this->message = $message;
    }
    
    ////////////////////////////////////////////////////////////////////////////
    // Overload functions.
    
    ////////////////////////////////////////////////////////////////////////////
    // Interface functions.

    ////////////////////////////////////////////////////////////////////////////
    // Internal functions.
    
    /**
     * Sends a log message to an email inbox.
     *
     * @param string $message The message.
     * @param integer $priority The priority.
     * @param string $timestamp The date.
     * @throws unexpectedvalueexception If the email log could not be sent.
     * @return log_writer_email A fluent interface.
     */
    protected function _write($message, $priority, $timestamp)
    {
        $subject = str_replace(array(':timestamp', ':priority', ':message'), array($timestamp, $priority, ((strlen($message) > 40) ? substr($message, 0, 37) . '...' : $message)), $this->subject);;
        $message = str_replace(array(':timestamp', ':priority', ':message'), array($timestamp, $priority, $message), $this->message);
        
        foreach ($this->recipient as $r)
        {
            if (!mail($r, $subject, $message))
            {
                throw new unexpectedvalueexception('Email log could not be sent.');
            }
        }
        
        return $this;
    }
}
