PHP Classes

File: docs/files/AbstractEmailLogger.php.txt

Recommend this page to a friend!
  Classes of Kiril Savchev   ITE Logger   docs/files/AbstractEmailLogger.php.txt   Download  
File: docs/files/AbstractEmailLogger.php.txt
Role: Documentation
Content type: text/plain
Description: Documentation
Class: ITE Logger
Log messages to different storage PSR-3 compliant
Author: By
Last change: Update documentation
Date: 8 years ago
Size: 8,114 bytes
 

Contents

Class file image Download
<?php /** * AbstractEmailLogger file * * Copyright (c) 2016, Kiril Savchev * All rights reserved. * * @category Libs * @package Logger * * @author Kiril Savchev <k.savchev@gmail.com> * * @license https://opensource.org/licenses/BSD-3-Clause BSD 3 License * @link http://ifthenelse.info */ namespace Ite\Logger; use Psr\Log\AbstractLogger; use Ite\Logger\Exception\RuntimeException; /** * Base email logger * * This class is a base for email loggger. Its ancestors must implement only the * actual sending mail in send() method using prefered emailing library * * @version 1.0 * * @author Kiril Savchev <k.savchev@gmail.com> */ abstract class AbstractEmailLogger extends AbstractLogger { use MessagePrepareTrait; /** * The default receiver of emails * * If in email config for a level the 'to' or 'from' element is missed * this value will be used * * @var string */ protected $defaultTo; /** * The default encoding * * If in email config for a level the 'Content-Type' header is missed * this value will be used for charset in conjuction with the * $defaultContentType * * @var string */ protected $defaultEncoding = 'UTF-8'; /** * The default content type * * If in email config for a level the Content-Type header is missed * this value will be used in conjuction with the $defaultEncoding * * @var string */ protected $defaultContentType = 'text/plain'; /** * Email configs for log levels * * The keys are the log levels and their values are array with the email * parts - 'from', 'to', 'subject' and 'headers' * * @var array */ protected $config = []; /** * Abstract email logger class with the base methods for logging, parsing * and preparing the email and log message for sending * * @param array|string $config [Optional] * @param string $defaultTo [Optional] */ public function __construct($config = null, $defaultTo = '') { if (is_array($config)) { $this->config = $config; if ($defaultTo) { $this->defaultTo = $defaultTo; } } else { $this->defaultTo = $config; } } /** * Prepares the email parts * * Extracts the email config for the desired level and fills the missing * parts with the defaults * * @param string $level * @return array * @throws RuntimeException */ protected function prepareEmailParts($level) { if (array_key_exists($level, $this->config)) { $parts = $this->config[$level]; } else { $parts = []; } if (!array_key_exists('headers', $parts)) { $parts['headers'] = []; } if (!array_key_exists('Content-Type', $parts['headers'])) { if (empty($this->defaultContentType)) { throw new RuntimeException("No content type"); } $parts['headers']['Content-Type'] = $this->defaultContentType; if (!empty($this->defaultEncoding)) { $parts['headers']['Content-Type'] .= '; charset='.$this->defaultEncoding; } } if (!array_key_exists('to', $parts)) { if (empty($this->defaultTo)) { throw new RuntimeException("No recepient provided"); } $parts['to'] = $this->defaultTo; } $parts['headers']['To'] = $parts['to']; if (!array_key_exists('from', $parts)) { if (empty($this->defaultTo)) { throw new RuntimeException("No sender provided"); } $parts['from'] = $this->defaultTo; } $parts['headers']['From'] = $parts['from']; $this->config[$level] = $parts; return $parts; } /** * Do the actual logging * * Prepares the message body and email parts (from, to, headers, etc...) * and calls the abstract send() method * * @param string $level * @param string $message * @param array $context [Optional] * @throws InvalidArgumentException */ public function log($level, $message, array $context = []) { $parts = $this->prepareEmailParts($level); if (empty($parts['subject'])) { $parts['subject'] = $message; } $body = $this->prepareMessage($message, $context); $this->send($parts['to'], $parts['subject'], $body, $parts['headers']); } /** * The actual sending method * * Every class that extends AbstractMailLogger must implement own send() * method with the prefered sending method and/or library * * @param string $to The log receiver * @param string $subject The email subject. Typicaly the log raw message * @param string $message The prepared email message with datetime, context, etc... * @param array $headers [Optional] Additional headers * * @return bool */ abstract protected function send($to, $subject, $message, array $headers = []); /** * Get default receiver email address * * @return string */ public function getDefaultTo() { return $this->defaultTo; } /** * Get default mail encoding * * @return string */ public function getDefaultEncoding() { return $this->defaultEncoding; } /** * Get defaul mail content type * * @return string */ public function getDefaultContentType() { return $this->defaultContentType; } /** * Get logger config * * @return array */ public function getConfig() { return $this->config; } /** * Sets default receiver email address * * @param string $defaultTo * @return \Ite\Logger\AbstractEmailLogger */ public function setDefaultTo($defaultTo) { $this->defaultTo = $defaultTo; return $this; } /** * Sets default mail encoding * * @param string $defaultEncoding * @return \Ite\Logger\AbstractEmailLogger */ public function setDefaultEncoding($defaultEncoding) { $this->defaultEncoding = $defaultEncoding; return $this; } /** * sets default mail content type * * @param string $defaultContentType * @return \Ite\Logger\AbstractEmailLogger */ public function setDefaultContentType($defaultContentType) { $this->defaultContentType = $defaultContentType; return $this; } /** * Sets logger config * * @param array $config * @return \Ite\Logger\AbstractEmailLogger */ public function setConfig(array $config) { $this->config = $config; return $this; } }