Skip to content

Instantly share code, notes, and snippets.

@AmyStephen
Last active August 29, 2015 13:57
Show Gist options
  • Save AmyStephen/9431688 to your computer and use it in GitHub Desktop.
Save AmyStephen/9431688 to your computer and use it in GitHub Desktop.
What pattern does this use?
<?php
class Email implements EmailInterface
{
/**
* Email Handler
*
* @var object
* @since 1.0
*/
protected $handler;
/**
* Constructor
*
* @param EmailInterface $email
*
* @since 1.0
*/
public function __construct(EmailInterface $email)
{
$this->handler = $email;
}
/**
* Return parameter value or default
*
* @param string $key
* @param string $default
*
* @return mixed
* @since 1.0
*/
public function get($key, $default = null)
{
return $this->handler->get($key, $default);
}
/**
* Set parameter value
*
* @param string $key
* @param mixed $value
*
* @return $this
* @since 1.0
*/
public function set($key, $value = null)
{
$this->handler->set($key, $value);
return $this;
}
/**
* Send Email
*
* @return mixed
* @since 1.0
*/
public function send()
{
$this->handler->send();
return $this;
}
}
class PhpMailer extends AbstractHandler implements EmailInterface
{
// get, set, send methods that interact with phpMailer
}
class Swiftmailer extends AbstractHandler implements EmailInterface
{
// get, set, send methods that interact with Swiftmailer
}
// To use
// 1. Instantiate an Email Handler.
$options = array();
$options['mailer_transport'] = 'mail';
$options['site_name'] = 'Sitename';
$class = 'Molajo\\Email\\Swiftmailer';
$handler = new $class($options);
// 2. Instantiate the Email class, injecting it with the Handler.
$class = 'Molajo\\Email';
$email = new $class($handler);
// 3. Set email parameters
$email->set('to', 'AmyStephen@gmail.com,Fname Lname');
$email->set('from', 'AmyStephen@gmail.com,Fname Lname');
$email->set('reply_to', 'AmyStephen@gmail.com,FName LName');
$email->set('cc', 'AmyStephen@gmail.com,FName LName');
$email->set('bcc', 'AmyStephen@gmail.com,FName LName');
$email->set('subject', 'Welcome to our Site');
$email->set('body', 'Stuff goes here');
$email->set('mailer_html_or_text', 'text');
// 4. Send Email.
$email->send();
@weierophinney
Copy link

Combination proxy (the email class proxies to the underlying handler instance) and adapter (specific handlers).

@AmyStephen
Copy link
Author

Thank you, @weierophinney

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment