View source
<?php
namespace Drupal\mandrill;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
class MandrillAPI implements MandrillAPIInterface {
protected $config;
protected $log;
public function __construct(ConfigFactoryInterface $config_factory, LoggerChannelFactoryInterface $logger_factory) {
$this->config = $config_factory
->get('mandrill.settings');
$this->log = $logger_factory
->get('mandrill');
}
public function isLibraryInstalled() {
return class_exists('Mandrill');
}
public function getMessages($email) {
$messages = array();
try {
if ($mandrill = $this
->getAPIObject()) {
$messages = $mandrill->messages
->search("email:{$email}");
}
} catch (\Exception $e) {
\Drupal::messenger()
->addError(t('Mandrill: %message', array(
'%message' => $e
->getMessage(),
)));
$this->log
->error($e
->getMessage());
}
return $messages;
}
public function getTemplates() {
$templates = NULL;
try {
if ($mandrill = $this
->getAPIObject()) {
$templates = $mandrill->templates
->getList();
}
} catch (\Exception $e) {
\Drupal::messenger()
->addError(t('Mandrill: %message', array(
'%message' => $e
->getMessage(),
)));
$this->log
->error($e
->getMessage());
}
return $templates;
}
public function getSubAccounts() {
$accounts = array();
try {
if ($mandrill = $this
->getAPIObject()) {
$accounts = $mandrill->subaccounts
->getList();
}
} catch (\Exception $e) {
\Drupal::messenger()
->addError(t('Mandrill: %message', array(
'%message' => $e
->getMessage(),
)));
$this->log
->error($e
->getMessage());
}
return $accounts;
}
public function getWebhooks() {
$webhooks = array();
try {
if ($mandrill = $this
->getAPIObject()) {
$webhooks = $mandrill->webhooks
->getList();
}
} catch (\Exception $e) {
\Drupal::messenger()
->addError(t('Mandrill: %message', array(
'%message' => $e
->getMessage(),
)));
$this->log
->error($e
->getMessage());
}
return $webhooks;
}
public function getInboundDomains() {
$domains = array();
try {
if ($mandrill = $this
->getAPIObject()) {
$domains = $mandrill->inbound
->domains();
}
} catch (\Exception $e) {
\Drupal::messenger()
->addError(t('Mandrill: %message', array(
'%message' => $e
->getMessage(),
)));
$this->log
->error($e
->getMessage());
}
return $domains;
}
public function getUser() {
$users = array();
try {
if ($mandrill = $this
->getAPIObject()) {
$users = $mandrill->users
->info();
}
} catch (\Exception $e) {
\Drupal::messenger()
->addError(t('Mandrill: %message', array(
'%message' => $e
->getMessage(),
)));
$this->log
->error($e
->getMessage());
}
return $users;
}
public function getTags() {
$tags = array();
try {
if ($mandrill = $this
->getAPIObject()) {
$tags = $mandrill->tags
->info();
}
} catch (\Exception $e) {
\Drupal::messenger()
->addError(t('Mandrill: %message', array(
'%message' => $e
->getMessage(),
)));
$this->log
->error($e
->getMessage());
}
return $tags;
}
public function getTag($tag) {
$tag = NULL;
try {
if ($mandrill = $this
->getAPIObject()) {
$tag = $mandrill->tags
->info($tag);
}
} catch (\Exception $e) {
\Drupal::messenger()
->addError(t('Mandrill: %message', array(
'%message' => $e
->getMessage(),
)));
$this->log
->error($e
->getMessage());
}
return $tag;
}
public function getTagTimeSeries($tag) {
$data = array();
try {
if ($mandrill = $this
->getAPIObject()) {
$data = $mandrill->tags
->timeSeries($tag);
}
} catch (\Exception $e) {
\Drupal::messenger()
->addError(t('Mandrill: %message', array(
'%message' => $e
->getMessage(),
)));
$this->log
->error($e
->getMessage());
}
return $data;
}
public function getTagsAllTimeSeries() {
$data = array();
try {
if ($mandrill = $this
->getAPIObject()) {
$data = $mandrill->tags
->allTimeSeries();
}
} catch (\Exception $e) {
\Drupal::messenger()
->addError(t('Mandrill: %message', array(
'%message' => $e
->getMessage(),
)));
$this->log
->error($e
->getMessage());
}
return $data;
}
public function getSenders() {
$senders = array();
try {
if ($mandrill = $this
->getAPIObject()) {
$senders = $mandrill->senders
->getList();
}
} catch (\Exception $e) {
\Drupal::messenger()
->addError(t('Mandrill: %message', array(
'%message' => $e
->getMessage(),
)));
$this->log
->error($e
->getMessage());
}
return $senders;
}
public function getSender($email) {
$sender = NULL;
try {
if ($mandrill = $this
->getAPIObject()) {
$sender = $mandrill->senders
->info($email);
}
} catch (\Exception $e) {
\Drupal::messenger()
->addError(t('Mandrill: %message', array(
'%message' => $e
->getMessage(),
)));
$this->log
->error($e
->getMessage());
}
return $sender;
}
public function getSenderTimeSeries($email) {
$data = array();
try {
if ($mandrill = $this
->getAPIObject()) {
$data = $mandrill->senders
->timeSeries($email);
}
} catch (\Exception $e) {
\Drupal::messenger()
->addError(t('Mandrill: %message', array(
'%message' => $e
->getMessage(),
)));
$this->log
->error($e
->getMessage());
}
return $data;
}
public function getURLs() {
$urls = array();
try {
if ($mandrill = $this
->getAPIObject()) {
$urls = $mandrill->urls
->getList();
}
} catch (\Exception $e) {
\Drupal::messenger()
->addError(t('Mandrill: %message', array(
'%message' => $e
->getMessage(),
)));
$this->log
->error($e
->getMessage());
}
return $urls;
}
public function getURLTimeSeries($url) {
$data = array();
try {
if ($mandrill = $this
->getAPIObject()) {
$data = $mandrill->urls
->timeSeries($url);
}
} catch (\Exception $e) {
\Drupal::messenger()
->addError(t('Mandrill: %message', array(
'%message' => $e
->getMessage(),
)));
$this->log
->error($e
->getMessage());
}
return $data;
}
public function getInboundRoutes() {
$routes = array();
try {
if ($mandrill = $this
->getAPIObject()) {
$routes = $mandrill->inbound
->routes();
}
} catch (\Exception $e) {
\Drupal::messenger()
->addError(t('Mandrill: %message', array(
'%message' => $e
->getMessage(),
)));
$this->log
->error($e
->getMessage());
}
return $routes;
}
public function addInboundDomain($domain) {
$result = NULL;
try {
if ($mandrill = $this
->getAPIObject()) {
$result = $mandrill->inbound
->addDomain($domain);
}
} catch (\Exception $e) {
\Drupal::messenger()
->addError(t('Mandrill: %message', array(
'%message' => $e
->getMessage(),
)));
$this->log
->error($e
->getMessage());
}
return $result;
}
public function addWebhook($path, $events, $description = 'Drupal Webhook') {
$result = NULL;
try {
if ($mandrill = $this
->getAPIObject()) {
$result = $mandrill->webhooks
->add($GLOBALS['base_url'] . '/' . $path, $description, $events);
}
} catch (\Exception $e) {
\Drupal::messenger()
->addError(t('Mandrill: %message', array(
'%message' => $e
->getMessage(),
)));
$this->log
->error($e
->getMessage());
}
return $result;
}
public function deleteInboundDomain($domain) {
$result = NULL;
try {
if ($mandrill = $this
->getAPIObject()) {
$result = $mandrill->inbound
->deleteDomain($domain);
}
} catch (\Exception $e) {
\Drupal::messenger()
->addError(t('Mandrill: %message', array(
'%message' => $e
->getMessage(),
)));
$this->log
->error($e
->getMessage());
}
return $result;
}
public function addInboundRoute($domain, $pattern, $url) {
$result = NULL;
try {
if ($mandrill = $this
->getAPIObject()) {
$result = $mandrill->inbound
->addRoute($domain, $pattern, $url);
}
} catch (\Exception $e) {
\Drupal::messenger()
->addError(t('Mandrill: %message', array(
'%message' => $e
->getMessage(),
)));
$this->log
->error($e
->getMessage());
}
return $result;
}
public function sendTemplate($message, $template_id, $template_content) {
$result = NULL;
try {
if ($mandrill = $this
->getAPIObject()) {
$result = $mandrill->messages
->sendTemplate($template_id, $template_content, $message);
}
} catch (\Exception $e) {
\Drupal::messenger()
->addError(t('Mandrill: %message', array(
'%message' => $e
->getMessage(),
)));
$this->log
->error($e
->getMessage());
}
return $result;
}
public function send(array $message) {
if ($mailer = $this
->getAPIObject()) {
return $mailer->messages
->send($message);
}
else {
throw new \Exception('Could not load Mandrill API.');
}
}
private function getAPIObject($reset = FALSE) {
$api =& drupal_static(__FUNCTION__, NULL);
if ($api === NULL || $reset) {
if (!$this
->isLibraryInstalled()) {
$msg = t('Failed to load Mandrill PHP library. Please refer to the installation requirements.');
$this->log
->error($msg);
\Drupal::messenger()
->addError($msg);
return NULL;
}
$api_key = $this->config
->get('mandrill_api_key');
$api_timeout = $this->config
->get('mandrill_api_timeout');
if (empty($api_key)) {
$msg = t('Failed to load Mandrill API Key. Please check your Mandrill settings.');
$this->log
->error($msg);
\Drupal::messenger()
->addError($msg);
return FALSE;
}
$className = $this->config
->get('mandrill_api_classname');
$api = new $className($api_key, $api_timeout);
}
return $api;
}
}